Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: define jsonnet-implementation flag in a single place #1260

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading