-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor to support variables, cli params
- Loading branch information
Neil Hemming
committed
Jul 20, 2021
1 parent
84015d4
commit d37b6c0
Showing
48 changed files
with
3,970 additions
and
989 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
"ironment", | ||
"logcli", | ||
"nehemming", | ||
"nolint" | ||
"nolint", | ||
"testdata" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,58 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/nehemming/cirocket/pkg/rocket" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func getCliParams(cmd *cobra.Command) ([]rocket.Param, error) { | ||
valueParams, err := cmd.Flags().GetStringArray(flagParams) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return parseParams(valueParams) | ||
} | ||
|
||
func parseParams(valueParams []string) ([]rocket.Param, error) { | ||
params := make([]rocket.Param, len(valueParams)) | ||
|
||
for i, nv := range valueParams { | ||
slice := strings.SplitN(nv, "=", 2) | ||
|
||
if len(slice) != 2 { | ||
return nil, fmt.Errorf("param[%d] %s is not formed as name=value", i, nv) | ||
} | ||
|
||
params[i] = rocket.Param{Name: slice[0], Value: slice[1]} | ||
} | ||
|
||
return params, nil | ||
} | ||
|
||
func (cli *cli) runFireCmd(cmd *cobra.Command, args []string) error { | ||
// Check that the init process found a config file | ||
if cli.initError != nil { | ||
return cli.initError | ||
} | ||
|
||
// Handle params | ||
params, err := getCliParams(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Attempt to launch mission | ||
return rocket.Default().LaunchMission(cli.ctx, viper.ConfigFileUsed(), viper.AllSettings(), args...) | ||
return rocket.Default(). | ||
LaunchMissionWithParams(cli.ctx, viper.ConfigFileUsed(), | ||
viper.AllSettings(), params, args...) | ||
} | ||
|
||
const flagParams = "params" | ||
|
||
func (cli *cli) bindLaunchFlagsAndConfig(cmd *cobra.Command) { | ||
cmd.Flags().StringArray(flagParams, nil, "supply parameter values to the mission, multiple params flags can be provided") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package cmd | ||
|
||
import "testing" | ||
|
||
func TestParseParamsEmpty(t *testing.T) { | ||
var list []string | ||
|
||
r, err := parseParams(list) | ||
if err != nil || len(r) > 0 { | ||
t.Error("unexpected", err, len(r)) | ||
} | ||
} | ||
|
||
func TestParseParamsSingle(t *testing.T) { | ||
list := []string{"abc=123"} | ||
|
||
r, err := parseParams(list) | ||
if err != nil || len(r) != 1 { | ||
t.Error("unexpected", err, len(r)) | ||
return | ||
} | ||
|
||
if r[0].Name != "abc" { | ||
t.Error("unexpected name", r[0].Name) | ||
} | ||
if r[0].Value != "123" { | ||
t.Error("unexpected value", r[0].Name, r[0].Value) | ||
} | ||
} | ||
|
||
func TestParseParamsMultiple(t *testing.T) { | ||
list := []string{"abc=123", "def=456,7=8"} | ||
|
||
r, err := parseParams(list) | ||
if err != nil || len(r) != 2 { | ||
t.Error("unexpected", err, len(r)) | ||
return | ||
} | ||
|
||
if r[0].Name != "abc" { | ||
t.Error("unexpected name", r[0].Name) | ||
} | ||
if r[0].Value != "123" { | ||
t.Error("unexpected value", r[0].Name, r[0].Value) | ||
} | ||
|
||
if r[1].Name != "def" { | ||
t.Error("unexpected name", r[1].Name) | ||
} | ||
if r[1].Value != "456,7=8" { | ||
t.Error("unexpected value", r[1].Name, r[0].Value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.