-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
151 lines (125 loc) · 3.83 KB
/
main.go
File metadata and controls
151 lines (125 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"runtime/debug"
"github.com/argoproj/argo-workflows/v3/util/template"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"
)
type InputJson struct {
Template string `json:"template"`
Values map[string]string `json:"values"`
}
var Version string
// Attempt to fill in the Version var if its not already filled in
// CI fills it using `-ldflags="-X main.Version=$(git describe --tags --always --match 'v*')"`
// doing a go install will use `debug.ReadBuildInfo`
func init_version() {
if Version != "" {
return
}
info, ok := debug.ReadBuildInfo()
if !ok {
Version = "unknown"
return
}
Version = info.Main.Version
}
func create_command() *cobra.Command {
var values map[string]string
var from_file string
var output_to_json bool
var quiet bool
rootCmd := &cobra.Command{
Use: "argo-expr",
Short: "Testing argo expressions",
Version: Version,
Long: `Testing argo expression expansions, useful for debugging work expressions without submitting a job to argo
Examples:
Directly convert a input value from a template
$ argo-expr "{{=input.parameters.name}}" --value input.parameters.name="hello world" # hello world
Using Sprig functions
$ argo-expr "{{=sprig.trim(input.parameters.name)}}" --value input.parameters.name=" hello world " # hello world
Convert input to a integer and use math
$ argo-expr "{{=asInt(input.parameters.name) + 1}}" --value input.parameters.name="1" # 2`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// Preload the argo replacements
replacement_map := map[string]string{}
var input_template string
if from_file != "" {
jsonFile, err := os.Open(from_file)
if err != nil {
panic(err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var jsonInputData InputJson
json.Unmarshal(byteValue, &jsonInputData)
if jsonInputData.Template != "" {
input_template = jsonInputData.Template
}
if jsonInputData.Values != nil {
maps.Copy(replacement_map, jsonInputData.Values)
}
}
if len(args) > 0 {
if !quiet && input_template != "" {
cmd.PrintErrf("Replacing template from:'%s' to:'%s'\n", input_template, args[0])
}
input_template = args[0]
}
// inject all of the --value parameters into the argo replacements
maps.Copy(replacement_map, values)
// Convert the template into a JSON object so it can be used by argo
template_raw := map[string]string{
"result": input_template,
}
template_json, err := json.Marshal(template_raw)
if err != nil {
panic(err)
}
// Replace the values in the template
s, err := template.Replace(string(template_json), replacement_map, false)
if err != nil {
cmd.Println(err.Error())
os.Exit(1)
}
var replaced_data map[string]interface{}
err = json.Unmarshal([]byte(s), &replaced_data)
if err != nil {
panic(err)
}
if output_to_json {
output_json := map[string]interface{}{
"template": input_template,
"values": replacement_map,
"result": replaced_data["result"],
}
output, err := json.Marshal(output_json)
if err != nil {
panic(err)
}
cmd.Println(string(output))
return
}
cmd.Println(replaced_data["result"])
},
}
rootCmd.Flags().StringToStringVarP(&values, "value", "v", map[string]string{}, "Key value pairs of inputs")
rootCmd.Flags().BoolVar(&output_to_json, "json", false, "Output as a JSON object")
rootCmd.Flags().BoolVarP(&quiet, "quiet", "q", false, "Do not print messages to stderr")
rootCmd.Flags().StringVarP(&from_file, "from-file", "f", "", "Load parameters from a file")
return rootCmd
}
func main() {
init_version()
var rootCmd = create_command()
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}