From 9453ad9623a287157ffbbe59be6a8e97d7c5cbd7 Mon Sep 17 00:00:00 2001 From: Darcy Cleaver Date: Tue, 12 Mar 2024 15:59:42 -0600 Subject: [PATCH] autocomplete and vendor refactor --- go.mod | 2 +- go.sum | 4 +-- src/cmd/root.go | 54 -------------------------------- src/cmd/vendored.go | 68 +++++++++++++++++++++++++++++++++++++++++ src/config/lang/lang.go | 8 ++--- 5 files changed, 75 insertions(+), 61 deletions(-) create mode 100644 src/cmd/vendored.go diff --git a/go.mod b/go.mod index a22f1270..64cfb8eb 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21.6 require ( github.com/AlecAivazis/survey/v2 v2.3.7 github.com/alecthomas/jsonschema v0.0.0-20220216202328-9eeeec9d044b - github.com/defenseunicorns/maru-runner v0.0.1 + github.com/defenseunicorns/maru-runner v0.0.2 github.com/defenseunicorns/zarf v0.32.4 github.com/fsnotify/fsnotify v1.7.0 github.com/goccy/go-yaml v1.11.3 diff --git a/go.sum b/go.sum index e944ae07..3bbb71e5 100644 --- a/go.sum +++ b/go.sum @@ -598,8 +598,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1 github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/daviddengcn/go-colortext v1.0.0 h1:ANqDyC0ys6qCSvuEK7l3g5RaehL/Xck9EX8ATG8oKsE= github.com/daviddengcn/go-colortext v1.0.0/go.mod h1:zDqEI5NVUop5QPpVJUxE9UO10hRnmkD5G4Pmri9+m4c= -github.com/defenseunicorns/maru-runner v0.0.1 h1:0SPiaXbPKnv7bjsUW2f7rPiIMmd3YLfT9+wQe4810K8= -github.com/defenseunicorns/maru-runner v0.0.1/go.mod h1:3K+JeLpud+rb8vC+nPFaTNjhqW40++6qFKKVTBEEzQM= +github.com/defenseunicorns/maru-runner v0.0.2 h1:aZ/MCryY3Dp+u14nhGrvOXVnZI3DYY3ytjFdfHnyqY4= +github.com/defenseunicorns/maru-runner v0.0.2/go.mod h1:3K+JeLpud+rb8vC+nPFaTNjhqW40++6qFKKVTBEEzQM= github.com/defenseunicorns/zarf v0.32.4 h1:3foCaUHUtAu8YId49j3u+EVknaTB8ERaQ9J6Do+bAwc= github.com/defenseunicorns/zarf v0.32.4/go.mod h1:f4H7al7qnj5VXfkUkB/CcepVW/DA/O5tvAy8TWv9aT8= github.com/deitch/magic v0.0.0-20230404182410-1ff89d7342da h1:ZOjWpVsFZ06eIhnh4mkaceTiVoktdU67+M7KDHJ268M= diff --git a/src/cmd/root.go b/src/cmd/root.go index 93268fc7..0f980929 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -7,19 +7,12 @@ import ( "fmt" "os" "path/filepath" - "runtime/debug" - "strings" - - runnerCLI "github.com/defenseunicorns/maru-runner/src/cmd" - runnerConfig "github.com/defenseunicorns/maru-runner/src/config" "github.com/defenseunicorns/uds-cli/src/config" "github.com/defenseunicorns/uds-cli/src/config/lang" "github.com/defenseunicorns/uds-cli/src/pkg/utils" "github.com/defenseunicorns/uds-cli/src/types" - zarfCLI "github.com/defenseunicorns/zarf/src/cmd" "github.com/defenseunicorns/zarf/src/cmd/common" - zarfConfig "github.com/defenseunicorns/zarf/src/config" "github.com/defenseunicorns/zarf/src/pkg/message" "github.com/defenseunicorns/zarf/src/pkg/utils/exec" "github.com/spf13/cobra" @@ -69,53 +62,6 @@ func RootCmd() *cobra.Command { } func init() { - // grab Zarf version to make Zarf library checks happy - if buildInfo, ok := debug.ReadBuildInfo(); ok { - for _, dep := range buildInfo.Deps { - if dep.Path == "github.com/defenseunicorns/zarf" { - zarfConfig.CLIVersion = strings.Split(dep.Version, "v")[1] - } - } - } - - // vendored Zarf command - if len(os.Args) > 1 && (os.Args[1] == "zarf" || os.Args[1] == "z") { - zarfCmd := &cobra.Command{ - Use: "zarf COMMAND", - Aliases: []string{"z"}, - Run: func(_ *cobra.Command, _ []string) { - os.Args = os.Args[1:] // grab 'zarf' and onward from the CLI args - zarfCLI.Execute() - }, - DisableFlagParsing: true, - } - rootCmd.AddCommand(zarfCmd) - - // disable UDS log file for Zarf commands bc Zarf has its own log file - config.SkipLogFile = true - return - } - - // vendored run command - if len(os.Args) > 1 && (os.Args[1] == "run" || os.Args[1] == "r") { - runnerCmd := &cobra.Command{ - Use: "run", - Aliases: []string{"r"}, - Run: func(_ *cobra.Command, _ []string) { - os.Args = os.Args[1:] // grab 'run' and onward from the CLI args - runnerConfig.CmdPrefix = "uds" // use vendored Zarf inside the runner - runnerConfig.EnvPrefix = "uds" - runnerCLI.RootCmd().SetArgs(os.Args) - runnerCLI.Execute() - }, - DisableFlagParsing: true, - } - rootCmd.AddCommand(runnerCmd) - - // disable UDS log file for the runner because the runner has its own log file - config.SkipLogFile = true - return - } initViper() diff --git a/src/cmd/vendored.go b/src/cmd/vendored.go new file mode 100644 index 00000000..a5cf6101 --- /dev/null +++ b/src/cmd/vendored.go @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: 2023-Present The UDS Authors + +package cmd + +import ( + "os" + "runtime/debug" + "strings" + + runnerCLI "github.com/defenseunicorns/maru-runner/src/cmd" + runnerConfig "github.com/defenseunicorns/maru-runner/src/config" + + "github.com/defenseunicorns/uds-cli/src/config" + "github.com/defenseunicorns/uds-cli/src/config/lang" + zarfCLI "github.com/defenseunicorns/zarf/src/cmd" + zarfConfig "github.com/defenseunicorns/zarf/src/config" + "github.com/spf13/cobra" +) + +var runnerCmd = &cobra.Command{ + Use: "run", + Aliases: []string{"r"}, + Short: lang.CmdRunShort, + Run: func(_ *cobra.Command, _ []string) { + os.Args = os.Args[1:] // grab 'run' and onward from the CLI args + runnerConfig.CmdPrefix = "uds" // use vendored Zarf inside the runner + runnerConfig.EnvPrefix = "uds" + runnerCLI.RootCmd().SetArgs(os.Args) + runnerCLI.Execute() + }, + DisableFlagParsing: true, + ValidArgsFunction: func(cmd *cobra.Command, tasks []string, task string) ([]string, cobra.ShellCompDirective) { + return runnerCLI.ListAutoCompleteTasks(cmd, tasks, task) + }, +} + +var zarfCmd = &cobra.Command{ + Use: "zarf COMMAND", + Aliases: []string{"z"}, + Short: lang.CmdZarfShort, + Run: func(_ *cobra.Command, _ []string) { + os.Args = os.Args[1:] // grab 'zarf' and onward from the CLI args + zarfCLI.Execute() + }, + DisableFlagParsing: true, +} + +func init() { + // grab Zarf version to make Zarf library checks happy + if buildInfo, ok := debug.ReadBuildInfo(); ok { + for _, dep := range buildInfo.Deps { + if dep.Path == "github.com/defenseunicorns/zarf" { + zarfConfig.CLIVersion = strings.Split(dep.Version, "v")[1] + } + } + } + + // vendored Zarf command + if len(os.Args) > 1 && (os.Args[1] == "zarf" || os.Args[1] == "z") && (os.Args[1] == "run" || os.Args[1] == "r") { + // disable UDS log file for zarf and run commands bc they have their own log file + config.SkipLogFile = true + } + + initViper() + rootCmd.AddCommand(runnerCmd) + rootCmd.AddCommand(zarfCmd) +} diff --git a/src/config/lang/lang.go b/src/config/lang/lang.go index c8c10432..84a0ba3f 100644 --- a/src/config/lang/lang.go +++ b/src/config/lang/lang.go @@ -71,8 +71,8 @@ const ( CmdInternalConfigSchemaErr = "Unable to generate the uds-bundle.yaml schema" // uds run - CmdRunFlag = "Name and location of task file to run" - CmdRunSetVarFlag = "Set a runner variable from the command line (KEY=value)" - CmdRunWithVarFlag = "Set the inputs for a task from the command line (KEY=value)" - CmdRunList = "List available tasks in a task file" + CmdRunShort = "Run a task using maru-runner" + + // uds zarf + CmdZarfShort = "Run a zarf command" )