Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate CLI to use cobra #114

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
af0cdb0
chore: comment out the older cli code
1garo Feb 27, 2024
e35c42d
fix: move version cmd to other file
1garo Feb 27, 2024
78ed1ba
feat: add root command
1garo Feb 27, 2024
2918a69
feat: main file to call cmd.Execute()
1garo Feb 27, 2024
bdd67e1
feat: add cobra to the project
1garo Feb 27, 2024
7ad4826
feat: add flags the way cobra uses it
1garo Feb 27, 2024
d1bf455
feat: refactor to cobra style, seems to be working fine
1garo Feb 27, 2024
bc5123d
chore: default is not 0
1garo Feb 27, 2024
0f5f5e5
chore: add some comments to struct and functions
1garo Feb 27, 2024
e7dd29a
fix: refactor some of the functions
1garo Feb 28, 2024
c6cb3d3
feat(WIP): re-add tests
1garo Feb 28, 2024
6de0733
chore: remove old testing file
1garo Feb 29, 2024
36594c5
feat: add version cmd tests
1garo Feb 29, 2024
7550f12
feat: uncomment and fix other tests
1garo Feb 29, 2024
f2b2242
chore: run gofmt and remove comment
1garo Mar 8, 2024
21c4071
Merge branch 'main' into feat/migrate-to-cobra
1garo Apr 3, 2024
58a6988
chore: merge main
1garo Apr 3, 2024
7e1b9c5
chore: add quiet flag
1garo Apr 3, 2024
923d911
Update Dockerfile
1garo Apr 12, 2024
d581599
Update Dockerfile
1garo Apr 12, 2024
a561a84
Merge branch 'main' into feat/migrate-to-cobra
1garo Apr 12, 2024
66b4a3b
fix: do some cleanup after merge
1garo Apr 12, 2024
8a5c705
fix: properly resolve conflicts of last merge
1garo Apr 12, 2024
a9dddfe
chore: change from cmd/validator to main.go target
1garo Apr 12, 2024
08f83f6
chore: go mod tidy to update new deps
1garo Apr 12, 2024
4d73a9f
fix: add search_path as positional argument
1garo Apr 12, 2024
541f6f9
fix: add back quiet flag and update tests
1garo Apr 12, 2024
dad9045
chore: update readme target to main.go instead of cmd/validator.go
1garo Apr 12, 2024
e220a4a
chore: lint
1garo Apr 12, 2024
d78ef64
fix: update usage to cobra format and commands to use `--`
1garo Apr 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cmd/validator/commands/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
validator "github.com/Boeing/config-file-validator/cmd/validator"
"github.com/spf13/cobra"
)

func CmdFlags(cmd *cobra.Command) {
cmd.PersistentFlags().
IntVar(&validator.Flags.Depth, "depth", 0, "Depth of recursion for the provided search paths. Set depth to 0 to disable recursive path traversal.")
cmd.PersistentFlags().
StringVar(&validator.Flags.ExcludeDirs, "exclude-dirs", "", "Subdirectories to exclude when searching for configuration files")
cmd.PersistentFlags().
StringVar(&validator.Flags.ExcludeFileTypes, "exclude-file-types", "", "A comma separated list of file types to ignore")
cmd.PersistentFlags().StringVar(&validator.Flags.Output, "output", "", "Destination to a file to output results")
cmd.PersistentFlags().
StringVar(&validator.Flags.ReportType, "reporter", "standard", "Format of the printed report. Options are standard and json")
cmd.PersistentFlags().
StringVar(&validator.Flags.GroupOutput, "groupby", "", "Group output by filetype, directory, pass-fail. Supported for Standard and JSON reports")
cmd.PersistentFlags().
StringVar(&validator.Flags.SearchPath, "search_path", ".", "search_path: The search path on the filesystem for configuration files. Defaults to the current working directory if no search_path provided.")
}

func init() {
CmdFlags(rootCmd)
}
68 changes: 68 additions & 0 deletions cmd/validator/commands/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cmd

import (
"bytes"
"fmt"
"strings"
"testing"

cmd "github.com/Boeing/config-file-validator/cmd/validator"
"github.com/spf13/cobra"
)

func ExecuteTestHelper(t *testing.T, c *cobra.Command, args ...string) (string, error) {
t.Helper()

buf := new(bytes.Buffer)
c.SetOut(buf)
c.SetErr(buf)
c.SetArgs(args)

err := c.Execute()
return strings.TrimSpace(buf.String()), err
}

func Test_flags(t *testing.T) {
// We manipulate the Args to set them up for the testcases
cases := []struct {
Name string
Args []string
ExpectedExit int
}{
{"blank", []string{}, 0},
{"negative depth set", []string{"--depth", "-1", "--reporter", "standard"}, 1},
{"flags set, wrong reporter", []string{"--exclude-dirs", "subdir", "--reporter", "wrong"}, 1},
{"flags set, json reporter", []string{"--exclude-dirs", "subdir", "--reporter", "json"}, 0},
{"flags set, junit reported", []string{"--exclude-dirs", "subdir", "--reporter", "junit"}, 0},
{"bad path", []string{"--search_path", "/path/does/not/exit"}, 1},
//{"exclude file types set", []string{"--exclude-file-types", "json"}, 0},
{"multiple paths", []string{"../../../test/fixtures/subdir/good.json", "../../../test/fixtures/good.json"}, 0},
{"output set", []string{"--output", "../../../test/output", "--reporter", "json"}, 0},
{"empty string output set", []string{"--output", "", "--reporter", "json", "."}, 0},
{"wrong output set", []string{"--output", "/path/not/exist", "--reporter", "json", "."}, 1},
{"incorrect group", []string{"--groupby", "badgroup"}, 1},
{"correct group", []string{"--groupby", "directory"}, 0},
}

var exitStatus int

for _, tc := range cases {
fmt.Printf("Testing args: %v = %v\n", tc.Name, tc.Args)
root := &cobra.Command{
Use: "root",
Run: func(c *cobra.Command, args []string) {
exitStatus = cmd.ExecRoot(c)
},
}
CmdFlags(root)

_, err := ExecuteTestHelper(t, root, tc.Args...)
if err != nil {
t.Error(err)
}

if tc.ExpectedExit != exitStatus {
t.Errorf("Wrong exit code, expected: %v, got: %v", tc.ExpectedExit, exitStatus)
}
}
}
25 changes: 25 additions & 0 deletions cmd/validator/commands/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"fmt"
"os"

validator "github.com/Boeing/config-file-validator/cmd/validator"
"github.com/spf13/cobra"
)

// rootCmd command configuration and setup
var rootCmd = &cobra.Command{
Use: "validator",
Short: "Cross Platform tool to validate configuration files",
Run: func(cmd *cobra.Command, args []string) {
os.Exit(validator.ExecRoot(cmd))
},
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
27 changes: 25 additions & 2 deletions version.go → cmd/validator/commands/version.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package configfilevalidator
package cmd

import "fmt"
import (
"fmt"

"github.com/spf13/cobra"
)

// Version information set by link flags during build. We fall back to these sane
// default values when not provided
Expand All @@ -18,9 +22,28 @@ func (v Version) String() string {
return fmt.Sprintf("validator version %v", v.Version)
}

// SetVersion set the version
func SetVersion(v string) {
version = v
}

// GetVersion returns the version information
func GetVersion() Version {
return Version{
Version: version,
}
}

func init() {
rootCmd.AddCommand(versionCmd)
}

// versionCmd command configuration and setup
var versionCmd = &cobra.Command{
Use: "version",
Short: "Version prints the release version of validator",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(GetVersion())
},
}
33 changes: 33 additions & 0 deletions cmd/validator/commands/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

import (
"testing"

cmd "github.com/Boeing/config-file-validator/cmd/validator"
"github.com/spf13/cobra"
)

func TestFlagVersion(t *testing.T) {
var exitStatus int
expectedExit := 0
root := &cobra.Command{
Use: "root",
Run: func(c *cobra.Command, args []string) {
exitStatus = cmd.ExecRoot(c)
}}

SetVersion("testing")
root.AddCommand(versionCmd)

args := []string{"version"}

_, err := ExecuteTestHelper(t, root, args...)
if err != nil {
t.Error(err)
}

if expectedExit != exitStatus {
t.Errorf("Wrong exit code, expected: %v, got: %v", expectedExit, exitStatus)
}

}
Loading
Loading