release: 0.2

- fix inplace arg
- follow symlinks by default
- reverse flag
- quiet flag
- try chown/chmod on unix
This commit is contained in:
2020-04-27 14:35:10 +03:00
parent 294a89ba74
commit 702cd21256
10 changed files with 144 additions and 43 deletions

View File

@ -102,7 +102,7 @@ func (obj *Ini) SetSection(section string) *Section {
return obj.AddSection(section)
}
func (obj *Ini) AddKey(section, key, value string) error {
func (obj *Ini) AddKey(section, key, value string, reverse bool) error {
sect, err := obj.FindSection(section)
if err != nil {
if createIfNotExist() {
@ -111,7 +111,7 @@ func (obj *Ini) AddKey(section, key, value string) error {
return err
}
}
sect.AddKey(key, value)
sect.AddKey(key, value, reverse)
return nil
}

View File

@ -111,39 +111,54 @@ func (obj *Section) GetKeyVal(name, value string) error {
return errors.New("Parameter:Value not found: " + name + ":" + value)
}
func (obj *Section) appendKey(name, value string) {
func (obj *Section) appendKey(name, value string, reverse bool) {
var newKeyValue KeyValue
var replaceIndex int = -1
newKeyValue.Key = name
newKeyValue.Value = value
// replace first emptyline
for i := len(obj.Lines) - 1; i >= 0; i-- {
if obj.Lines[i].Type() == TEmptyLine {
replaceIndex = i
} else {
break
if reverse {
// for right indent and tabs
for i := 0; i < len(obj.Lines); i++ {
if obj.Lines[i].Type() == TKeyValue {
template := obj.Lines[i].(*KeyValue)
newKeyValue.PrefixKey = template.PrefixKey
newKeyValue.PostfixKey = template.PostfixKey
newKeyValue.PrefixValue = template.PrefixValue
newKeyValue.PostfixValue = template.PostfixValue
break
}
}
}
// for right indent and tabs
for i := len(obj.Lines) - 1; i >= 0; i-- {
if obj.Lines[i].Type() == TKeyValue {
template := obj.Lines[i].(*KeyValue)
newKeyValue.PrefixKey = template.PrefixKey
newKeyValue.PostfixKey = template.PostfixKey
newKeyValue.PrefixValue = template.PrefixValue
newKeyValue.PostfixValue = template.PostfixValue
break
}
}
if replaceIndex == -1 {
obj.Lines = append(obj.Lines, &newKeyValue)
obj.Lines = append([]Element{&newKeyValue}, obj.Lines...)
} else {
obj.Lines = append(obj.Lines, obj.Lines[replaceIndex])
obj.Lines[replaceIndex] = &newKeyValue
// replace first emptyline
for i := len(obj.Lines) - 1; i >= 0; i-- {
if obj.Lines[i].Type() == TEmptyLine {
replaceIndex = i
} else {
break
}
}
// for right indent and tabs
for i := len(obj.Lines) - 1; i >= 0; i-- {
if obj.Lines[i].Type() == TKeyValue {
template := obj.Lines[i].(*KeyValue)
newKeyValue.PrefixKey = template.PrefixKey
newKeyValue.PostfixKey = template.PostfixKey
newKeyValue.PrefixValue = template.PrefixValue
newKeyValue.PostfixValue = template.PostfixValue
break
}
}
if replaceIndex == -1 {
obj.Lines = append(obj.Lines, &newKeyValue)
} else {
obj.Lines = append(obj.Lines, obj.Lines[replaceIndex])
obj.Lines[replaceIndex] = &newKeyValue
}
}
}
func (obj *Section) AddKey(name, value string) {
func (obj *Section) AddKey(name, value string, reverse bool) {
gotIt := false
for i, keyVal := range obj.Lines {
if keyVal.Type() == TKeyValue &&
@ -157,7 +172,7 @@ func (obj *Section) AddKey(name, value string) {
}
}
if !gotIt {
obj.appendKey(name, value)
obj.appendKey(name, value, reverse)
}
}
@ -176,7 +191,7 @@ func (obj *Section) SetKey(name, value string) error {
}
if !gotIt {
if createIfNotExist() {
obj.appendKey(name, value)
obj.appendKey(name, value, false)
} else {
return errors.New("Parameter not found: " + name)
}