mirror of
https://github.com/TomWright/dasel.git
synced 2022-05-22 02:32:45 +03:00
Add -v, --value flag to put commands
This commit is contained in:
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
- Nothing yet.
|
||||
### Added
|
||||
|
||||
- `-v`, `--value` flag to workaround [dash issue](https://github.com/TomWright/dasel/issues/117).
|
||||
|
||||
## [v1.19.0] - 2021-08-14
|
||||
|
||||
|
||||
@@ -290,7 +290,7 @@ func getOutputWriter(cmd *cobra.Command, in io.Writer, file string, out string)
|
||||
}
|
||||
|
||||
func putCommand() *cobra.Command {
|
||||
var fileFlag, selectorFlag, parserFlag, readParserFlag, writeParserFlag, outFlag string
|
||||
var fileFlag, selectorFlag, parserFlag, readParserFlag, writeParserFlag, outFlag, valueFlag string
|
||||
var multiFlag, compactFlag, mergeInputDocumentsFlag bool
|
||||
|
||||
cmd := &cobra.Command{
|
||||
@@ -315,6 +315,7 @@ func putCommand() *cobra.Command {
|
||||
cmd.PersistentFlags().BoolVarP(&multiFlag, "multiple", "m", false, "Select multiple results.")
|
||||
cmd.PersistentFlags().BoolVarP(&compactFlag, "compact", "c", false, "Compact the output by removing all pretty-printing where possible.")
|
||||
cmd.PersistentFlags().BoolVar(&mergeInputDocumentsFlag, "merge-input-documents", false, "Merge multiple input documents into an array.")
|
||||
cmd.PersistentFlags().StringVarP(&valueFlag, "value", "v", "", "Value to put.")
|
||||
|
||||
_ = cmd.MarkPersistentFlagFilename("file")
|
||||
|
||||
@@ -349,14 +350,16 @@ func getGenericInit(cmd *cobra.Command, args []string) func(options genericPutOp
|
||||
opts.Multi, _ = cmd.Flags().GetBool("multiple")
|
||||
opts.Compact, _ = cmd.Flags().GetBool("compact")
|
||||
opts.MergeInputDocuments, _ = cmd.Flags().GetBool("merge-input-documents")
|
||||
opts.Value, _ = cmd.Flags().GetString("value")
|
||||
|
||||
if opts.Selector == "" && len(args) > 0 {
|
||||
opts.Selector = args[0]
|
||||
args = args[1:]
|
||||
}
|
||||
|
||||
if len(args) > 0 {
|
||||
if opts.Value == "" && len(args) > 0 {
|
||||
opts.Value = args[0]
|
||||
args = args[1:]
|
||||
}
|
||||
|
||||
return opts
|
||||
|
||||
@@ -8,7 +8,6 @@ func putBoolCommand() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "bool -f <file> -s <selector> <value>",
|
||||
Short: "Update a bool property in the given document.",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runGenericPutCommand(genericPutOptions{
|
||||
ValueType: "bool",
|
||||
|
||||
@@ -98,15 +98,21 @@ func putDocumentCommand() *cobra.Command {
|
||||
Parser: cmd.Flag("parser").Value.String(),
|
||||
Selector: cmd.Flag("selector").Value.String(),
|
||||
DocumentParser: documentParserFlag,
|
||||
DocumentString: args[0],
|
||||
}
|
||||
opts.Multi, _ = cmd.Flags().GetBool("multiple")
|
||||
opts.Compact, _ = cmd.Flags().GetBool("compact")
|
||||
opts.DocumentString, _ = cmd.Flags().GetString("value")
|
||||
|
||||
if opts.Selector == "" && len(args) > 1 {
|
||||
if opts.Selector == "" && len(args) > 0 {
|
||||
opts.Selector = args[0]
|
||||
opts.DocumentString = args[1]
|
||||
args = args[1:]
|
||||
}
|
||||
|
||||
if opts.DocumentString == "" && len(args) > 0 {
|
||||
opts.DocumentString = args[0]
|
||||
args = args[1:]
|
||||
}
|
||||
|
||||
return runPutDocumentCommand(opts, cmd)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ func putIntCommand() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "int -f <file> -s <selector> <value>",
|
||||
Short: "Update an int property in the given document.",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runGenericPutCommand(genericPutOptions{
|
||||
ValueType: "int",
|
||||
|
||||
@@ -8,7 +8,6 @@ func putStringCommand() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "string -f <file> -s <selector> <value>",
|
||||
Short: "Update a string property in the given document.",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runGenericPutCommand(genericPutOptions{
|
||||
ValueType: "string",
|
||||
|
||||
@@ -60,16 +60,20 @@ func TestRootCMD_Put(t *testing.T) {
|
||||
}
|
||||
|
||||
func putTest(in string, varType string, parser string, selector string, value string, out string, expErr error, additionalArgs ...string) func(t *testing.T) {
|
||||
args := []string{
|
||||
"put", varType,
|
||||
}
|
||||
args = append(args, additionalArgs...)
|
||||
args = append(args, "-p", parser, selector, value)
|
||||
|
||||
return baseTest(in, out, expErr, args...)
|
||||
}
|
||||
|
||||
func baseTest(in string, out string, expErr error, args ...string) func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
cmd := command.NewRootCMD()
|
||||
outputBuffer := bytes.NewBuffer([]byte{})
|
||||
|
||||
args := []string{
|
||||
"put", varType,
|
||||
}
|
||||
args = append(args, additionalArgs...)
|
||||
args = append(args, "-p", parser, selector, value)
|
||||
|
||||
cmd.SetOut(outputBuffer)
|
||||
cmd.SetIn(strings.NewReader(in))
|
||||
cmd.SetArgs(args)
|
||||
@@ -653,6 +657,37 @@ foo: true
|
||||
]
|
||||
`, nil, "--merge-input-documents"))
|
||||
|
||||
t.Run("ValueFlag", func(t *testing.T) {
|
||||
// Test -v/--value flag
|
||||
// Workaround for https://github.com/TomWright/dasel/issues/117
|
||||
|
||||
t.Run("StringWithDash", baseTest(`{
|
||||
"id": "x"
|
||||
}`, `{
|
||||
"id": "-abc"
|
||||
}
|
||||
`, nil, "put", "string", "-p", "json", "-v", "-abc", ".id"))
|
||||
|
||||
t.Run("NegativeInt", baseTest(`{
|
||||
"id": 1
|
||||
}`, `{
|
||||
"id": -1
|
||||
}
|
||||
`, nil, "put", "int", "-p", "json", "-v", "-1", ".id"))
|
||||
})
|
||||
t.Run("StringWithDashWithSelectorFlag", baseTest(`{
|
||||
"id": "x"
|
||||
}`, `{
|
||||
"id": "-abc"
|
||||
}
|
||||
`, nil, "put", "string", "-p", "json", "-v", "-abc", "-s", ".id"))
|
||||
|
||||
t.Run("NegativeIntWithSelectorFlag", baseTest(`{
|
||||
"id": 1
|
||||
}`, `{
|
||||
"id": -1
|
||||
}
|
||||
`, nil, "put", "int", "-p", "json", "-v", "-1", "-s", ".id"))
|
||||
}
|
||||
|
||||
func TestRootCMD_Put_YAML(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user