MCPcopy
hub / github.com/TomWright/dasel / run

Function run

internal/cli/run.go:30–111  ·  view source on GitHub ↗
(o runOpts)

Source from the content-addressed store, hash-verified

28}
29
30func run(o runOpts) ([]byte, error) {
31 cfg, err := LoadConfig(o.ConfigPath)
32 if err != nil {
33 return nil, fmt.Errorf("error loading config: %w", err)
34 }
35
36 var opts []execution.ExecuteOptionFn
37
38 if o.OutFormat == "" && o.InFormat != "" {
39 o.OutFormat = o.InFormat
40 } else if o.OutFormat != "" && o.InFormat == "" {
41 o.InFormat = o.OutFormat
42 } else if o.OutFormat == "" {
43 o.OutFormat = cfg.DefaultFormat
44 }
45
46 readerOptions := parsing.DefaultReaderOptions()
47 applyReaderFlags(&readerOptions, o.ExtReadFlags, o.ExtReadWriteFlags)
48
49 var reader parsing.Reader
50 if len(o.InFormat) > 0 {
51 reader, err = parsing.Format(o.InFormat).NewReader(readerOptions)
52 if err != nil {
53 return nil, fmt.Errorf("failed to get input reader: %w", err)
54 }
55 }
56
57 writerOptions := parsing.DefaultWriterOptions()
58 writerOptions.Compact = o.Compact
59 applyWriterFlags(&writerOptions, o.ExtWriteFlags, o.ExtReadWriteFlags)
60
61 writer, err := parsing.Format(o.OutFormat).NewWriter(writerOptions)
62 if err != nil {
63 return nil, fmt.Errorf("failed to get output writer: %w", err)
64 }
65
66 opts = append(opts, variableOptions(o.Vars)...)
67
68 // Default to null. If stdin is being read then this will be overwritten.
69 inputData := model.NewNullValue()
70
71 var inputBytes []byte
72 if o.Stdin != nil {
73 inputBytes, err = io.ReadAll(o.Stdin)
74 if err != nil {
75 return nil, fmt.Errorf("error reading stdin: %w", err)
76 }
77 }
78
79 if len(inputBytes) > 0 {
80 if reader == nil {
81 return nil, fmt.Errorf("input format is required when reading stdin")
82 }
83 inputData, err = reader.Read(inputBytes)
84 if err != nil {
85 return nil, fmt.Errorf("error reading input: %w", err)
86 }
87 }

Callers 15

TestValue_EqualFunction · 0.85
TestValue_NotEqualFunction · 0.85
TestValue_LessThanFunction · 0.85
TestValue_GreaterThanFunction · 0.85
TestValue_CompareFunction · 0.85
TestValue_AddFunction · 0.85
TestValue_SubtractFunction · 0.85
TestValue_MultiplyFunction · 0.85
TestValue_DivideFunction · 0.85
TestValue_ModuloFunction · 0.85

Calls 15

ReadMethod · 0.95
WriteMethod · 0.95
DefaultReaderOptionsFunction · 0.92
FormatTypeAlias · 0.92
DefaultWriterOptionsFunction · 0.92
NewNullValueFunction · 0.92
WithVariableFunction · 0.92
WithUnstableFunction · 0.92
NewOptionsFunction · 0.92
ExecuteSelectorFunction · 0.92
LoadConfigFunction · 0.85
applyReaderFlagsFunction · 0.85

Tested by 15

TestValue_EqualFunction · 0.68
TestValue_NotEqualFunction · 0.68
TestValue_LessThanFunction · 0.68
TestValue_GreaterThanFunction · 0.68
TestValue_CompareFunction · 0.68
TestValue_AddFunction · 0.68
TestValue_SubtractFunction · 0.68
TestValue_MultiplyFunction · 0.68
TestValue_DivideFunction · 0.68
TestValue_ModuloFunction · 0.68