diff --git a/cmd/collapse.go b/cmd/collapse.go new file mode 100644 index 000000000..4b31d87de --- /dev/null +++ b/cmd/collapse.go @@ -0,0 +1,34 @@ +package cmd + +import ( + "encoding/json" + + "github.com/circleci/circleci-cli/filetree" + "github.com/spf13/cobra" +) + +var collapseCommand = &cobra.Command{ + Use: "collapse", + Short: "Collapse your CircleCI configuration to a single file", + Run: collapse, +} + +var root string + +func init() { + collapseCommand.Flags().StringVarP(&root, "root", "r", ".circleci", "path to your configuration (default is .circleci)") + // TODO: Add flag for excluding paths +} + +func collapse(cmd *cobra.Command, args []string) { + tree, err := filetree.NewTree(root) + if err != nil { + Logger.FatalOnError("An error occurred", err) + } + + data, err := json.MarshalIndent(tree, "", " ") + if err != nil { + Logger.FatalOnError("An error occurred trying to marshal", err) + } + Logger.Infoln(data) +} diff --git a/cmd/collapse_test.go b/cmd/collapse_test.go new file mode 100644 index 000000000..577cca5fa --- /dev/null +++ b/cmd/collapse_test.go @@ -0,0 +1,53 @@ +package cmd_test + +import ( + "io/ioutil" + "os" + "os/exec" + "path/filepath" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "github.com/onsi/gomega/gbytes" + "github.com/onsi/gomega/gexec" +) + +var _ = Describe("collapse", func() { + var ( + tempRoot string + command *exec.Cmd + ) + + BeforeEach(func() { + var err error + tempRoot, err = ioutil.TempDir("", "circleci-cli-test-") + Expect(err).ToNot(HaveOccurred()) + + command = exec.Command(pathCLI, "collapse", "-r", tempRoot) + }) + + AfterEach(func() { + Expect(os.RemoveAll(tempRoot)).To(Succeed()) + }) + + Describe("with a single file under root", func() { + BeforeEach(func() { + var err error + _, err = os.OpenFile( + filepath.Join(tempRoot, "foo"), + os.O_RDWR|os.O_CREATE, + 0600, + ) + Expect(err).ToNot(HaveOccurred()) + }) + + It("Prints a JSON tree of the nested file-structure", func() { + session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter) + Expect(err).ShouldNot(HaveOccurred()) + Eventually(session.Err.Contents()).Should(BeEmpty()) + + Eventually(session.Out).Should(gbytes.Say("{}")) + Eventually(session).Should(gexec.Exit(0)) + }) + }) +}) diff --git a/cmd/root.go b/cmd/root.go index b67c0ce5a..6c2f3e45b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,6 +33,7 @@ var rootCmd = &cobra.Command{ func addCommands() { rootCmd.AddCommand(diagnosticCmd) rootCmd.AddCommand(queryCmd) + rootCmd.AddCommand(collapseCommand) rootCmd.AddCommand(configureCommand) }