Skip to content

Commit

Permalink
feat: Implement config generate command
Browse files Browse the repository at this point in the history
This command takes a directory path and prints
an automatically generated CircleCI config for
the codebase in that directory to stdout
  • Loading branch information
polymeris committed Jun 9, 2023
1 parent 5369baa commit 69bcc31
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
37 changes: 37 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package cmd

import (
"fmt"
"github.com/CircleCI-Public/circleci-config/generation"
"github.com/CircleCI-Public/circleci-config/labeling"
"github.com/CircleCI-Public/circleci-config/labeling/codebase"
"os"

"github.com/CircleCI-Public/circleci-cli/config"
"github.com/CircleCI-Public/circleci-cli/filetree"
Expand Down Expand Up @@ -121,10 +125,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 +159,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("#.*"))
})
})
})

0 comments on commit 69bcc31

Please sign in to comment.