2 Commits
0.2 ... 0.2.2

Author SHA1 Message Date
0ea49ff1ad release: 0.2.2
- stop parsing options when pos args started
- add sections with newline before
2020-04-29 11:38:02 +03:00
325c6c25d7 release: 0.2.1
fix "rename invalid cross-device link"
2020-04-29 05:31:19 +03:00
5 changed files with 58 additions and 10 deletions

View File

@ -1,5 +1,5 @@
NAME=multini
VERSION=0.2
VERSION=0.2.2
GOCMD=go
LDFLAGS:="$(LDFLAGS) -X 'main.Version=$(VERSION)'"
GOBUILD=$(GOCMD) build -ldflags=$(LDFLAGS)

View File

@ -100,7 +100,7 @@ func init() {
}
func parseArgs() error {
gnuflag.Parse(true)
gnuflag.Parse(false)
// info
switch {

View File

@ -17,4 +17,5 @@ DefKey3=NoSpaces!
[SectionWithIndent]
Key=Value
[SectionWithoutNewLineBefore]
[NewSection]

View File

@ -89,6 +89,14 @@ func (obj *Ini) GetKeyVal(section, key, value string) error {
func (obj *Ini) AddSection(section string) *Section {
sect, err := obj.FindSection(section)
if err != nil {
sectSize := len(obj.Sections)
if sectSize > 1 {
prevSect := obj.Sections[sectSize-1].(*Section)
lineSize := len(prevSect.Lines)
if lineSize == 0 || lineSize > 0 && prevSect.Lines[lineSize-1].Type() != TEmptyLine {
obj.Sections[sectSize-1].(*Section).Lines = append(obj.Sections[sectSize-1].(*Section).Lines, &EmptyLine{})
}
}
var newSection Section
newSection.Name = section
newSection.Prefix = obj.Sections[len(obj.Sections)-1].Indent()

View File

@ -2,6 +2,7 @@ package main
import (
"bufio"
"io"
"io/ioutil"
"os"
"path/filepath"
@ -9,6 +10,47 @@ import (
"multini/types"
)
// Source: https://gist.github.com/var23rav/23ae5d0d4d830aff886c3c970b8f6c6b
/*
GoLang: os.Rename() give error "invalid cross-device link" for Docker container with Volumes.
MoveFile(source, destination) will work moving file between folders
*/
func tryMoveFile(sourcePath, destPath string) error {
inputFile, err := os.Open(sourcePath)
if err != nil {
return err
}
outputFile, err := os.Create(destPath)
if err != nil {
inputFile.Close()
return err
}
defer outputFile.Close()
_, err = io.Copy(outputFile, inputFile)
inputFile.Close()
if err != nil {
return err
}
// The copy was successful, so now delete the original file
err = os.Remove(sourcePath)
if err != nil {
return err
}
return nil
}
func tryRemoveRenameFile(sourcePath, destPath string) bool {
err := os.Remove(destPath)
if err != nil {
return false
}
err = os.Rename(sourcePath, destPath)
if err != nil {
return false
}
return true
}
func replaceOriginal(oldFile, newFile string) error {
realOldFile, err := filepath.EvalSymlinks(oldFile)
if err != nil {
@ -23,14 +65,11 @@ func replaceOriginal(oldFile, newFile string) error {
var uid, gid int = GetUidGid(infoOldFile)
err = os.Remove(realOldFile)
if err != nil {
return err
}
err = os.Rename(newFile, realOldFile)
if err != nil {
return err
if !tryRemoveRenameFile(newFile, realOldFile) {
err = tryMoveFile(newFile, realOldFile)
if err != nil {
return err
}
}
err = os.Chmod(realOldFile, mode)