Skip to content

Commit

Permalink
Merge pull request #55 from CircleCI-Public/proxy
Browse files Browse the repository at this point in the history
Proxy in-build commands to old CLI
  • Loading branch information
Zachary Scott authored Aug 14, 2018
2 parents a78fed5 + fa9ce56 commit 772e389
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 2 deletions.
19 changes: 18 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -36,7 +37,7 @@ func newConfigCommand() *cobra.Command {
RunE: validateConfig,
Args: cobra.MaximumNArgs(1),
}
validateCommand.PersistentFlags().StringVarP(&configPath, "config", "c", ".circleci/config.yml", "path to config file (default \".circleci/config.yml\")")
validateCommand.PersistentFlags().StringVarP(&configPath, "config", "c", ".circleci/config.yml", "path to config file")
err := validateCommand.PersistentFlags().MarkHidden("config")
if err != nil {
panic(err)
Expand All @@ -49,9 +50,21 @@ func newConfigCommand() *cobra.Command {
Args: cobra.ExactArgs(1),
}

migrateCommand := &cobra.Command{
Use: "migrate",
Short: "migrate a pre-release 2.0 config to the official release version",
RunE: migrateConfig,
Hidden: true,
DisableFlagParsing: true,
}
// These flags are for documentation and not actually parsed
migrateCommand.PersistentFlags().StringP("config", "c", ".circleci/config.yml", "path to config file")
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
}
Expand Down Expand Up @@ -113,3 +126,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([]string{"config", "migrate"}, args)
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})

})
Expand Down
31 changes: 31 additions & 0 deletions cmd/step.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
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,
DisableFlagParsing: true,
}

haltCmd := &cobra.Command{
Use: "halt",
Short: "Halt the current job and treat it as successful",
RunE: haltRunE,
Hidden: true,
DisableFlagParsing: true,
}

stepCmd.AddCommand(haltCmd)

return stepCmd
}

func haltRunE(cmd *cobra.Command, args []string) error {
return proxy.Exec([]string{"step", "halt"}, args)
}
19 changes: 19 additions & 0 deletions cmd/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/CircleCI-Public/circleci-cli/proxy"
"github.com/bmatcuk/doublestar"
"github.com/spf13/cobra"
)
Expand All @@ -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
}
Expand Down Expand Up @@ -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([]string{"tests", "split"}, args)
}
23 changes: 23 additions & 0 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package proxy

import (
"os"
"os/exec"
"syscall"

"github.com/pkg/errors"
)

// Exec will invoke the given command and proxy any arguments for backwards compatibility.
func Exec(command []string, args []string) error {
agent, err := exec.LookPath("circleci-agent")
if err != nil {
return errors.Wrap(err, "Please ensure that circleci-agent is installed, expected this to be called inside a job")
}

arguments := append([]string{agent}, command...)
arguments = append(arguments, args...)

err = syscall.Exec(agent, arguments, os.Environ()) // #nosec
return errors.Wrapf(err, "failed to proxy command %s, expected this to be called inside a job", command)
}

0 comments on commit 772e389

Please sign in to comment.