Skip to content

Commit aa50903

Browse files
authored
Merge pull request #2 from jszroberto/master
Little refactor + Bug fixes
2 parents 4d4c412 + 9d30b58 commit aa50903

14 files changed

+703
-491
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
goml-linux-amd64
22
goml-darwin-amd64
33
vendor/*
4+
goml

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
all:
2+
go build -o goml cmd/goml/main.go
3+
test:
4+
go test

main.go cmd/goml/main.go

+8-36
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ package main
33
import (
44
"errors"
55
"fmt"
6-
"io/ioutil"
76
"os"
87

9-
"github.com/JulzDiverse/goml/goml"
8+
"github.com/JulzDiverse/goml"
109
"github.com/fatih/color"
1110
"github.com/urfave/cli"
1211
)
@@ -74,10 +73,7 @@ func getParam(c *cli.Context) {
7473
os.Exit(1)
7574
}
7675

77-
yaml, err := goml.ReadYamlFromFile(c.String("file"))
78-
exitWithError(err)
79-
80-
res, err := goml.Get(yaml, c.String("prop"))
76+
res, err := goml.GetFromFile(c.String("file"), c.String("prop"))
8177
exitWithError(err)
8278

8379
fmt.Println(res)
@@ -89,21 +85,14 @@ func setParam(c *cli.Context) {
8985
exitWithError(errors.New("invalid number of arguments"))
9086
}
9187

92-
yaml, err := goml.ReadYamlFromFile(c.String("file"))
93-
exitWithError(err)
94-
95-
var value string
88+
var err error
9689
if c.String("value") != "" {
97-
value = c.String("value")
90+
value := c.String("value")
91+
err = goml.SetInFile(c.String("file"), c.String("prop"), value)
9892
} else if c.String("key") != "" {
99-
bytes, err := ioutil.ReadFile(c.String("key"))
100-
exitWithError(err)
101-
value = string(bytes)
93+
err = goml.SetKeyInFile(c.String("file"), c.String("prop"), c.String("key"))
10294
}
103-
104-
err = goml.Set(yaml, c.String("prop"), value)
10595
exitWithError(err)
106-
goml.WriteYaml(yaml, c.String("file"))
10796
}
10897

10998
func deleteParam(c *cli.Context) {
@@ -112,15 +101,8 @@ func deleteParam(c *cli.Context) {
112101
exitWithError(errors.New("invalid number of arguments"))
113102
}
114103

115-
yaml, err := goml.ReadYamlFromFile(c.String("file"))
104+
err := goml.DeleteInFile(c.String("file"), c.String("prop"))
116105
exitWithError(err)
117-
118-
err = goml.Delete(yaml, c.String("prop"))
119-
if err != nil {
120-
exitWithError(errors.New("Couldn't delete property for path: " + c.String("prop")))
121-
}
122-
123-
goml.WriteYaml(yaml, c.String("file"))
124106
}
125107

126108
func transferParam(c *cli.Context) {
@@ -129,18 +111,8 @@ func transferParam(c *cli.Context) {
129111
exitWithError(errors.New("invalid number of arguments"))
130112
}
131113

132-
sourceYaml, err := goml.ReadYamlFromFile(c.String("file"))
133-
exitWithError(err)
134-
135-
destYaml, err := goml.ReadYamlFromFile(c.String("df"))
114+
err := goml.TransferToFile(c.String("file"), c.String("prop"), c.String("df"), c.String("dp"))
136115
exitWithError(err)
137-
138-
value, _ := goml.Get(sourceYaml, c.String("prop"))
139-
140-
err = goml.Set(destYaml, c.String("dp"), value)
141-
exitWithError(err)
142-
143-
goml.WriteYaml(destYaml, c.String("df"))
144116
}
145117

146118
func exitWithError(err error) {

delete.go

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package goml
2+
3+
import (
4+
"errors"
5+
"os"
6+
"strconv"
7+
"strings"
8+
9+
"github.com/smallfish/simpleyaml"
10+
)
11+
12+
func DeleteInFile(file string, path string) error {
13+
yaml, err := ReadYamlFromFile(file)
14+
if err != nil {
15+
return err
16+
}
17+
18+
err = Delete(yaml, path)
19+
if err != nil {
20+
return errors.New("Couldn't delete property for path: " + path)
21+
}
22+
23+
if yaml.IsMap() {
24+
keys, err := yaml.GetMapKeys()
25+
if err != nil {
26+
return err
27+
}
28+
29+
if len(keys) == 0 {
30+
err = os.Remove(path)
31+
if err != nil {
32+
return err
33+
}
34+
}
35+
}
36+
37+
return WriteYaml(yaml, file)
38+
}
39+
40+
func Delete(yml *simpleyaml.Yaml, path string) error {
41+
propsArr := strings.Split(path, ".")
42+
propName := propsArr[len(propsArr)-1]
43+
props := propsArr[:len(propsArr)-1]
44+
newPath := strings.Join(props, ".")
45+
46+
if index, err := strconv.Atoi(propName); err == nil {
47+
tmp, props := get(yml, newPath)
48+
prop, err := tmp.Array()
49+
if err != nil {
50+
return err
51+
}
52+
53+
prop = append(prop[:index], prop[index+1:]...)
54+
55+
updateYaml(yml, props, prop)
56+
return nil
57+
}
58+
59+
if strings.Contains(propName, ":") {
60+
tmp, props := get(yml, newPath)
61+
prop, err := tmp.Array()
62+
if err != nil {
63+
return err
64+
}
65+
66+
index, err := returnIndexForProp(propName, prop)
67+
if err != nil {
68+
return err
69+
}
70+
71+
prop = append(prop[:index], prop[index+1:]...)
72+
updateYaml(yml, props, prop)
73+
return nil
74+
}
75+
76+
var res map[interface{}]interface{}
77+
if len(propsArr) == 1 {
78+
tmp, err := yml.Map()
79+
if err != nil {
80+
return err
81+
}
82+
res = tmp
83+
} else {
84+
prop, _ := get(yml, newPath)
85+
tmp, err := prop.Map()
86+
if err != nil {
87+
return err
88+
}
89+
res = tmp
90+
}
91+
_, ok := res[propName]
92+
if ok {
93+
delete(res, propName)
94+
} else {
95+
return errors.New("property not found")
96+
}
97+
98+
return nil
99+
}

delete_test.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package goml_test
2+
3+
import (
4+
. "github.com/JulzDiverse/goml"
5+
6+
. "github.com/onsi/ginkgo"
7+
. "github.com/onsi/gomega"
8+
"github.com/smallfish/simpleyaml"
9+
)
10+
11+
var _ = Describe("Delete", func() {
12+
var yml *simpleyaml.Yaml
13+
var err error
14+
15+
BeforeEach(func() {
16+
yaml := `map:
17+
name: foo
18+
19+
array:
20+
- bar
21+
- var
22+
- zar
23+
24+
mapArray:
25+
- foo: bar
26+
zoo: lion
27+
arr:
28+
- one
29+
- two
30+
- three
31+
- foo: var
32+
boo: laa`
33+
34+
yml, err = simpleyaml.NewYaml([]byte(yaml))
35+
Expect(err).NotTo(HaveOccurred())
36+
})
37+
38+
It("should delete a value from a map", func() {
39+
err = Delete(yml, "map.name")
40+
Expect(err).NotTo(HaveOccurred())
41+
42+
_, err = Get(yml, "map.name")
43+
Expect(err).To(MatchError("property not found"))
44+
})
45+
46+
It("should delete a value from an array ", func() {
47+
err = Delete(yml, "array.0")
48+
Expect(err).NotTo(HaveOccurred())
49+
50+
_, err := Get(yml, "array.:bar")
51+
Expect(err).To(MatchError("property not found"))
52+
53+
err = Delete(yml, "array.:zar")
54+
Expect(err).NotTo(HaveOccurred())
55+
_, err = Get(yml, "array.:zar")
56+
Expect(err).To(MatchError("property not found"))
57+
})
58+
59+
It("should delete a value from an map inside an array ", func() {
60+
err = Delete(yml, "mapArray.foo:bar.zoo")
61+
Expect(err).NotTo(HaveOccurred())
62+
63+
_, err := Get(yml, "mapArray.foo:bar.zoo")
64+
Expect(err).To(HaveOccurred())
65+
})
66+
67+
It("should delete a value from an array inside a map which in turn is inside an array ", func() {
68+
err = Delete(yml, "mapArray.foo:bar.arr.0")
69+
Expect(err).NotTo(HaveOccurred())
70+
71+
_, err := Get(yml, "mapArray.foo:bar.arr.:one")
72+
Expect(err).To(MatchError("property not found"))
73+
74+
err = Delete(yml, "mapArray.foo:bar.arr.:two")
75+
Expect(err).NotTo(HaveOccurred())
76+
77+
_, err = Get(yml, "mapArray.foo:bar.arr.:two")
78+
Expect(err).To(MatchError("property not found"))
79+
})
80+
})

get.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package goml
2+
3+
import (
4+
"errors"
5+
"strconv"
6+
"strings"
7+
8+
"github.com/smallfish/simpleyaml"
9+
)
10+
11+
func GetFromFile(file string, path string) (interface{}, error) {
12+
yaml, err := ReadYamlFromFile(file)
13+
if err != nil {
14+
return nil, err
15+
}
16+
17+
return Get(yaml, path)
18+
}
19+
20+
func GetFromFileAsSimpleYaml(file string, path string) (*simpleyaml.Yaml, error) {
21+
yaml, err := ReadYamlFromFile(file)
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
return GetAsSimpleYaml(yaml, path)
27+
}
28+
29+
func Get(yml *simpleyaml.Yaml, path string) (interface{}, error) {
30+
val, ok := get(yml, path)
31+
if ok == nil {
32+
return nil, errors.New("property not found")
33+
}
34+
35+
result, err := ExtractType(val)
36+
return result, err
37+
}
38+
39+
func GetAsSimpleYaml(yml *simpleyaml.Yaml, path string) (*simpleyaml.Yaml, error) {
40+
val, ok := get(yml, path)
41+
if ok == nil {
42+
return nil, errors.New("property not found")
43+
}
44+
45+
return val, nil
46+
}
47+
48+
func get(yml *simpleyaml.Yaml, path string) (*simpleyaml.Yaml, []string) {
49+
solvedPath := []string{}
50+
51+
props := strings.Split(path, ".")
52+
for _, p := range props {
53+
if index, err := strconv.Atoi(p); err == nil {
54+
yml = yml.GetIndex(index)
55+
solvedPath = append(solvedPath, strconv.Itoa(index))
56+
continue
57+
}
58+
59+
if strings.Contains(p, ":") {
60+
if prop, err := yml.Array(); err == nil {
61+
index, err := returnIndexForProp(p, prop)
62+
if err != nil {
63+
return yml, nil
64+
}
65+
66+
yml = yml.GetIndex(index)
67+
solvedPath = append(solvedPath, strconv.Itoa(index))
68+
continue
69+
}
70+
}
71+
solvedPath = append(solvedPath, p)
72+
yml = yml.Get(p)
73+
}
74+
return yml, solvedPath
75+
}

0 commit comments

Comments
 (0)