Skip to content

Commit

Permalink
refactor: define jsonnet-implementation flag in a single place (#1260)
Browse files Browse the repository at this point in the history
  • Loading branch information
zerok authored Dec 3, 2024
1 parent 7ced3f9 commit d30882b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
34 changes: 34 additions & 0 deletions acceptance-tests/flag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func TestJsonnetImplementationFlag(t *testing.T) {
// The jsonnet implementation flag should be present for the following sub-commands
supportedSubCommands := []string{
"eval",
"export",
"status",
"apply",
"diff",
"delete",
"show",
// https://github.com/grafana/tanka/pull/1208
"env list",
}
tmpDir := t.TempDir()
for _, subcommand := range supportedSubCommands {
t.Run(subcommand, func(t *testing.T) {
args := []string{}
command := strings.Split(subcommand, " ")
args = append(args, command...)
args = append(args, "--help")
helpOutput := getCmdOutput(t, tmpDir, "tk", args...)
require.Contains(t, helpOutput, "jsonnet-implementation")
})
}
}
5 changes: 3 additions & 2 deletions cmd/tk/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ func envListCmd() *cli.Command {
Args: args,
}

jsonnetImplementation := cmd.Flags().String("jsonnet-implementation", "go", "Use `go` to use native go-jsonnet implementation and `binary:<path>` to delegate evaluation to a binary (with the same API as the regular `jsonnet` binary, see the BinaryImplementation docstrings for more details)")
var jsonnetImplementation string
jsonnetImplementationFlag(cmd.Flags(), &jsonnetImplementation)
useJSON := cmd.Flags().Bool("json", false, "json output")
getLabelSelector := labelSelectorFlag(cmd.Flags())

Expand All @@ -256,7 +257,7 @@ func envListCmd() *cli.Command {
}
}

envs, err := tanka.FindEnvs(path, tanka.FindOpts{JsonnetImplementation: *jsonnetImplementation, Selector: getLabelSelector(), JsonnetOpts: getJsonnetOpts()})
envs, err := tanka.FindEnvs(path, tanka.FindOpts{JsonnetImplementation: jsonnetImplementation, Selector: getLabelSelector(), JsonnetOpts: getJsonnetOpts()})
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/tk/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ type workflowFlagVars struct {
jsonnetImplementation string
}

func jsonnetImplementationFlag(fs *pflag.FlagSet, output *string) {
fs.StringVar(output, "jsonnet-implementation", "go", "Use `go` to use native go-jsonnet implementation and `binary:<path>` to delegate evaluation to a binary (with the same API as the regular `jsonnet` binary, see the BinaryImplementation docstrings for more details)")
}

func workflowFlags(fs *pflag.FlagSet) *workflowFlagVars {
v := workflowFlagVars{}
fs.StringVar(&v.name, "name", "", "string that only a single inline environment contains in its name")
fs.StringSliceVarP(&v.targets, "target", "t", nil, "Regex filter on '<kind>/<name>'. See https://tanka.dev/output-filtering")
fs.StringVar(&v.jsonnetImplementation, "jsonnet-implementation", "go", "Use `go` to use native go-jsonnet implementation and `binary:<path>` to delegate evaluation to a binary (with the same API as the regular `jsonnet` binary, see the BinaryImplementation docstrings for more details)")
jsonnetImplementationFlag(fs, &v.jsonnetImplementation)
return &v
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/tk/jsonnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ func evalCmd() *cli.Command {
Args: workflowArgs,
}

var jsonnetImplementation string
evalPattern := cmd.Flags().StringP("eval", "e", "", "Evaluate expression on output of jsonnet")
jsonnetImplementation := cmd.Flags().String("jsonnet-implementation", "go", "Use `go` to use native go-jsonnet implementation and `binary:<path>` to delegate evaluation to a binary (with the same API as the regular `jsonnet` binary, see the BinaryImplementation docstrings for more details)")
jsonnetImplementationFlag(cmd.Flags(), &jsonnetImplementation)

getJsonnetOpts := jsonnetFlags(cmd.Flags())

cmd.Run = func(_ *cli.Command, args []string) error {
jsonnetOpts := tanka.Opts{
JsonnetImplementation: *jsonnetImplementation,
JsonnetImplementation: jsonnetImplementation,
JsonnetOpts: getJsonnetOpts(),
}
if *evalPattern != "" {
Expand Down

0 comments on commit d30882b

Please sign in to comment.