@@ -59,73 +59,47 @@ NOTE: Values are interpreted as JSON literals when valid. Otherwise they are sen
5959
6060 instanceID := cmd .Args ().First ()
6161 client := cfg .apiClient (clicompat .APIFlagsFromCmd (cmd ), cmd .Writer )
62- return runABCVariablesSet (ctx , client , instanceID , cmd .Args ().Tail (), abcVariableArgs (cmd .StringSlice ("var" )), cmd .Writer , cmd .Bool ("get-curl" ))
62+ abcVariables , err := parseABCVariables (cmd .Args ().Tail (), cmd .StringSlice ("var" ))
63+ if err != nil {
64+ return err
65+ }
66+ return runABCVariablesSet (ctx , client , instanceID , abcVariables , cmd .Writer , cmd .Bool ("get-curl" ))
6367 },
6468})
6569
66- type abcVariableArgs []string
67-
68- func (a * abcVariableArgs ) String () string {
69- return strings .Join (* a , "," )
70- }
71-
72- func (a * abcVariableArgs ) Set (value string ) error {
73- * a = append (* a , value )
74- return nil
75- }
76-
77- type abcVariable struct {
78- Key string
79- Value string
80- }
81-
82- func parseABCVariables (positional []string , flagged abcVariableArgs ) ([]abcVariable , error ) {
83- rawVariables := append ([]string {}, positional ... )
84- rawVariables = append (rawVariables , flagged ... )
70+ func parseABCVariables (positional []string , flagged []string ) (map [string ]string , error ) {
71+ rawVariables := append (positional , flagged ... )
8572 if len (rawVariables ) == 0 {
8673 return nil , cmderrors .Usage ("must provide at least one variable assignment" )
8774 }
8875
89- variables := make ([]abcVariable , 0 , len (rawVariables ))
90- for _ , rawVariable := range rawVariables {
91- variable , err := parseABCVariable (rawVariable )
76+ variables := make (map [string ]string , len (rawVariables ))
77+ for _ , v := range rawVariables {
78+ name , rawValue , ok := strings .Cut (v , "=" )
79+ if ! ok || name == "" {
80+ return nil , cmderrors .Usagef ("invalid variable assignment %q: must be in <name>=<value> form" , v )
81+ }
82+
83+ value , remove , err := marshalABCVariableValue (rawValue )
9284 if err != nil {
9385 return nil , err
9486 }
95- variables = append (variables , variable )
96- }
97-
98- return variables , nil
99- }
87+ if remove {
88+ return nil , cmderrors .Usagef ("invalid variable assignment %q: use 'src abc variables delete <workflow-instance-id> %s' to remove a variable" , rawValue , name )
89+ }
10090
101- func parseABCVariable (raw string ) (abcVariable , error ) {
102- name , rawValue , ok := strings .Cut (raw , "=" )
103- if ! ok || name == "" {
104- return abcVariable {}, cmderrors .Usagef ("invalid variable assignment %q: must be in <name>=<value> form" , raw )
91+ variables [name ] = value
10592 }
10693
107- value , remove , err := marshalABCVariableValue (rawValue )
108- if err != nil {
109- return abcVariable {}, err
110- }
111- if remove {
112- return abcVariable {}, cmderrors .Usagef ("invalid variable assignment %q: use 'src abc variables delete <workflow-instance-id> %s' to remove a variable" , raw , name )
113- }
114-
115- return abcVariable {Key : name , Value : value }, nil
94+ return variables , nil
11695}
11796
118- func runABCVariablesSet (ctx context.Context , client api.Client , instanceID string , positional []string , flagged abcVariableArgs , output io.Writer , getCurl bool ) error {
119- variables , err := parseABCVariables (positional , flagged )
120- if err != nil {
121- return err
122- }
123-
97+ func runABCVariablesSet (ctx context.Context , client api.Client , instanceID string , variables map [string ]string , output io.Writer , getCurl bool ) error {
12498 graphqlVariables := make ([]map [string ]string , 0 , len (variables ))
125- for _ , variable := range variables {
99+ for k , v := range variables {
126100 graphqlVariables = append (graphqlVariables , map [string ]string {
127- "key" : variable . Key ,
128- "value" : variable . Value ,
101+ "key" : k ,
102+ "value" : v ,
129103 })
130104 }
131105
@@ -137,12 +111,7 @@ func runABCVariablesSet(ctx context.Context, client api.Client, instanceID strin
137111 return nil
138112 }
139113
140- if len (variables ) == 1 {
141- _ , err = fmt .Fprintf (output , "Set variable %q on workflow instance %q.\n " , variables [0 ].Key , instanceID )
142- return err
143- }
144-
145- _ , err = fmt .Fprintf (output , "Updated %d variables on workflow instance %q.\n " , len (variables ), instanceID )
114+ _ , err := fmt .Fprintf (output , "Updated %d variables on workflow instance %q.\n " , len (variables ), instanceID )
146115 return err
147116}
148117
0 commit comments