Debian Codegraph @

internal/cli/read_write_flag.go

raw symbols SCIP
1 package cli
2
3 import (
4 "fmt"
5 "reflect"
6 "strings"
7
8 "github.com/alecthomas/kong"
9 "github.com/tomwright/dasel/v3/parsing"
10 )
11
12 type extReadWriteFlag struct {
13 Name string
14 Value string
15 }
16
17 type extReadWriteFlags *[]extReadWriteFlag
18
19 func applyReaderFlags(readerOptions *parsing.ReaderOptions, readerFlags extReadWriteFlags, readWriterFlags extReadWriteFlags) {
20 if readWriterFlags != nil {
21 for _, flag := range *readWriterFlags {
22 readerOptions.Ext[flag.Name] = flag.Value
23 }
24 }
25 if readerFlags != nil {
26 for _, flag := range *readerFlags {
27 readerOptions.Ext[flag.Name] = flag.Value
28 }
29 }
30 }
31
32 func applyWriterFlags(writerOptions *parsing.WriterOptions, writerFlags extReadWriteFlags, readWriterFlags extReadWriteFlags) {
33 if readWriterFlags != nil {
34 for _, flag := range *readWriterFlags {
35 writerOptions.Ext[flag.Name] = flag.Value
36 }
37 }
38 if writerFlags != nil {
39 for _, flag := range *writerFlags {
40 writerOptions.Ext[flag.Name] = flag.Value
41 }
42 }
43 }
44
45 type extReadWriteFlagMapper struct {
46 }
47
48 func (vm *extReadWriteFlagMapper) Decode(ctx *kong.DecodeContext, target reflect.Value) error {
49 t := ctx.Scan.Pop()
50
51 strVal, ok := t.Value.(string)
52 if !ok {
53 return fmt.Errorf("expected string value for variable")
54 }
55
56 nameValueSplit := strings.SplitN(strVal, "=", 2)
57 if len(nameValueSplit) != 2 {
58 return fmt.Errorf("invalid read/write flag format, expect foo=bar")
59 }
60
61 res := extReadWriteFlag{
62 Name: nameValueSplit[0],
63 Value: nameValueSplit[1],
64 }
65
66 target.Elem().Set(reflect.Append(target.Elem(), reflect.ValueOf(res)))
67
68 return nil
69 }