Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'indent' parameter to 'put' command #373

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/command/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func deleteFlags(cmd *cobra.Command) {
cmd.Flags().Bool("pretty", true, "Pretty print the output.")
cmd.Flags().Bool("colour", false, "Print colourised output.")
cmd.Flags().Bool("escape-html", false, "Escape HTML tags when writing output.")
cmd.Flags().Int("indent", 2, "The indention level when writing files.")
cmd.Flags().String("csv-comma", ",", "Comma separator to use when working with csv files.")
cmd.Flags().String("csv-write-comma", "", "Comma separator used when writing csv files. Overrides csv-comma when writing.")
cmd.Flags().String("csv-comment", "", "Comma separator used when reading csv files.")
Expand All @@ -44,6 +45,7 @@ func deleteRunE(cmd *cobra.Command, args []string) error {
colourFlag, _ := cmd.Flags().GetBool("colour")
escapeHTMLFlag, _ := cmd.Flags().GetBool("escape-html")
outFlag, _ := cmd.Flags().GetString("out")
indent, _ := cmd.Flags().GetInt("indent")
csvComma, _ := cmd.Flags().GetString("csv-comma")
csvWriteComma, _ := cmd.Flags().GetString("csv-write-comma")
csvComment, _ := cmd.Flags().GetString("csv-comment")
Expand All @@ -64,6 +66,7 @@ func deleteRunE(cmd *cobra.Command, args []string) error {
PrettyPrint: prettyPrintFlag,
Colourise: colourFlag,
EscapeHTML: escapeHTMLFlag,
Indent: indent,
CsvComma: csvWriteComma,
CsvUseCRLF: csvCRLF,
},
Expand Down
24 changes: 24 additions & 0 deletions internal/command/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,28 @@ func TestDeleteCommand(t *testing.T) {
nil,
nil,
))

t.Run("CheckIndentionForJSON", runTest(
[]string{"delete", "-r", "json", "--indent", "6", "--pretty=true", "x.y"},
[]byte(`{"x":{"x":1,"y":2}}`),
newline([]byte("{\n \"x\": {\n \"x\": 1\n }\n}")),
nil,
nil,
))

t.Run("CheckIndentionForYAML", runTest(
[]string{"delete", "-r", "json", "-w", "yaml", "--indent", "6", "--pretty=true", "x.y"},
[]byte(`{"x":{"x":1,"y":2}}`),
newline([]byte("x:\n x: 1")),
nil,
nil,
))

t.Run("CheckIndentionForTOML", runTest(
[]string{"delete", "-r", "json", "-w", "toml", "--indent", "6", "--pretty=true", "x.y"},
[]byte(`{"x":{"x":1,"y":2}}`),
newline([]byte("[x]\n x = 1")),
nil,
nil,
))
}
7 changes: 7 additions & 0 deletions internal/command/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/tomwright/dasel/v2/storage"
"io"
"os"
"strings"
)

type readOptions struct {
Expand Down Expand Up @@ -108,6 +109,8 @@ type writeOptions struct {
CsvComma string
// CsvUseCRLF determines whether CRLF is used when writing CSV files.
CsvUseCRLF bool

Indent int
}

func (o *writeOptions) writeToStdout() bool {
Expand Down Expand Up @@ -163,6 +166,10 @@ func (o *writeOptions) writeValues(cmd *cobra.Command, readOptions *readOptions,
options = append(options, storage.CsvCommaOption([]rune(o.CsvComma)[0]))
}

if o.Indent != 0 {
options = append(options, storage.IndentOption(strings.Repeat(" ", o.Indent)))
}

writer := o.Writer
if writer == nil {
if o.writeToStdout() {
Expand Down
3 changes: 3 additions & 0 deletions internal/command/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func putFlags(cmd *cobra.Command) {
cmd.Flags().Bool("pretty", true, "Pretty print the output.")
cmd.Flags().Bool("colour", false, "Print colourised output.")
cmd.Flags().Bool("escape-html", false, "Escape HTML tags when writing output.")
cmd.Flags().Int("indent", 2, "The indention level when writing files.")
cmd.Flags().String("csv-comma", ",", "Comma separator to use when working with csv files.")
cmd.Flags().String("csv-write-comma", "", "Comma separator used when writing csv files. Overrides csv-comma when writing.")
cmd.Flags().String("csv-comment", "", "Comma separator used when reading csv files.")
Expand All @@ -51,6 +52,7 @@ func putRunE(cmd *cobra.Command, args []string) error {
colourFlag, _ := cmd.Flags().GetBool("colour")
escapeHTMLFlag, _ := cmd.Flags().GetBool("escape-html")
outFlag, _ := cmd.Flags().GetString("out")
indent, _ := cmd.Flags().GetInt("indent")
csvComma, _ := cmd.Flags().GetString("csv-comma")
csvWriteComma, _ := cmd.Flags().GetString("csv-write-comma")
csvComment, _ := cmd.Flags().GetString("csv-comment")
Expand All @@ -71,6 +73,7 @@ func putRunE(cmd *cobra.Command, args []string) error {
PrettyPrint: prettyPrintFlag,
Colourise: colourFlag,
EscapeHTML: escapeHTMLFlag,
Indent: indent,
CsvComma: csvWriteComma,
CsvUseCRLF: csvCRLF,
},
Expand Down
24 changes: 24 additions & 0 deletions internal/command/put_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,28 @@ func TestPutCommand(t *testing.T) {
nil,
nil,
))

t.Run("VerifyCorrectIndentionForJSON", runTest(
[]string{"put", "-r", "json", "--indent", "6", "-t", "string", "--pretty=true", "-v", "Tom", "user"},
[]byte(`{}`),
newline([]byte("{\n \"user\": \"Tom\"\n}")),
nil,
nil,
))

t.Run("VerifyCorrectIndentionForYAML", runTest(
[]string{"put", "-r", "yaml", "--indent", "6", "-t", "string", "-v", "Tom", "user.name"},
[]byte(``),
newline([]byte("user:\n name: Tom")),
nil,
nil,
))

t.Run("VerifyCorrectIndentionForTOML", runTest(
[]string{"put", "-r", "toml", "--indent", "6", "-t", "string", "-v", "Tom", "user.name"},
[]byte(``),
newline([]byte("[user]\n name = 'Tom'")),
nil,
nil,
))
}
3 changes: 3 additions & 0 deletions internal/command/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func selectFlags(cmd *cobra.Command) {
cmd.Flags().Bool("pretty", true, "Pretty print the output.")
cmd.Flags().Bool("colour", false, "Print colourised output.")
cmd.Flags().Bool("escape-html", false, "Escape HTML tags when writing output.")
cmd.Flags().Int("indent", 2, "The indention level when writing files.")
cmd.Flags().String("csv-comma", ",", "Comma separator to use when working with csv files.")
cmd.Flags().String("csv-write-comma", "", "Comma separator used when writing csv files. Overrides csv-comma when writing.")
cmd.Flags().String("csv-comment", "", "Comma separator used when reading csv files.")
Expand All @@ -42,6 +43,7 @@ func selectRunE(cmd *cobra.Command, args []string) error {
prettyPrintFlag, _ := cmd.Flags().GetBool("pretty")
colourFlag, _ := cmd.Flags().GetBool("colour")
escapeHTMLFlag, _ := cmd.Flags().GetBool("escape-html")
indent, _ := cmd.Flags().GetInt("indent")
csvComma, _ := cmd.Flags().GetString("csv-comma")
csvWriteComma, _ := cmd.Flags().GetString("csv-write-comma")
csvComment, _ := cmd.Flags().GetString("csv-comment")
Expand All @@ -62,6 +64,7 @@ func selectRunE(cmd *cobra.Command, args []string) error {
PrettyPrint: prettyPrintFlag,
Colourise: colourFlag,
EscapeHTML: escapeHTMLFlag,
Indent: indent,
CsvComma: csvWriteComma,
CsvUseCRLF: csvCRLF,
},
Expand Down
24 changes: 24 additions & 0 deletions internal/command/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ func TestSelectCommand(t *testing.T) {
nil,
))

t.Run("VerifyCorrectIndentionForJSON", runTest(
[]string{"-r", "json", "--indent", "6", "--pretty=true", "users.all().filter(equal(flags.isBanned,true)).name"},
standardJsonSelectTestData(),
newline([]byte("{\n \"first\": \"Jim\",\n \"last\": \"Wright\"\n}")),
nil,
nil,
))

t.Run("VerifyCorrectIndentionForYAML", runTest(
[]string{"-r", "json", "-w", "yaml", "--indent", "6", "--pretty=true", "users.all().filter(equal(flags.isBanned,true))"},
standardJsonSelectTestData(),
newline([]byte("name:\n first: Jim\n last: Wright\nflags:\n isBanned: true")),
nil,
nil,
))

t.Run("VerifyCorrectIndentionForTOML", runTest(
[]string{"-r", "json", "-w", "toml", "--indent", "6", "--pretty=true", "users.all().filter(equal(flags.isBanned,true))"},
standardJsonSelectTestData(),
newline([]byte("[flags]\n isBanned = true\n\n[name]\n first = 'Jim'\n last = 'Wright'")),
nil,
nil,
))

t.Run("Issue258", runTest(
[]string{"-r", "json", "--pretty=false", "-w", "csv", "phones.all().mapOf(make,make,model,model,first,parent().parent().user.name.first,last,parent().parent().user.name.last).merge()"},
[]byte(`{
Expand Down
4 changes: 2 additions & 2 deletions storage/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func (p *YAMLParser) ToBytes(value dasel.Value, options ...ReadWriteOption) ([]b
colourise = value
}
case OptionIndent:
if value, ok := o.Value.(int); ok {
encoderOptions = append(encoderOptions, dencoding.YAMLEncodeIndent(value))
if value, ok := o.Value.(string); ok {
encoderOptions = append(encoderOptions, dencoding.YAMLEncodeIndent(len(value)))
}
}
}
Expand Down