-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This includes some boilerplate for the testing setup as well
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters