Skip to content

Commit

Permalink
Collapse command should print the tree in YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Scott committed Jun 19, 2018
1 parent 43b5e1e commit 980eb71
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
9 changes: 7 additions & 2 deletions cmd/collapse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/circleci/circleci-cli/filetree"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
)

var collapseCommand = &cobra.Command{
Expand All @@ -21,8 +22,12 @@ func init() {
func collapse(cmd *cobra.Command, args []string) {
tree, err := filetree.NewTree(root)
if err != nil {
Logger.FatalOnError("An error occurred", err)
Logger.FatalOnError("An error occurred trying to build the tree", err)
}

Logger.Prettyify(tree)
y, err := yaml.Marshal(&tree)
if err != nil {
Logger.FatalOnError("Failed trying to marshal the tree to YAML ", err)
}
Logger.Infof("%s\n", string(y))
}
12 changes: 10 additions & 2 deletions filetree/filetree.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package filetree

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"

"github.com/mitchellh/mapstructure"
yaml "gopkg.in/yaml.v2"
Expand Down Expand Up @@ -107,11 +107,19 @@ func NewTree(root string) (*Node, error) {
return err
}

// Skip any dotfiles automatically
re := regexp.MustCompile(`^\..+`)
if re.MatchString(info.Name()) {
// Turn off logging to stdout in this package
// fmt.Printf("Skipping: %+v\n", info.Name())
return filepath.SkipDir
}

// check if file is in exclude slice and skip it
// need to pass this in as an array
exclude := []string{"path/to/skip"}
if excluded(exclude, path) {
fmt.Printf("skipping: %+v \n", info.Name())
//fmt.Printf("skipping: %+v \n", info.Name())
return filepath.SkipDir
}

Expand Down
29 changes: 29 additions & 0 deletions filetree/filetree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,35 @@ sub_dir:
foo:
bar:
baz
`))
})
})

Describe("MarshalYAML", func() {
var subDir, subDirFile, emptyDir string

BeforeEach(func() {
subDir = filepath.Join(tempRoot, "sub_dir")
subDirFile = filepath.Join(tempRoot, "sub_dir", "sub_dir_file.yml")
emptyDir = filepath.Join(tempRoot, "empty_dir")

Expect(os.Mkdir(subDir, 0700)).To(Succeed())
Expect(ioutil.WriteFile(subDirFile, []byte("foo:\n bar:\n baz"), 0600)).To(Succeed())
Expect(os.Mkdir(emptyDir, 0700)).To(Succeed())

})
It("renders to YAML", func() {
tree, err := filetree.NewTree(tempRoot)
Expect(err).ToNot(HaveOccurred())

out, err := yaml.Marshal(tree)
Expect(err).ToNot(HaveOccurred())
Expect(out).To(MatchYAML(`empty_dir: null
sub_dir:
sub_dir_file.yml:
foo:
bar:
baz
`))
})
})
Expand Down

0 comments on commit 980eb71

Please sign in to comment.