From 86289a14e0564098bac111b0a14cfcb89843fbf7 Mon Sep 17 00:00:00 2001 From: Zachary Scott Date: Fri, 10 Aug 2018 17:07:21 +0900 Subject: [PATCH] Proxy in-build commands to old CLI --- cmd/config.go | 15 +++++++++++++++ cmd/root.go | 1 + cmd/root_test.go | 2 +- cmd/step.go | 29 +++++++++++++++++++++++++++++ cmd/tests.go | 19 +++++++++++++++++++ proxy/proxy.go | 19 +++++++++++++++++++ 6 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 cmd/step.go create mode 100644 proxy/proxy.go diff --git a/cmd/config.go b/cmd/config.go index 96d2b6ad6..ac9fe3517 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -5,6 +5,7 @@ import ( "github.com/CircleCI-Public/circleci-cli/api" "github.com/CircleCI-Public/circleci-cli/filetree" + "github.com/CircleCI-Public/circleci-cli/proxy" "github.com/pkg/errors" "github.com/spf13/cobra" yaml "gopkg.in/yaml.v2" @@ -49,9 +50,19 @@ func newConfigCommand() *cobra.Command { Args: cobra.ExactArgs(1), } + migrateCommand := &cobra.Command{ + Use: "migrate", + Short: "migrate configuration file to new format", + RunE: migrateConfig, + Hidden: true, + } + migrateCommand.PersistentFlags().StringP("config", "c", ".circleci/config.yml", "path to config file (default \".circleci/config.yml\")") + migrateCommand.PersistentFlags().BoolP("in-place", "i", false, "whether to update file in place. If false, emits to stdout") + configCmd.AddCommand(collapseCommand) configCmd.AddCommand(validateCommand) configCmd.AddCommand(expandCommand) + configCmd.AddCommand(migrateCommand) return configCmd } @@ -113,3 +124,7 @@ func collapseConfig(cmd *cobra.Command, args []string) error { Logger.Infof("%s\n", string(y)) return nil } + +func migrateConfig(cmd *cobra.Command, args []string) error { + return proxy.Exec("config migrate", args) +} diff --git a/cmd/root.go b/cmd/root.go index a1dbdccda..3628813f6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -85,6 +85,7 @@ func MakeCommands() *cobra.Command { rootCmd.AddCommand(newUpdateCommand()) rootCmd.AddCommand(newNamespaceCommand()) rootCmd.AddCommand(newUsageCommand()) + rootCmd.AddCommand(newStepCommand()) rootCmd.PersistentFlags().Bool("verbose", false, "Enable verbose logging.") rootCmd.PersistentFlags().String("endpoint", defaultEndpoint, "the endpoint of your CircleCI GraphQL API") rootCmd.PersistentFlags().String("token", "", "your token for using CircleCI") diff --git a/cmd/root_test.go b/cmd/root_test.go index d295a1918..64f3e609a 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -16,7 +16,7 @@ var _ = Describe("Root", func() { It("can create commands", func() { commands := cmd.MakeCommands() - Expect(len(commands.Commands())).To(Equal(11)) + Expect(len(commands.Commands())).To(Equal(12)) }) }) diff --git a/cmd/step.go b/cmd/step.go new file mode 100644 index 000000000..ecbcf838e --- /dev/null +++ b/cmd/step.go @@ -0,0 +1,29 @@ +package cmd + +import ( + "github.com/CircleCI-Public/circleci-cli/proxy" + "github.com/spf13/cobra" +) + +func newStepCommand() *cobra.Command { + stepCmd := &cobra.Command{ + Use: "step", + Short: "Execute steps", + Hidden: true, + } + + haltCmd := &cobra.Command{ + Use: "halt", + Short: "halt current task and treat as a successful", + RunE: haltRunE, + Hidden: true, + } + + stepCmd.AddCommand(haltCmd) + + return stepCmd +} + +func haltRunE(cmd *cobra.Command, args []string) error { + return proxy.Exec("step halt", args) +} diff --git a/cmd/tests.go b/cmd/tests.go index 6f5ab6d9b..b8de03c17 100644 --- a/cmd/tests.go +++ b/cmd/tests.go @@ -4,6 +4,7 @@ import ( "fmt" "os" + "github.com/CircleCI-Public/circleci-cli/proxy" "github.com/bmatcuk/doublestar" "github.com/spf13/cobra" ) @@ -22,7 +23,21 @@ func newTestsCommand() *cobra.Command { Hidden: true, } + splitCmd := &cobra.Command{ + Use: "split", + Short: "return a split batch of provided files", + RunE: splitRunE, + Hidden: true, + } + splitCmd.Flags().Uint("index", 0, "index of node.") + splitCmd.Flags().Uint("total", 1, "number of nodes.") + splitCmd.Flags().String("split-by", "name", `how to weight the split, allowed values are "name", "filesize", and "timings".`) + splitCmd.Flags().String("timings-type", "filename", `lookup historical timing data by: "classname", "filename", or "testname".`) + splitCmd.Flags().Bool("show-counts", false, `print test file or test class counts to stderr (default false).`) + splitCmd.Flags().String("timings-file", "", "JSON file containing historical timing data.") + testsCmd.AddCommand(globCmd) + testsCmd.AddCommand(splitCmd) return testsCmd } @@ -54,3 +69,7 @@ func globRun(cmd *cobra.Command, args []string) { fmt.Println(filename) } } + +func splitRunE(cmd *cobra.Command, args []string) error { + return proxy.Exec("tests split", args) +} diff --git a/proxy/proxy.go b/proxy/proxy.go new file mode 100644 index 000000000..303129bb9 --- /dev/null +++ b/proxy/proxy.go @@ -0,0 +1,19 @@ +package proxy + +import ( + "os" + "os/exec" + "syscall" + + "github.com/pkg/errors" +) + +func Exec(command string, args []string) error { + agent, err := exec.LookPath("picard") + if err != nil { + return errors.Wrap(err, "Could not find `picard` executable on $PATH; please ensure that build-agent is installed") + } + + err = syscall.Exec(agent, args, os.Environ()) // #nosec + return errors.Wrap(err, "failed to execute picard command") +}