Skip to content

Commit

Permalink
implement paths
Browse files Browse the repository at this point in the history
  • Loading branch information
herrjulz committed Aug 7, 2018
1 parent bfbd1ef commit 82020c7
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 21 deletions.
158 changes: 144 additions & 14 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 23 additions & 2 deletions cmd/goml/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func main() {
cli.StringFlag{Name: "value, v", Usage: "value for the defined property"},
cli.StringFlag{Name: "key, k", Usage: "private key file"},
cli.BoolFlag{Name: "dry-run, d", Usage: "print set result to stdout"},
cli.BoolFlag{Name: "json, j", Usage: "format output as json"},
},
},
{
Expand All @@ -65,6 +66,14 @@ func main() {
cli.StringFlag{Name: "dp", Usage: "destination property path (string) - foo.bar.zoo"},
},
},
{
Name: "paths",
Usage: "Get paths of a yaml file",
Action: getPaths,
Flags: []cli.Flag{
cli.StringFlag{Name: "file, f", Usage: "path to YAML file"},
},
},
}
cmd.Run(os.Args)
}
Expand Down Expand Up @@ -100,13 +109,12 @@ func setParam(c *cli.Context) {
if c.Bool("dry-run") {
bytes, err := ioutil.ReadFile(c.String("file"))
exitWithError(err)
output, err := goml.SetInMemory(bytes, key, value)
output, err := goml.SetInMemory(bytes, key, value, c.Bool("json"))
exitWithError(err)
fmt.Println(string(output))
} else {
err = goml.SetInFile(c.String("file"), key, value)
}

}

exitWithError(err)
Expand All @@ -132,6 +140,19 @@ func transferParam(c *cli.Context) {
exitWithError(err)
}

func getPaths(c *cli.Context) {
filepath := c.String("file")
file, err := ioutil.ReadFile(filepath)
exitWithError(err)

paths, err := goml.GetPaths(file)
exitWithError(err)

for _, path := range paths {
fmt.Println(path)
}
}

func exitWithError(err error) {
if err != nil {
r := color.New(color.FgHiRed)
Expand Down
4 changes: 3 additions & 1 deletion get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
var _ = Describe("Get", func() {
var yml *simpleyaml.Yaml
var err error
var yaml string

BeforeEach(func() {
yaml := `map:
yaml = `map:
name: foo
array:
Expand Down Expand Up @@ -80,4 +81,5 @@ mapArray:
Expect(value).To(Equal("var"))
})
})

})
25 changes: 25 additions & 0 deletions paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package goml

import (
"strings"

"github.com/smallfish/simpleyaml"
"github.com/smallfish/simpleyaml/helper/util"
)

func GetPaths(file []byte) ([]string, error) {
yml, err := simpleyaml.NewYaml(file)
if err != nil {
return nil, err
}
paths, err := util.GetAllPaths(yml)
if err != nil {
return nil, err
}

for i, _ := range paths {
paths[i] = strings.Replace(paths[i], "/", ".", -1)
}

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

import (
"fmt"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

. "github.com/JulzDiverse/goml"
)

var _ = XDescribe("Paths", func() {
Context("When retrieving paths of a yaml file", func() {

var (
yaml string
err error
)

//JustBeforeEach(func() {
//sl, _ := GetPaths([]byte(yaml))
//paths = sl
//sl = []string{}
//fmt.Println("PATHS for test", test, paths)
//})

//AfterEach(func() {
//paths = nil
//})

Context("For a simple map", func() {
BeforeEach(func() {
yaml = `---
map:
name: julz`

})

It("should not fail", func() {
Expect(err).ToNot(HaveOccurred())
})

It("should return the paths", func() {
paths, err := GetPaths([]byte(yaml))
Expect(err).ToNot(HaveOccurred())
Expect(paths[0]).To(Equal("map.name"))
})

It("should be a valid goml path", func() {
paths, err := GetPaths([]byte(yaml))
Expect(err).ToNot(HaveOccurred())
result, err := GetInMemory([]byte(yaml), paths[0])
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal("julz"))
})
})

Context("For a simple array", func() {
BeforeEach(func() {
yaml = `---
array:
- julz`
})

It("should not fail", func() {
Expect(err).ToNot(HaveOccurred())
})

It("should return the path", func() {
paths, err := GetPaths([]byte(yaml))
Expect(err).ToNot(HaveOccurred())
fmt.Println("PATHS", paths)
Expect(paths[0]).To(Equal("array.0"))
})

It("should be a valid goml path", func() {
paths, err := GetPaths([]byte(yaml))
Expect(err).ToNot(HaveOccurred())
result, err := GetInMemory([]byte(yaml), paths[0])
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal("julz"))
})
})
})
})
Loading

0 comments on commit 82020c7

Please sign in to comment.