Skip to content

Commit

Permalink
Add skeleton for collapse command
Browse files Browse the repository at this point in the history
This includes some boilerplate for the testing setup as well
  • Loading branch information
Zachary Scott authored and dcarley committed Jun 15, 2018
1 parent 05cb0e6 commit c802e9f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cmd/collapse.go
Original file line number Diff line number Diff line change
@@ -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)
}
53 changes: 53 additions & 0 deletions cmd/collapse_test.go
Original file line number Diff line number Diff line change
@@ -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))
})
})
})
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var rootCmd = &cobra.Command{
func addCommands() {
rootCmd.AddCommand(diagnosticCmd)
rootCmd.AddCommand(queryCmd)
rootCmd.AddCommand(collapseCommand)
rootCmd.AddCommand(configureCommand)
}

Expand Down

0 comments on commit c802e9f

Please sign in to comment.