Skip to content

Commit

Permalink
fix: Making the job mandatory when running circleci local execute
Browse files Browse the repository at this point in the history
  • Loading branch information
rlegan committed Jan 17, 2023
1 parent 162f425 commit 240bc9a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
10 changes: 8 additions & 2 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ import (
)

func newLocalExecuteCommand(config *settings.Config) *cobra.Command {
var args []string
buildCommand := &cobra.Command{
Use: "execute",
Use: "execute <job-name>",
Short: "Run a job in a container on the local machine",
PreRunE: func(cmd *cobra.Command, _args []string) error {
args = _args
return nil
},
RunE: func(cmd *cobra.Command, _ []string) error {
return local.Execute(cmd.Flags(), config)
return local.Execute(cmd.Flags(), config, args)
},
Args: cobra.MinimumNArgs(1),
}

local.AddFlagsForDocumentation(buildCommand.Flags())
Expand Down
10 changes: 5 additions & 5 deletions local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var picardRepo = "circleci/picard"

const DefaultConfigPath = ".circleci/config.yml"

func Execute(flags *pflag.FlagSet, cfg *settings.Config) error {
func Execute(flags *pflag.FlagSet, cfg *settings.Config, args []string) error {
var err error
var configResponse *api.ConfigResponse
cl := graphql.NewClient(cfg.HTTPClient, cfg.Host, cfg.Endpoint, cfg.Token, cfg.Debug)
Expand Down Expand Up @@ -82,7 +82,8 @@ func Execute(flags *pflag.FlagSet, cfg *settings.Config) error {
return errors.Wrap(err, "Could not find picard image")
}

arguments := generateDockerCommand(processedConfigPath, image, pwd, processedArgs...)
job := args[0]
arguments := generateDockerCommand(processedConfigPath, image, pwd, job, processedArgs...)

if cfg.Debug {
_, err = fmt.Fprintf(os.Stderr, "Starting docker with args: %s", arguments)
Expand All @@ -107,7 +108,6 @@ func Execute(flags *pflag.FlagSet, cfg *settings.Config) error {
// are public in the original command.
func AddFlagsForDocumentation(flags *pflag.FlagSet) {
flags.StringP("config", "c", DefaultConfigPath, "config file")
flags.String("job", "build", "job to be executed")
flags.Int("node-total", 1, "total number of parallel nodes")
flags.Int("index", 0, "node index of parallelism")
flags.Bool("skip-checkout", true, "use local path as-is")
Expand Down Expand Up @@ -275,15 +275,15 @@ func writeStringToTempFile(data string) (string, error) {
return f.Name(), nil
}

func generateDockerCommand(configPath, image, pwd string, arguments ...string) []string {
func generateDockerCommand(configPath, image, pwd string, job string, arguments ...string) []string {
const configPathInsideContainer = "/tmp/local_build_config.yml"
core := []string{"docker", "run", "--interactive", "--tty", "--rm",
"--volume", "/var/run/docker.sock:/var/run/docker.sock",
"--volume", fmt.Sprintf("%s:%s", configPath, configPathInsideContainer),
"--volume", fmt.Sprintf("%s:%s", pwd, pwd),
"--volume", fmt.Sprintf("%s:/root/.circleci", settings.SettingsPath()),
"--workdir", pwd,
image, "circleci", "build", "--config", configPathInsideContainer}
image, "circleci", "build", "--config", configPathInsideContainer, "--job", job}
return append(core, arguments...)
}

Expand Down
7 changes: 4 additions & 3 deletions local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var _ = Describe("build", func() {
It("can generate a command line", func() {
home, err := os.UserHomeDir()
Expect(err).NotTo(HaveOccurred())
Expect(generateDockerCommand("/config/path", "docker-image-name", "/current/directory", "extra-1", "extra-2")).To(ConsistOf(
Expect(generateDockerCommand("/config/path", "docker-image-name", "/current/directory", "build", "extra-1", "extra-2")).To(ConsistOf(
"docker",
"run",
"--interactive",
Expand All @@ -30,6 +30,7 @@ var _ = Describe("build", func() {
"--workdir", "/current/directory",
"docker-image-name", "circleci", "build",
"--config", "/tmp/local_build_config.yml",
"--job", "build",
"extra-1", "extra-2",
))
})
Expand Down Expand Up @@ -92,9 +93,9 @@ var _ = Describe("build", func() {
}),

Entry("many args", TestCase{
input: []string{"--job", "horse", "--config", "foo", "--index", "9", "d"},
input: []string{"--config", "foo", "--index", "9", "d"},
expectedConfigPath: "foo",
expectedArgs: []string{"--index", "9", "--job", "horse", "d"},
expectedArgs: []string{"--index", "9", "d"},
}),

Entry("many args, multiple envs", TestCase{
Expand Down

0 comments on commit 240bc9a

Please sign in to comment.