1
0
mirror of https://github.com/taigrr/yq synced 2025-01-18 04:53:17 -08:00

Compare commits

...

3 Commits
1.7 ... 1.8

Author SHA1 Message Date
Mike Farah
c22394b540 Updated readme 2017-04-12 21:30:29 +10:00
Mike Farah
b1ff47022b Updated readme 2017-04-12 21:20:43 +10:00
Mike Farah
82f1230db8 Added "new" command for creating new yaml 2017-04-12 21:10:00 +10:00
3 changed files with 145 additions and 1 deletions

View File

@@ -14,8 +14,12 @@ go get github.com/mikefarah/yaml
- Deep read a yaml file with a given path
- Update a yaml file given a path
- Update a yaml file given a script file
- Update creates any missing entries in the path on the fly
- Create a yaml file given a deep path and value
- Create a yaml file given a script file
- Convert from json to yaml
- Convert from yaml to json
- Pipe data in by using '-'
## Read examples
```
@@ -111,6 +115,7 @@ will output:
```
## Update examples
Existing yaml files can be updated via the write command
### Update to stdout
Given a sample.yaml file of:
@@ -128,6 +133,31 @@ b:
c: cat
```
### Update from STDIN
```bash
cat sample.yaml | yaml w - b.c blah
```
### Adding new fields
Any missing fields in the path will be created on the fly.
Given a sample.yaml file of:
```yaml
b:
c: 2
```
then
```bash
yaml w sample.yaml b.d[0] "new thing"
```
will output:
```yaml
b:
c: cat
d:
- new thing
```
### Updating yaml in-place
Given a sample.yaml file of:
```yaml
@@ -167,6 +197,51 @@ b:
- name: Howdy Partner
```
And, of course, you can pipe the instructions in using '-':
```bash
cat update_instructions.yaml | yaml w -s - sample.yaml
```
## New Examples
Yaml files can be created using the 'new' command. This works in the same way as the write command, but you don't pass in an existing Yaml file.
### Creating a simple yaml file
```bash
yaml n b.c cat
```
will output:
```yaml
b:
c: cat
```
### Creating using a create script
Create scripts follow the same format as the update scripts.
Given a script create_instructions.yaml of:
```yaml
b.c: 3
b.e[0].name: Howdy Partner
```
then
```bash
yaml n -s create_instructions.yaml
```
will output:
```yaml
b:
c: 3
e:
- name: Howdy Partner
```
You can also pipe the instructions in:
```bash
cat create_instructions.yaml | yaml n -s -
```
## Converting to and from json
### Yaml2json

54
yaml.go
View File

@@ -30,13 +30,14 @@ func main() {
var cmdRead = createReadCmd()
var cmdWrite = createWriteCmd()
var cmdNew = createNewCmd()
var rootCmd = &cobra.Command{Use: "yaml"}
rootCmd.PersistentFlags().BoolVarP(&trimOutput, "trim", "t", true, "trim yaml output")
rootCmd.PersistentFlags().BoolVarP(&outputToJSON, "tojson", "j", false, "output as json")
rootCmd.PersistentFlags().BoolVarP(&inputJSON, "fromjson", "J", false, "input as json")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose mode")
rootCmd.AddCommand(cmdRead, cmdWrite)
rootCmd.AddCommand(cmdRead, cmdWrite, cmdNew)
rootCmd.Execute()
}
@@ -87,6 +88,28 @@ a.b.e:
return cmdWrite
}
func createNewCmd() *cobra.Command {
var cmdNew = &cobra.Command{
Use: "new [path] [value]",
Aliases: []string{"n"},
Short: "yaml n [--script/-s script_file] a.b.c newValueForC",
Example: `
yaml new a.b.c cat
yaml n a.b.c cat
yaml n --script create_script.yaml
`,
Long: `Creates a new yaml w.r.t the given path and value.
Outputs to STDOUT
Create Scripts:
Note that you can give a create script to perform more sophisticated yaml. This follows the same format as the update script.
`,
Run: newProperty,
}
cmdNew.PersistentFlags().StringVarP(&writeScript, "script", "s", "", "yaml script for updating yaml")
return cmdNew
}
func readProperty(cmd *cobra.Command, args []string) {
if verbose {
backend.SetLevel(logging.DEBUG, "")
@@ -108,6 +131,35 @@ func read(args []string) interface{} {
return readMap(parsedData, paths[0], paths[1:len(paths)])
}
func newProperty(cmd *cobra.Command, args []string) {
if verbose {
backend.SetLevel(logging.DEBUG, "")
}
updatedData := newYaml(args)
print(updatedData)
}
func newYaml(args []string) interface{} {
var writeCommands map[string]interface{}
if writeScript != "" {
readData(writeScript, &writeCommands, false)
} else if len(args) < 2 {
die("Must provide <path_to_update> <value>")
} else {
writeCommands = make(map[string]interface{})
writeCommands[args[0]] = parseValue(args[1])
}
parsedData := make(yaml.MapSlice, 0)
for path, value := range writeCommands {
var paths = parsePath(path)
parsedData = writeMap(parsedData, paths, value)
}
return parsedData
}
func writeProperty(cmd *cobra.Command, args []string) {
if verbose {
backend.SetLevel(logging.DEBUG, "")

View File

@@ -36,6 +36,14 @@ application: MyApp`,
formattedResult)
}
func TestNewYaml(t *testing.T) {
result := newYaml([]string{"b.c", "3"})
formattedResult := fmt.Sprintf("%v", result)
assertResult(t,
"[{b [{c 3}]}]",
formattedResult)
}
func TestUpdateYaml(t *testing.T) {
result := updateYaml([]string{"sample.yaml", "b.c", "3"})
formattedResult := fmt.Sprintf("%v", result)
@@ -48,3 +56,12 @@ func TestUpdateYaml_WithScript(t *testing.T) {
writeScript = "instruction_sample.yaml"
updateYaml([]string{"sample.yaml"})
}
func TestNewYaml_WithScript(t *testing.T) {
writeScript = "instruction_sample.yaml"
result := newYaml([]string{""})
formattedResult := fmt.Sprintf("%v", result)
assertResult(t,
"[{b [{c cat} {e [[{name Mike Farah}]]}]}]",
formattedResult)
}