Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
c76140ffe1 | |||
a89e63f19f | |||
0ea49ff1ad | |||
325c6c25d7 |
2
Makefile
2
Makefile
@ -1,5 +1,5 @@
|
||||
NAME=multini
|
||||
VERSION=0.2
|
||||
VERSION=0.2.3
|
||||
GOCMD=go
|
||||
LDFLAGS:="$(LDFLAGS) -X 'main.Version=$(VERSION)'"
|
||||
GOBUILD=$(GOCMD) build -ldflags=$(LDFLAGS)
|
||||
|
2
args.go
2
args.go
@ -100,7 +100,7 @@ func init() {
|
||||
}
|
||||
|
||||
func parseArgs() error {
|
||||
gnuflag.Parse(true)
|
||||
gnuflag.Parse(false)
|
||||
|
||||
// info
|
||||
switch {
|
||||
|
@ -17,4 +17,5 @@ DefKey3=NoSpaces!
|
||||
[SectionWithIndent]
|
||||
Key=Value
|
||||
[SectionWithoutNewLineBefore]
|
||||
|
||||
[NewSection]
|
||||
|
@ -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()
|
||||
|
57
writer.go
57
writer.go
@ -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)
|
||||
@ -75,7 +114,7 @@ func iniWriteInplace(filename string, ini *types.Ini) error {
|
||||
}
|
||||
mode = info.Mode()
|
||||
}
|
||||
targetFile, err := os.OpenFile(realfilename, os.O_WRONLY|os.O_CREATE, mode)
|
||||
targetFile, err := os.OpenFile(realfilename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
|
||||
if err == nil {
|
||||
datawriter := bufio.NewWriter(targetFile)
|
||||
_, err = datawriter.WriteString(ini.Full())
|
||||
|
Reference in New Issue
Block a user