|
| 1 | +// SPDX-FileCopyrightText: 2023 Christoph Mewes |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package options |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "os" |
| 11 | + "regexp" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "go.xrstf.de/rudi/cmd/rudi/encoding" |
| 15 | + "go.xrstf.de/rudi/cmd/rudi/types" |
| 16 | + |
| 17 | + "github.com/spf13/pflag" |
| 18 | +) |
| 19 | + |
| 20 | +type Options struct { |
| 21 | + ShowHelp bool |
| 22 | + Interactive bool |
| 23 | + ScriptFile string |
| 24 | + StdinFormat types.Encoding |
| 25 | + OutputFormat types.Encoding |
| 26 | + PrintAst bool |
| 27 | + ShowVersion bool |
| 28 | + Coalescing types.Coalescing |
| 29 | + EnableRudispaceFunctions bool |
| 30 | + ExtraVariables map[string]any |
| 31 | + extraVariableFlags []string |
| 32 | +} |
| 33 | + |
| 34 | +func NewDefaultOptions() Options { |
| 35 | + return Options{ |
| 36 | + Coalescing: types.StrictCoalescing, |
| 37 | + StdinFormat: types.YamlEncoding, |
| 38 | + OutputFormat: types.JsonEncoding, |
| 39 | + ExtraVariables: map[string]any{}, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func (o *Options) AddFlags(fs *pflag.FlagSet) { |
| 44 | + fs.SortFlags = false |
| 45 | + |
| 46 | + stdinFormatFlag := newEnumFlag(&o.StdinFormat, types.AllEncodings...) |
| 47 | + outputFormatFlag := newEnumFlag(&o.OutputFormat, types.AllEncodings...) |
| 48 | + coalescingFlag := newEnumFlag(&o.Coalescing, types.AllCoalescings...) |
| 49 | + |
| 50 | + fs.BoolVarP(&o.Interactive, "interactive", "i", o.Interactive, "Start an interactive REPL to run expressions.") |
| 51 | + fs.StringVarP(&o.ScriptFile, "script", "s", o.ScriptFile, "Load Rudi script from file instead of first argument (only in non-interactive mode).") |
| 52 | + fs.StringArrayVar(&o.extraVariableFlags, "var", o.extraVariableFlags, "Define additional global variables (can be given multiple times).") |
| 53 | + stdinFormatFlag.Add(fs, "stdin-format", "f", "What data format is used for data provided on stdin") |
| 54 | + outputFormatFlag.Add(fs, "output-format", "o", "What data format to use for outputting data") |
| 55 | + fs.BoolVar(&o.EnableRudispaceFunctions, "enable-funcs", o.EnableRudispaceFunctions, "Enable the func! function to allow defining new functions in Rudi code.") |
| 56 | + coalescingFlag.Add(fs, "coalesce", "c", "Type conversion handling") |
| 57 | + fs.BoolVarP(&o.ShowHelp, "help", "h", o.ShowHelp, "Show help and documentation.") |
| 58 | + fs.BoolVarP(&o.ShowVersion, "version", "V", o.ShowVersion, "Show version and exit.") |
| 59 | + fs.BoolVarP(&o.PrintAst, "debug-ast", "", o.PrintAst, "Output syntax tree of the parsed script in non-interactive mode.") |
| 60 | +} |
| 61 | + |
| 62 | +func (o *Options) Validate() error { |
| 63 | + if o.Interactive && o.ScriptFile != "" { |
| 64 | + return errors.New("cannot combine --interactive with --script") |
| 65 | + } |
| 66 | + |
| 67 | + if o.Interactive && o.PrintAst { |
| 68 | + return errors.New("cannot combine --interactive with --debug-ast") |
| 69 | + } |
| 70 | + |
| 71 | + if err := o.parseExtraVariables(); err != nil { |
| 72 | + return fmt.Errorf("invalid --var flags: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +var extraVariableFlagFormat = regexp.MustCompile(`^([a-zA-Z_][a-zA-Z0-9_]*)=([a-z]+):([a-z]+):(.+)$`) |
| 79 | + |
| 80 | +func (o *Options) parseExtraVariables() error { |
| 81 | + for i, flagValue := range o.extraVariableFlags { |
| 82 | + varName, value, err := o.parseExtraVariable(flagValue) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("--var flag %d: %w", i, err) |
| 85 | + } |
| 86 | + |
| 87 | + o.ExtraVariables[varName] = value |
| 88 | + } |
| 89 | + |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +func (o *Options) parseExtraVariable(flagValue string) (string, any, error) { |
| 94 | + flagValue = strings.TrimSpace(flagValue) |
| 95 | + |
| 96 | + match := extraVariableFlagFormat.FindStringSubmatch(flagValue) |
| 97 | + if match == nil { |
| 98 | + return "", nil, errors.New("must be in the form of \"varname=encoding:source:data\"") |
| 99 | + } |
| 100 | + |
| 101 | + varName := match[1] |
| 102 | + enc := types.Encoding(match[2]) |
| 103 | + source := types.VariableSource(match[3]) |
| 104 | + data := match[4] |
| 105 | + |
| 106 | + // validate the given parameters for this variable |
| 107 | + |
| 108 | + if _, exists := o.ExtraVariables[varName]; exists { |
| 109 | + return "", nil, fmt.Errorf("variable $%s is defined multiple times", varName) |
| 110 | + } |
| 111 | + |
| 112 | + if !enc.IsValid() { |
| 113 | + return "", nil, fmt.Errorf("invalid encoding %q, must be one of %v", enc, types.AllEncodings) |
| 114 | + } |
| 115 | + |
| 116 | + if !source.IsValid() { |
| 117 | + return "", nil, fmt.Errorf("invalid source type %q, must be one of %v", source, types.AllVariableSources) |
| 118 | + } |
| 119 | + |
| 120 | + // resolve the variable source |
| 121 | + |
| 122 | + var input io.Reader |
| 123 | + |
| 124 | + switch source { |
| 125 | + case types.StringVariableSource: |
| 126 | + input = strings.NewReader(data) |
| 127 | + case types.EnvironmentVariableSource: |
| 128 | + input = strings.NewReader(os.Getenv(data)) |
| 129 | + case types.FileVariableSource: |
| 130 | + f, err := os.Open(data) |
| 131 | + if err != nil { |
| 132 | + return "", nil, fmt.Errorf("failed to open %q: %w", data, err) |
| 133 | + } |
| 134 | + defer f.Close() |
| 135 | + |
| 136 | + input = f |
| 137 | + default: |
| 138 | + // This should never happen. |
| 139 | + return "", nil, fmt.Errorf("unknown source type %q", source) |
| 140 | + } |
| 141 | + |
| 142 | + // parse the data as requested |
| 143 | + |
| 144 | + varData, err := encoding.Decode(input, enc) |
| 145 | + if err != nil { |
| 146 | + return "", nil, fmt.Errorf("failed to decode data: %w", err) |
| 147 | + } |
| 148 | + |
| 149 | + return varName, varData, nil |
| 150 | +} |
0 commit comments