Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
refactor(cmd): initialize the cmd in a func (#13)
Browse files Browse the repository at this point in the history
Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored May 15, 2024
1 parent 6e67788 commit 95b5b75
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 44 deletions.
68 changes: 35 additions & 33 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,46 @@ import (
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := checkCmd.Execute()
if err != nil {
if err := NewCheckCmd().Execute(); err != nil {
os.Exit(1)
}
}

var apps, components []string
var baseDir string
var verbose bool

// checkCmd represents the base command when called without any subcommands
var checkCmd = &cobra.Command{
Use: "check-argocd",
Short: "Checks the Argo CD configuration",
Args: cobra.ExactArgs(0),
func NewCheckCmd() *cobra.Command {

Run: func(cmd *cobra.Command, _ []string) {
logger := charmlog.New(cmd.OutOrStderr())
logger.SetLevel(charmlog.InfoLevel)
if verbose {
logger.SetLevel(charmlog.DebugLevel)
}
logger.Info("🏁 Checking Argo CD configuration", "base-dir", baseDir)
afs := afero.Afero{
Fs: afero.NewOsFs(),
}
// verifies that the source path of the Applications and ApplicationSets exists
if err := validation.CheckApplications(logger, afs, baseDir, apps...); err != nil {
logger.Error(strings.ReplaceAll(err.Error(), ": ", ":\n"))
os.Exit(1)
}
// verifies that `kustomize build` on each component completes successfully
if err := validation.CheckComponents(logger, afs, baseDir, components...); err != nil {
logger.Error(strings.ReplaceAll(err.Error(), ": ", ":\n"))
os.Exit(1)
}
},
}
var apps, components []string
var baseDir string
var verbose bool

checkCmd := &cobra.Command{
Use: "argocd-checker --base-dir=$(pwd) --apps apps-of-apps,apps --components components --verbose=false",
Short: "Checks the Argo CD configuration",
Args: cobra.ExactArgs(0),

Run: func(cmd *cobra.Command, _ []string) {
logger := charmlog.New(cmd.OutOrStderr())
logger.SetLevel(charmlog.InfoLevel)
if verbose {
logger.SetLevel(charmlog.DebugLevel)
}
logger.Info("🏁 Checking Argo CD configuration", "base-dir", baseDir)
afs := afero.Afero{
Fs: afero.NewOsFs(),
}
// verifies that the source path of the Applications and ApplicationSets exists
if err := validation.CheckApplications(logger, afs, baseDir, apps...); err != nil {
logger.Error(strings.ReplaceAll(err.Error(), ": ", ":\n"))
os.Exit(1)
}
// verifies that `kustomize build` on each component completes successfully
if err := validation.CheckComponents(logger, afs, baseDir, components...); err != nil {
logger.Error(strings.ReplaceAll(err.Error(), ": ", ":\n"))
os.Exit(1)
}
},
}

func init() {
checkCmd.Flags().StringSliceVar(&apps, "apps", []string{}, "path(s) to the applications (comma-separated, relative to '--baseDir')")
if err := checkCmd.MarkFlagRequired("apps"); err != nil {
panic(fmt.Sprintf("failed to mark flag as required: %s", err))
Expand All @@ -65,4 +65,6 @@ func init() {
panic(fmt.Sprintf("failed to mark flag as required: %s", err))
}
checkCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
return checkCmd

}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/charmbracelet/log v0.2.5
github.com/sanity-io/litter v1.5.5
github.com/spf13/afero v1.6.0
github.com/spf13/cobra v1.7.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
k8s.io/apimachinery v0.25.0
sigs.k8s.io/kustomize/api v0.15.0
Expand Down
6 changes: 3 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ github.com/charmbracelet/log v0.2.5/go.mod h1:nQGK8tvc4pS9cvVEH/pWJiZ50eUq1aoXUO
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -95,8 +95,8 @@ github.com/sanity-io/litter v1.5.5/go.mod h1:9gzJgR2i4ZpjZHsKvUXIRQVk7P+yM3e+jAF
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
5 changes: 1 addition & 4 deletions pkg/validation/kustomize_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package validation

import (
"bytes"
"os/exec"
"path/filepath"

"github.com/spf13/afero"
Expand All @@ -21,7 +20,7 @@ func lookupKustomizationFile(logger Logger, afs afero.Afero, basedir string) (st
return "", false
}

// verifies that `kustomize build` completes successfully
// Verifies that `kustomize build` completes successfully
func checkBuild(logger Logger, fsys kfsys.FileSystem, path string) error {
logger.Debug("👀 checking kustomize build", "path", path)
buffy := new(bytes.Buffer)
Expand All @@ -30,7 +29,5 @@ func checkBuild(logger Logger, fsys kfsys.FileSystem, path string) error {
if err := kcmd.RunE(kcmd, []string{path}); err != nil {
return err
}

exec.Command("kustomize", "build")
return nil
}
6 changes: 3 additions & 3 deletions pkg/validation/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"sigs.k8s.io/kustomize/api/types"
)

// compares the entries of `resources` in the Kustomize file with the contents in the current directory to see if
// any local file is missing (not referenced as a resource). Files starting with an underscore character (`_`) are
// ingored
// Compares the entries of `resources` in the Kustomize file with the contents in the current directory to see if
// any local file is missing (not referenced as a resource).
// Files starting with an underscore character (`_`) are ignored
func checkKustomizeResources(logger Logger, afs afero.Afero, path string) error {
logger.Debug("checking kustomization resource", "path", path)
data, err := afs.ReadFile(path)
Expand Down

0 comments on commit 95b5b75

Please sign in to comment.