Skip to content

Commit

Permalink
Move collapse under config command
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Scott committed Jul 31, 2018
1 parent e58a277 commit 3f46a51
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 130 deletions.
36 changes: 0 additions & 36 deletions cmd/collapse.go

This file was deleted.

92 changes: 0 additions & 92 deletions cmd/collapse_test.go

This file was deleted.

27 changes: 27 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,29 @@ import (
"context"

"github.com/CircleCI-Public/circleci-cli/api"
"github.com/CircleCI-Public/circleci-cli/filetree"
"github.com/pkg/errors"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
)

const defaultConfigPath = ".circleci/config.yml"

var root string

func newConfigCommand() *cobra.Command {
configCmd := &cobra.Command{
Use: "config",
Short: "Operate on build config files",
}

collapseCommand := &cobra.Command{
Use: "collapse",
Short: "Collapse your CircleCI configuration to a single file",
RunE: collapseConfig,
}
collapseCommand.Flags().StringVarP(&root, "root", "r", ".", "path to your configuration (default is current path)")

validateCommand := &cobra.Command{
Use: "validate [config.yml]",
Aliases: []string{"check"},
Expand All @@ -30,6 +42,7 @@ func newConfigCommand() *cobra.Command {
Args: cobra.MaximumNArgs(1),
}

configCmd.AddCommand(collapseCommand)
configCmd.AddCommand(validateCommand)
configCmd.AddCommand(expandCommand)

Expand Down Expand Up @@ -76,3 +89,17 @@ func expandConfig(cmd *cobra.Command, args []string) error {
Logger.Info(response.OutputYaml)
return nil
}

func collapseConfig(cmd *cobra.Command, args []string) error {
tree, err := filetree.NewTree(root)
if err != nil {
return errors.Wrap(err, "An error occurred trying to build the tree")
}

y, err := yaml.Marshal(&tree)
if err != nil {
return errors.Wrap(err, "Failed trying to marshal the tree to YAML ")
}
Logger.Infof("%s\n", string(y))
return nil
}
82 changes: 82 additions & 0 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd_test

import (
"io/ioutil"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -206,4 +207,85 @@ var _ = Describe("Config", func() {
})
})
})

Describe("collapse", func() {
var (
command *exec.Cmd
results []byte
)

Describe("a .circleci folder with config.yml and local orbs folder containing the hugo orb", func() {
BeforeEach(func() {
var err error
command = exec.Command(pathCLI, "config", "collapse", "-r", "testdata/hugo-collapse/.circleci")
results, err = ioutil.ReadFile("testdata/hugo-collapse/result.yml")
Expect(err).ShouldNot(HaveOccurred())
})

It("collapse all YAML contents as expected", func() {
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
session.Wait()
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err.Contents()).Should(BeEmpty())
Eventually(session.Out.Contents()).Should(MatchYAML(results))
Eventually(session).Should(gexec.Exit(0))
})
})

Describe("local orbs folder with mixed inline and local commands, jobs, etc", func() {
BeforeEach(func() {
var err error
var path string = "nested-orbs-and-local-commands-etc"
command = exec.Command(pathCLI, "config", "collapse", "-r", filepath.Join("testdata", path, "test"))
results, err = ioutil.ReadFile(filepath.Join("testdata", path, "result.yml"))
Expect(err).ShouldNot(HaveOccurred())
})

It("collapse all YAML contents as expected", func() {
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
session.Wait()
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err.Contents()).Should(BeEmpty())
Eventually(session.Out.Contents()).Should(MatchYAML(results))
Eventually(session).Should(gexec.Exit(0))
})
})

Describe("an orb containing local executors and commands in folder", func() {
BeforeEach(func() {
var err error
command = exec.Command(pathCLI, "config", "collapse", "-r", "testdata/myorb/test")
results, err = ioutil.ReadFile("testdata/myorb/result.yml")
Expect(err).ShouldNot(HaveOccurred())
})

It("collapse all YAML contents as expected", func() {
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
session.Wait()
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err.Contents()).Should(BeEmpty())
Eventually(session.Out.Contents()).Should(MatchYAML(results))
Eventually(session).Should(gexec.Exit(0))
})
})

Describe("with a large nested config including rails orb", func() {
BeforeEach(func() {
var err error
var path string = "test-with-large-nested-rails-orb"
command = exec.Command(pathCLI, "config", "collapse", "-r", filepath.Join("testdata", path, "test"))
results, err = ioutil.ReadFile(filepath.Join("testdata", path, "result.yml"))
Expect(err).ShouldNot(HaveOccurred())
})

It("collapse all YAML contents as expected", func() {
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
session.Wait()
Expect(err).ShouldNot(HaveOccurred())
Eventually(session.Err.Contents()).Should(BeEmpty())
Eventually(session.Out.Contents()).Should(MatchYAML(results))
Eventually(session).Should(gexec.Exit(0))
})
})
})
})
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func MakeCommands() *cobra.Command {

rootCmd.AddCommand(newDiagnosticCommand())
rootCmd.AddCommand(newQueryCommand())
rootCmd.AddCommand(newCollapseCommand())
rootCmd.AddCommand(newConfigureCommand())
rootCmd.AddCommand(newConfigCommand())
rootCmd.AddCommand(newOrbCommand())
Expand Down
2 changes: 1 addition & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var _ = Describe("Root", func() {

It("can create commands", func() {
commands := cmd.MakeCommands()
Expect(len(commands.Commands())).To(Equal(9))
Expect(len(commands.Commands())).To(Equal(8))
})

})
Expand Down

0 comments on commit 3f46a51

Please sign in to comment.