Debian Codegraph @

internal/cli/variable.go

raw symbols SCIP
1 package cliscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/internal/cli`/
2
3 import (
4 "fmtscip-go gomod github.com/golang/go/src go1.25 fmt/"
5 "ioscip-go gomod github.com/golang/go/src go1.25 io/"
6 "osscip-go gomod github.com/golang/go/src go1.25 os/"
7 "reflectscip-go gomod github.com/golang/go/src go1.25 reflect/"
8 "stringsscip-go gomod github.com/golang/go/src go1.25 strings/"
9
10 "github.com/alecthomas/kongscip-go gomod github.com/alecthomas/kong v1.14.0 `github.com/alecthomas/kong`/"
11 "github.com/tomwright/dasel/v3/executionscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/"
12 "github.com/tomwright/dasel/v3/modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/"
13 "github.com/tomwright/dasel/v3/parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/"
14 )
15
16 type variablescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/internal/cli`/variable# struct {
17 Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/internal/cli`/variable#Name. string
18 Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/internal/cli`/variable#Value. *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#
19 }
20
21 type variablesscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/internal/cli`/variables# *[]variablescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/internal/cli`/variable#
22
23 func variableOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/internal/cli`/variableOptions().(vars variablesscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/internal/cli`/variables#) []executionscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/.ExecuteOptionFnscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/ExecuteOptionFn# {
24 var opts []executionscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/.ExecuteOptionFnscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/ExecuteOptionFn#
25 if vars != nil {
26 for _, v := range *vars {
27 opts = append(opts, executionscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/.WithVariablescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/execution`/WithVariable().(v.Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/internal/cli`/variable#Name., v.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/internal/cli`/variable#Value.))
28 }
29 }
30 return opts
31 }
32
33 type variableMapperscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/internal/cli`/variableMapper# struct {
34 }
35
36 // Decode decodes a variable from a flag.
37 // E.g. --var foo=bar
38 // E.g. --var foo=json:{"bar":"baz"}
39 // E.g. --var foo=json:file:/path/to/file.json
40 func (vm *variableMapperscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/internal/cli`/variableMapper#) Decodescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/internal/cli`/variableMapper#Decode().(ctx *kongscip-go gomod github.com/alecthomas/kong v1.14.0 `github.com/alecthomas/kong`/.DecodeContextscip-go gomod github.com/alecthomas/kong v1.14.0 `github.com/alecthomas/kong`/DecodeContext#, target reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Valuescip-go gomod github.com/golang/go/src go1.25 reflect/Value#) error {
41 t := ctx.Scanscip-go gomod github.com/alecthomas/kong v1.14.0 `github.com/alecthomas/kong`/DecodeContext#Scan..Popscip-go gomod github.com/alecthomas/kong v1.14.0 `github.com/alecthomas/kong`/Scanner#Pop().()
42
43 strVal, ok := t.Valuescip-go gomod github.com/alecthomas/kong v1.14.0 `github.com/alecthomas/kong`/Token#Value..(string)
44 if !ok {
45 return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("expected string value for variable")
46 }
47
48 nameValueSplit := stringsscip-go gomod github.com/golang/go/src go1.25 strings/.SplitNscip-go gomod github.com/golang/go/src go1.25 strings/SplitN().(strVal, "=", 2)
49 if len(nameValueSplit) != 2 {
50 return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("invalid variable format, expect foo=bar, or foo=format:file:path")
51 }
52
53 res := variablescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/internal/cli`/variable#{
54 Namescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/internal/cli`/variable#Name.: nameValueSplit[0],
55 }
56
57 format := "dasel"
58 valueRaw := nameValueSplit[1]
59
60 firstSplit := stringsscip-go gomod github.com/golang/go/src go1.25 strings/.SplitNscip-go gomod github.com/golang/go/src go1.25 strings/SplitN().(valueRaw, ":", 2)
61 if len(firstSplit) == 2 {
62 format = firstSplit[0]
63 valueRaw = firstSplit[1]
64 }
65 if stringsscip-go gomod github.com/golang/go/src go1.25 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25 strings/HasPrefix().(valueRaw, "file:") {
66 filePath := valueRaw[len("file:"):]
67
68 f, err := osscip-go gomod github.com/golang/go/src go1.25 os/.Openscip-go gomod github.com/golang/go/src go1.25 os/Open().(filePath)
69 if err != nil {
70 return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("failed to open file: %w", err)
71 }
72 defer func() {
73 _ = f.Closescip-go gomod github.com/golang/go/src go1.25 os/File#Close().()
74 }()
75 contents, err := ioscip-go gomod github.com/golang/go/src go1.25 io/.ReadAllscip-go gomod github.com/golang/go/src go1.25 io/ReadAll().(f)
76 if err != nil {
77 return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("failed to read file contents: %w", err)
78 }
79 valueRaw = string(contents)
80 }
81
82 reader, err := parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.Formatscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#(format).NewReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#NewReader().(parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.DefaultReaderOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/DefaultReaderOptions().())
83 if err != nil {
84 return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("failed to create reader: %w", err)
85 }
86 res.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/internal/cli`/variable#Value., err = reader.Readscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Reader#Read.([]byte(valueRaw))
87 if err != nil {
88 return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("failed to read value: %w", err)
89 }
90
91 target.Elemscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Elem().().Setscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Set().(reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.Appendscip-go gomod github.com/golang/go/src go1.25 reflect/Append().(target.Elemscip-go gomod github.com/golang/go/src go1.25 reflect/Value#Elem().(), reflectscip-go gomod github.com/golang/go/src go1.25 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25 reflect/ValueOf().(res)))
92
93 return nil
94 }