Skip to content

Commit

Permalink
Merge pull request #369 from CircleCI-Public/build-config-2-1-two
Browse files Browse the repository at this point in the history
CIRCLE-25004 Support Config Version 2.1 in Local Builds
  • Loading branch information
marcomorain authored Mar 9, 2020
2 parents 1a556c8 + ea94690 commit e5767b0
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 252 deletions.
50 changes: 3 additions & 47 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,17 @@ import (
"github.com/spf13/cobra"
)

// These options are purely here to retain a mock of the structure of the flags used by `build`.
// They don't reflect the entire structure or available flags, only those which are public in the original command.
type proxyBuildArgs struct {
configFilename string
taskInfo struct {
NodeTotal int
NodeIndex int
Job string
SkipCheckout bool
Volumes []string
Revision string
Branch string
RepositoryURI string
}
checkoutKey string
envVarArgs []string
}

func newLocalExecuteCommand(config *settings.Config) *cobra.Command {
opts := local.BuildOptions{
Cfg: config,
}

buildCommand := &cobra.Command{
Use: "execute",
Short: "Run a job in a container on the local machine",
PreRun: func(cmd *cobra.Command, args []string) {
opts.Args = args
opts.Help = cmd.Help
},
RunE: func(_ *cobra.Command, _ []string) error {
return local.Execute(opts)
RunE: func(cmd *cobra.Command, _ []string) error {
return local.Execute(cmd.Flags(), config)
},
DisableFlagParsing: true,
}

// Used as a convenience work-around when DisableFlagParsing is enabled
// Allows help command to access the combined rollup of flags
args := proxyBuildArgs{}
flags := buildCommand.Flags()

flags.StringVarP(&args.configFilename, "config", "c", local.DefaultConfigPath, "config file")
flags.StringVar(&args.taskInfo.Job, "job", "build", "job to be executed")
flags.IntVar(&args.taskInfo.NodeTotal, "node-total", 1, "total number of parallel nodes")
flags.IntVar(&args.taskInfo.NodeIndex, "index", 0, "node index of parallelism")

flags.BoolVar(&args.taskInfo.SkipCheckout, "skip-checkout", true, "use local path as-is")
flags.StringSliceVarP(&args.taskInfo.Volumes, "volume", "v", nil, "Volume bind-mounting")

flags.StringVar(&args.checkoutKey, "checkout-key", "~/.ssh/id_rsa", "Git Checkout key")
flags.StringVar(&args.taskInfo.Revision, "revision", "", "Git Revision")
flags.StringVar(&args.taskInfo.Branch, "branch", "", "Git branch")
flags.StringVar(&args.taskInfo.RepositoryURI, "repo-url", "", "Git Url")

flags.StringArrayVarP(&args.envVarArgs, "env", "e", nil, "Set environment variables, e.g. `-e VAR=VAL`")
local.AddFlagsForDocumentation(buildCommand.Flags())

return buildCommand
}
Expand Down
64 changes: 0 additions & 64 deletions cmd/build_test.go
Original file line number Diff line number Diff line change
@@ -1,72 +1,8 @@
package cmd

import (
"github.com/CircleCI-Public/circleci-cli/local"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("build", func() {

Describe("local execute", func() {
It("provides a help documentation when provided with a --help flag", func() {
called := false
mockHelp := func() error {
called = true
return nil
}
mockOptions := local.BuildOptions{
Args: []string{"--help"},
Help: mockHelp,
}
err := local.Execute(mockOptions)
Expect(err).ToNot(HaveOccurred())
Expect(called).To(BeTrue())
})

It("provides a help documentation when provided with a --help flag mixed with other flags", func() {
called := false
mockHelp := func() error {
called = true
return nil
}
mockOptions := local.BuildOptions{
Args: []string{"--skip-checkout", "--help"},
Help: mockHelp,
}
err := local.Execute(mockOptions)
Expect(err).ToNot(HaveOccurred())
Expect(called).To(BeTrue())
})

It("provides a help documentation when provided with a -h flag", func() {
called := false
mockHelp := func() error {
called = true
return nil
}
mockOptions := local.BuildOptions{
Args: []string{"-h"},
Help: mockHelp,
}
err := local.Execute(mockOptions)
Expect(err).ToNot(HaveOccurred())
Expect(called).To(BeTrue())
})

It("provides a help documentation when provided with a -h flag mixed with other flags", func() {
called := false
mockHelp := func() error {
called = true
return nil
}
mockOptions := local.BuildOptions{
Args: []string{"--skip-checkout", "-h"},
Help: mockHelp,
}
err := local.Execute(mockOptions)
Expect(err).ToNot(HaveOccurred())
Expect(called).To(BeTrue())
})
})
})
Loading

0 comments on commit e5767b0

Please sign in to comment.