Skip to content

Commit

Permalink
Merge pull request #948 from skaasten/config-generate-command
Browse files Browse the repository at this point in the history
Add circleci config generate command
  • Loading branch information
JulesFaucherre authored Jun 12, 2023
2 parents a8a87f1 + e29c408 commit 26ed5cd
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ jobs:

lint:
docker:
- image: golangci/golangci-lint:v1.46-alpine
- image: golangci/golangci-lint:v1.51.0-alpine
resource_class: large
steps:
- checkout
Expand Down
38 changes: 38 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package cmd

import (
"fmt"
"os"

"github.com/CircleCI-Public/circleci-config/generation"
"github.com/CircleCI-Public/circleci-config/labeling"
"github.com/CircleCI-Public/circleci-config/labeling/codebase"

"github.com/CircleCI-Public/circleci-cli/config"
"github.com/CircleCI-Public/circleci-cli/filetree"
Expand Down Expand Up @@ -121,10 +126,19 @@ func newConfigCommand(globalConfig *settings.Config) *cobra.Command {
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")

generateCommand := &cobra.Command{
Use: "generate <path>",
Short: "Generate a config by analyzing your repository contents",
RunE: func(cmd *cobra.Command, args []string) error {
return generateConfig(args)
},
}

configCmd.AddCommand(packCommand)
configCmd.AddCommand(validateCommand)
configCmd.AddCommand(processCommand)
configCmd.AddCommand(migrateCommand)
configCmd.AddCommand(generateCommand)

return configCmd
}
Expand All @@ -146,3 +160,27 @@ func packConfig(args []string) error {
func migrateConfig(args []string) error {
return proxy.Exec([]string{"config", "migrate"}, args)
}

func generateConfig(args []string) error {
path := "."
if len(args) == 1 {
path = args[0]
}

stat, err := os.Stat(path)

if os.IsNotExist(err) || !stat.IsDir() {
return fmt.Errorf("%s is not a directory", path)
}
if err != nil {
return fmt.Errorf("error reading from %s: %v", path, err)
}

cb := codebase.LocalCodebase{BasePath: path}
labels := labeling.ApplyAllRules(cb)
generatedConfig := generation.GenerateConfig(labels)

fmt.Print(generatedConfig.String())

return nil
}
18 changes: 18 additions & 0 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,22 @@ var _ = Describe("Config", func() {
})
})
})
Describe("generate", func() {
It("works without a path", func() {
command := exec.Command(pathCLI, "config", "generate")
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
session.Wait()
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err.Contents()).Should(BeEmpty())
Eventually(session.Out.Contents()).Should(MatchRegexp("#.*"))
})
It("works with a path", func() {
command := exec.Command(pathCLI, "config", "generate", "..")
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
session.Wait()
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err.Contents()).Should(BeEmpty())
Eventually(session.Out.Contents()).Should(MatchRegexp("#.*"))
})
})
})
36 changes: 22 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/blang/semver v3.5.1+incompatible
github.com/briandowns/spinner v1.18.1
github.com/fatih/color v1.13.0
github.com/go-git/go-git/v5 v5.1.0
github.com/go-git/go-git/v5 v5.7.0
github.com/google/go-github v15.0.0+incompatible // indirect
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 // indirect
github.com/google/uuid v1.3.0
Expand All @@ -32,62 +32,70 @@ require (
)

require (
github.com/CircleCI-Public/circleci-config v0.0.0-20230609135034-182164ce950a
github.com/a8m/envsubst v1.4.2
github.com/charmbracelet/lipgloss v0.5.0
github.com/erikgeiser/promptkit v0.7.0
github.com/hexops/gotextdiff v1.0.3
github.com/stretchr/testify v1.8.2
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
golang.org/x/term v0.8.0
)

require (
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/OneOfOne/xxhash v1.2.8 // indirect
github.com/a8m/envsubst v1.4.2 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230518184743-7afd39499903 // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/alessio/shellescape v1.4.1 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/charmbracelet/bubbles v0.11.0 // indirect
github.com/charmbracelet/bubbletea v0.21.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.0.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.4.1 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hinshun/vt10x v0.0.0-20220301184237-5011da428d02 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 // indirect
github.com/muesli/cancelreader v0.2.0 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.12.0 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/open-policy-agent/opa v0.50.2 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/skeema/knownhosts v1.1.1 // indirect
github.com/tchap/go-patricia/v2 v2.3.1 // indirect
github.com/xanzy/ssh-agent v0.2.1 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/yashtewari/glob-intersection v0.1.0 // indirect
golang.org/x/crypto v0.3.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
Loading

0 comments on commit 26ed5cd

Please sign in to comment.