Skip to content

Commit

Permalink
Merge pull request #2 from jszroberto/master
Browse files Browse the repository at this point in the history
Little refactor + Bug fixes
  • Loading branch information
herrjulz authored Jul 25, 2017
2 parents 4d4c412 + 9d30b58 commit aa50903
Show file tree
Hide file tree
Showing 14 changed files with 703 additions and 491 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
goml-linux-amd64
goml-darwin-amd64
vendor/*
goml
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
all:
go build -o goml cmd/goml/main.go
test:
go test
44 changes: 8 additions & 36 deletions main.go → cmd/goml/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package main
import (
"errors"
"fmt"
"io/ioutil"
"os"

"github.com/JulzDiverse/goml/goml"
"github.com/JulzDiverse/goml"
"github.com/fatih/color"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -74,10 +73,7 @@ func getParam(c *cli.Context) {
os.Exit(1)
}

yaml, err := goml.ReadYamlFromFile(c.String("file"))
exitWithError(err)

res, err := goml.Get(yaml, c.String("prop"))
res, err := goml.GetFromFile(c.String("file"), c.String("prop"))
exitWithError(err)

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

yaml, err := goml.ReadYamlFromFile(c.String("file"))
exitWithError(err)

var value string
var err error
if c.String("value") != "" {
value = c.String("value")
value := c.String("value")
err = goml.SetInFile(c.String("file"), c.String("prop"), value)
} else if c.String("key") != "" {
bytes, err := ioutil.ReadFile(c.String("key"))
exitWithError(err)
value = string(bytes)
err = goml.SetKeyInFile(c.String("file"), c.String("prop"), c.String("key"))
}

err = goml.Set(yaml, c.String("prop"), value)
exitWithError(err)
goml.WriteYaml(yaml, c.String("file"))
}

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

yaml, err := goml.ReadYamlFromFile(c.String("file"))
err := goml.DeleteInFile(c.String("file"), c.String("prop"))
exitWithError(err)

err = goml.Delete(yaml, c.String("prop"))
if err != nil {
exitWithError(errors.New("Couldn't delete property for path: " + c.String("prop")))
}

goml.WriteYaml(yaml, c.String("file"))
}

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

sourceYaml, err := goml.ReadYamlFromFile(c.String("file"))
exitWithError(err)

destYaml, err := goml.ReadYamlFromFile(c.String("df"))
err := goml.TransferToFile(c.String("file"), c.String("prop"), c.String("df"), c.String("dp"))
exitWithError(err)

value, _ := goml.Get(sourceYaml, c.String("prop"))

err = goml.Set(destYaml, c.String("dp"), value)
exitWithError(err)

goml.WriteYaml(destYaml, c.String("df"))
}

func exitWithError(err error) {
Expand Down
99 changes: 99 additions & 0 deletions delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package goml

import (
"errors"
"os"
"strconv"
"strings"

"github.com/smallfish/simpleyaml"
)

func DeleteInFile(file string, path string) error {
yaml, err := ReadYamlFromFile(file)
if err != nil {
return err
}

err = Delete(yaml, path)
if err != nil {
return errors.New("Couldn't delete property for path: " + path)
}

if yaml.IsMap() {
keys, err := yaml.GetMapKeys()
if err != nil {
return err
}

if len(keys) == 0 {
err = os.Remove(path)
if err != nil {
return err
}
}
}

return WriteYaml(yaml, file)
}

func Delete(yml *simpleyaml.Yaml, path string) error {
propsArr := strings.Split(path, ".")
propName := propsArr[len(propsArr)-1]
props := propsArr[:len(propsArr)-1]
newPath := strings.Join(props, ".")

if index, err := strconv.Atoi(propName); err == nil {
tmp, props := get(yml, newPath)
prop, err := tmp.Array()
if err != nil {
return err
}

prop = append(prop[:index], prop[index+1:]...)

updateYaml(yml, props, prop)
return nil
}

if strings.Contains(propName, ":") {
tmp, props := get(yml, newPath)
prop, err := tmp.Array()
if err != nil {
return err
}

index, err := returnIndexForProp(propName, prop)
if err != nil {
return err
}

prop = append(prop[:index], prop[index+1:]...)
updateYaml(yml, props, prop)
return nil
}

var res map[interface{}]interface{}
if len(propsArr) == 1 {
tmp, err := yml.Map()
if err != nil {
return err
}
res = tmp
} else {
prop, _ := get(yml, newPath)
tmp, err := prop.Map()
if err != nil {
return err
}
res = tmp
}
_, ok := res[propName]
if ok {
delete(res, propName)
} else {
return errors.New("property not found")
}

return nil
}
80 changes: 80 additions & 0 deletions delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package goml_test

import (
. "github.com/JulzDiverse/goml"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/smallfish/simpleyaml"
)

var _ = Describe("Delete", func() {
var yml *simpleyaml.Yaml
var err error

BeforeEach(func() {
yaml := `map:
name: foo
array:
- bar
- var
- zar
mapArray:
- foo: bar
zoo: lion
arr:
- one
- two
- three
- foo: var
boo: laa`

yml, err = simpleyaml.NewYaml([]byte(yaml))
Expect(err).NotTo(HaveOccurred())
})

It("should delete a value from a map", func() {
err = Delete(yml, "map.name")
Expect(err).NotTo(HaveOccurred())

_, err = Get(yml, "map.name")
Expect(err).To(MatchError("property not found"))
})

It("should delete a value from an array ", func() {
err = Delete(yml, "array.0")
Expect(err).NotTo(HaveOccurred())

_, err := Get(yml, "array.:bar")
Expect(err).To(MatchError("property not found"))

err = Delete(yml, "array.:zar")
Expect(err).NotTo(HaveOccurred())
_, err = Get(yml, "array.:zar")
Expect(err).To(MatchError("property not found"))
})

It("should delete a value from an map inside an array ", func() {
err = Delete(yml, "mapArray.foo:bar.zoo")
Expect(err).NotTo(HaveOccurred())

_, err := Get(yml, "mapArray.foo:bar.zoo")
Expect(err).To(HaveOccurred())
})

It("should delete a value from an array inside a map which in turn is inside an array ", func() {
err = Delete(yml, "mapArray.foo:bar.arr.0")
Expect(err).NotTo(HaveOccurred())

_, err := Get(yml, "mapArray.foo:bar.arr.:one")
Expect(err).To(MatchError("property not found"))

err = Delete(yml, "mapArray.foo:bar.arr.:two")
Expect(err).NotTo(HaveOccurred())

_, err = Get(yml, "mapArray.foo:bar.arr.:two")
Expect(err).To(MatchError("property not found"))
})
})
75 changes: 75 additions & 0 deletions get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package goml

import (
"errors"
"strconv"
"strings"

"github.com/smallfish/simpleyaml"
)

func GetFromFile(file string, path string) (interface{}, error) {
yaml, err := ReadYamlFromFile(file)
if err != nil {
return nil, err
}

return Get(yaml, path)
}

func GetFromFileAsSimpleYaml(file string, path string) (*simpleyaml.Yaml, error) {
yaml, err := ReadYamlFromFile(file)
if err != nil {
return nil, err
}

return GetAsSimpleYaml(yaml, path)
}

func Get(yml *simpleyaml.Yaml, path string) (interface{}, error) {
val, ok := get(yml, path)
if ok == nil {
return nil, errors.New("property not found")
}

result, err := ExtractType(val)
return result, err
}

func GetAsSimpleYaml(yml *simpleyaml.Yaml, path string) (*simpleyaml.Yaml, error) {
val, ok := get(yml, path)
if ok == nil {
return nil, errors.New("property not found")
}

return val, nil
}

func get(yml *simpleyaml.Yaml, path string) (*simpleyaml.Yaml, []string) {
solvedPath := []string{}

props := strings.Split(path, ".")
for _, p := range props {
if index, err := strconv.Atoi(p); err == nil {
yml = yml.GetIndex(index)
solvedPath = append(solvedPath, strconv.Itoa(index))
continue
}

if strings.Contains(p, ":") {
if prop, err := yml.Array(); err == nil {
index, err := returnIndexForProp(p, prop)
if err != nil {
return yml, nil
}

yml = yml.GetIndex(index)
solvedPath = append(solvedPath, strconv.Itoa(index))
continue
}
}
solvedPath = append(solvedPath, p)
yml = yml.Get(p)
}
return yml, solvedPath
}
Loading

0 comments on commit aa50903

Please sign in to comment.