Skip to content

Commit

Permalink
Break a part CLI and CLI tests into their respective packages
Browse files Browse the repository at this point in the history
- Replace codegangsta/cli with the more advanced cobra cli
- diff.go shoudn't be in main
- Fix diff tests
- Move pkg cli tests to their respective packages
- Update vendor dependencies
  • Loading branch information
redhatrises committed Jun 7, 2018
1 parent 3ef42d3 commit 616b199
Show file tree
Hide file tree
Showing 299 changed files with 165,079 additions and 5,051 deletions.
136 changes: 4 additions & 132 deletions cmd/compliance-masonry/compliance-masonry.go
Original file line number Diff line number Diff line change
@@ -1,142 +1,14 @@
package main

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

"github.com/codegangsta/cli"
"github.com/opencontrol/compliance-masonry/pkg/cli/docs"
"github.com/opencontrol/compliance-masonry/pkg/cli/docs/gitbook"
"github.com/opencontrol/compliance-masonry/pkg/cli/get"
"github.com/opencontrol/compliance-masonry/tools/constants"
"github.com/opencontrol/compliance-masonry/tools/fs"
"github.com/opencontrol/compliance-masonry/version"
"github.com/opencontrol/compliance-masonry/pkg/cmd/masonry"
)

var exportPath, markdownPath, opencontrolDir string

// NewCLIApp creates a new instances of the CLI
func NewCLIApp() *cli.App {
app := cli.NewApp()
app.Name = "Compliance Masonry"
app.Usage = "Open Control CLI Tool"
app.Version = version.Version
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "verbose",
Usage: "Indicates whether to run the command with verbosity.",
},
}
app.Before = func(c *cli.Context) error {
// Resets the log to output to nothing
log.SetOutput(ioutil.Discard)
if c.Bool("verbose") {
log.SetOutput(os.Stderr)
log.Println("Running with verbosity")
}
return nil
}
app.Commands = []cli.Command{
{
Name: "get",
Aliases: []string{"g"},
Usage: "Install compliance dependencies",
Flags: []cli.Flag{
cli.StringFlag{
Name: "dest",
Value: constants.DefaultDestination,
Usage: "Location to download the repos.",
},
cli.StringFlag{
Name: "config",
Value: constants.DefaultConfigYaml,
Usage: "Location of system yaml",
},
},
Action: func(c *cli.Context) error {
f := fs.OSUtil{}
config := c.String("config")
configBytes, err := f.OpenAndReadFile(config)
if err != nil {
fmt.Fprintf(app.Writer, "%v\n", err.Error())
os.Exit(1)
}
wd, err := os.Getwd()
if err != nil {
fmt.Fprintf(app.Writer, "%v\n", err.Error())
os.Exit(1)
}
destination := filepath.Join(wd, c.String("dest"))
err = get.Get(destination, configBytes)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
fmt.Fprintf(app.Writer, "%v\n", "Compliance Dependencies Installed")
return nil
},
},
{
Name: "docs",
Aliases: []string{"d"},
Usage: "Create Documentation",
Subcommands: []cli.Command{
{
Name: "gitbook",
Aliases: []string{"g"},
Usage: "Create Gitbook Documentation",
Flags: []cli.Flag{
cli.StringFlag{
Name: "opencontrols, o",
Value: "opencontrols",
Usage: "Set opencontrols directory",
Destination: &opencontrolDir,
},
cli.StringFlag{
Name: "exports, e",
Value: "exports",
Usage: "Sets the export directory",
Destination: &exportPath,
},
cli.StringFlag{
Name: "markdowns, m",
Value: "markdowns",
Usage: "Sets the markdowns directory",
Destination: &markdownPath,
},
},
Action: func(c *cli.Context) error {
config := gitbook.Config{
Certification: c.Args().First(),
OpencontrolDir: opencontrolDir,
ExportPath: exportPath,
MarkdownPath: markdownPath,
}
warning, errMessages := docs.MakeGitbook(config)
if warning != "" {
fmt.Fprintf(app.Writer, "%v\n", warning)
}
if errMessages != nil && len(errMessages) > 0 {
err := cli.NewMultiError(errMessages...)
return cli.NewExitError(err.Error(), 1)
}
fmt.Fprintf(app.Writer, "%v\n", "New Gitbook Documentation Created")
return nil
},
},
},
},
diffCommand,
}
return app
}

func main() {
app := NewCLIApp()
err := app.Run(os.Args)
if err != nil {
log.Fatalln(err)
if err := masonry.Run(); err != nil {
os.Exit(1)
}
os.Exit(0)
}
51 changes: 51 additions & 0 deletions cmd/compliance-masonry/compliance-masonry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main_test

import (
"bufio"
"os/exec"
"strings"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
)

var usage = `
Usage:
compliance-masonry [flags]
compliance-masonry [command]
Available Commands:
diff Compliance Diff Gap Analysis
docs Create compliance documentation
get Install compliance dependencies
help Help about any command
Flags:
-h, --help help for compliance-masonry
--verbose Run with verbosity
-v, --version Print the version
`

var _ = Describe("Masonry CLI", func() {
Describe("When the CLI is run with no commands", func() {
It("should list the available commands", func() {
output := Masonry()
Eventually(output.Out.Contents).Should(ContainSubstring(usage))
})
})
})

func Masonry(args ...string) *Session {
path, err := Build("github.com/opencontrol/compliance-masonry/cmd/compliance-masonry")
Expect(err).NotTo(HaveOccurred())
cmd := exec.Command(path, args...)
stdin, err := cmd.StdinPipe()
Expect(err).ToNot(HaveOccurred())
buffer := bufio.NewWriter(stdin)
_, _ = buffer.WriteString(strings.Join(args, " "))
_ = buffer.Flush()
session, err := Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
return session
}
123 changes: 0 additions & 123 deletions cmd/compliance-masonry/compliance_masonry_test.go

This file was deleted.

48 changes: 0 additions & 48 deletions cmd/compliance-masonry/diff.go

This file was deleted.

0 comments on commit 616b199

Please sign in to comment.