github.com/tomwright/dasel@v1.27.3/internal/command/delete.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "github.com/spf13/cobra" 6 "github.com/tomwright/dasel" 7 "github.com/tomwright/dasel/storage" 8 "io" 9 ) 10 11 type deleteOptions struct { 12 File string 13 Parser string 14 ReadParser string 15 WriteParser string 16 Selector string 17 Reader io.Reader 18 Writer io.Writer 19 Multi bool 20 Compact bool 21 MergeInputDocuments bool 22 Out string 23 EscapeHTML bool 24 } 25 26 func runDeleteMultiCommand(cmd *cobra.Command, rootNode *dasel.Node, opts deleteOptions, writeParser storage.WriteParser, writeOptions []storage.ReadWriteOption) error { 27 err := rootNode.DeleteMultiple(opts.Selector) 28 29 written, err := customErrorHandling(customErrorHandlingOpts{ 30 File: opts.File, 31 Writer: opts.Writer, 32 Err: err, 33 Cmd: cmd, 34 NullFlag: false, 35 Out: opts.Out, 36 }) 37 if err != nil { 38 return fmt.Errorf("could not delete multiple node: %w", err) 39 } 40 if written { 41 return nil 42 } 43 44 if err := writeNodeToOutput(writeNodeToOutputOpts{ 45 Node: rootNode, 46 Parser: writeParser, 47 Writer: opts.Writer, 48 File: opts.File, 49 Out: opts.Out, 50 }, cmd, writeOptions...); err != nil { 51 return fmt.Errorf("could not write output: %w", err) 52 } 53 return nil 54 } 55 56 func runDeleteCommand(opts deleteOptions, cmd *cobra.Command) error { 57 readParser, err := getReadParser(opts.File, opts.ReadParser, opts.Parser) 58 if err != nil { 59 return err 60 } 61 rootNode, err := getRootNode(getRootNodeOpts{ 62 File: opts.File, 63 Parser: readParser, 64 Reader: opts.Reader, 65 MergeInputDocuments: opts.MergeInputDocuments, 66 }, cmd) 67 if err != nil { 68 return err 69 } 70 71 if !rootNode.Value.IsValid() { 72 rootNode = dasel.New(&storage.BasicSingleDocument{ 73 Value: map[string]interface{}{}, 74 }) 75 } 76 77 writeParser, err := getWriteParser(readParser, opts.WriteParser, opts.Parser, opts.Out, opts.File, "") 78 if err != nil { 79 return err 80 } 81 82 writeOptions := []storage.ReadWriteOption{ 83 storage.EscapeHTMLOption(opts.EscapeHTML), 84 } 85 86 if opts.Compact { 87 writeOptions = append(writeOptions, storage.PrettyPrintOption(false)) 88 } 89 90 if opts.Multi { 91 return runDeleteMultiCommand(cmd, rootNode, opts, writeParser, writeOptions) 92 } 93 94 if deleteErr := rootNode.Delete(opts.Selector); deleteErr != nil { 95 err = fmt.Errorf("could not delete node: %w", deleteErr) 96 } 97 written, err := customErrorHandling(customErrorHandlingOpts{ 98 File: opts.File, 99 Writer: opts.Writer, 100 Err: err, 101 Cmd: cmd, 102 NullFlag: false, 103 Out: opts.Out, 104 }) 105 if err != nil { 106 return err 107 } 108 if written { 109 return nil 110 } 111 112 if err := writeNodeToOutput(writeNodeToOutputOpts{ 113 Node: rootNode, 114 Parser: writeParser, 115 Writer: opts.Writer, 116 File: opts.File, 117 Out: opts.Out, 118 }, cmd, writeOptions...); err != nil { 119 return fmt.Errorf("could not write output: %w", err) 120 } 121 122 return nil 123 } 124 125 func deleteCommand() *cobra.Command { 126 var fileFlag, selectorFlag, parserFlag, readParserFlag, writeParserFlag, outFlag string 127 var plainFlag, multiFlag, compactFlag, mergeInputDocumentsFlag, escapeHTMLFlag bool 128 129 cmd := &cobra.Command{ 130 Use: "delete -f <file> -p <json,yaml,toml,xml,csv> <selector>", 131 Short: "Delete properties from the given file.", 132 RunE: func(cmd *cobra.Command, args []string) error { 133 if selectorFlag == "" && len(args) > 0 { 134 selectorFlag = args[0] 135 args = args[1:] 136 } 137 if plainFlag { 138 writeParserFlag = "-" 139 } 140 return runDeleteCommand(deleteOptions{ 141 File: fileFlag, 142 Parser: parserFlag, 143 ReadParser: readParserFlag, 144 WriteParser: writeParserFlag, 145 Selector: selectorFlag, 146 Multi: multiFlag, 147 Compact: compactFlag, 148 MergeInputDocuments: mergeInputDocumentsFlag, 149 Out: outFlag, 150 EscapeHTML: escapeHTMLFlag, 151 }, cmd) 152 }, 153 } 154 155 cmd.Flags().StringVarP(&fileFlag, "file", "f", "", "The file to delete from.") 156 cmd.Flags().StringVarP(&selectorFlag, "selector", "s", "", "The selector to use when deleting from the data structure.") 157 cmd.Flags().StringVarP(&parserFlag, "parser", "p", "", "Shorthand for -r FORMAT -w FORMAT.") 158 cmd.Flags().StringVarP(&readParserFlag, "read", "r", "", "The parser to use when reading.") 159 cmd.Flags().StringVarP(&writeParserFlag, "write", "w", "", "The parser to use when writing.") 160 cmd.Flags().BoolVar(&plainFlag, "plain", false, "Alias of -w plain") 161 cmd.Flags().BoolVarP(&multiFlag, "multiple", "m", false, "Delete multiple results.") 162 cmd.Flags().BoolVar(&mergeInputDocumentsFlag, "merge-input-documents", false, "Merge multiple input documents into an array.") 163 cmd.Flags().BoolVarP(&compactFlag, "compact", "c", false, "Compact the output by removing all pretty-printing where possible.") 164 cmd.Flags().BoolVar(&escapeHTMLFlag, "escape-html", false, "Escape HTML tags when writing output.") 165 cmd.Flags().StringVarP(&outFlag, "out", "o", "", "Output destination.") 166 167 _ = cmd.MarkFlagFilename("file") 168 169 return cmd 170 }