diff --git a/cmd/capture.go b/cmd/capture.go index 4f9155d..96dae6e 100644 --- a/cmd/capture.go +++ b/cmd/capture.go @@ -56,11 +56,9 @@ framed capture --output ./framed.yaml --name my-project func init() { rootCmd.AddCommand(captureCmd) - // Here you will define your flags and configuration settings. captureCmd.PersistentFlags().String("output", "./framed.yaml", "path to output file") captureCmd.PersistentFlags().String("name", "default", "name of the project") - // Int flag - depth captureCmd.PersistentFlags().String("depth", "-1", "depth of the directory tree to capture") } diff --git a/cmd/create.go b/cmd/create.go index 1a79e03..431fcf6 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -40,9 +40,7 @@ framed create --template ./framed.yaml --files true func init() { rootCmd.AddCommand(createCmd) - // Here you will define your flags and configuration settings. - createCmd.PersistentFlags().String("template", "./framed.yaml", "path to template file default") - // add flag to create required files + createCmd.PersistentFlags().Bool("files", false, "create required files") } diff --git a/cmd/import.go b/cmd/import.go index f71c514..33f4a80 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -6,8 +6,8 @@ Copyright © 2023 Maciej Tatarski maciektatarski@gmail.com package cmd import ( - "fmt" "framed/pkg/ext" + "os" "github.com/spf13/cobra" ) @@ -30,16 +30,16 @@ framed import --example python --output ./python.yaml if url != "" { err := ext.ImportFromUrl(output, url) if err != nil { - fmt.Println("Error importing from url: ", err) - return + ext.PrintOut("☠️ Failed to import from url ==>", url) + os.Exit(1) } } if example != "" { err := ext.ImportFromUrl(output, ext.ExampleToUrl(example)) if err != nil { - fmt.Println("Error importing from example: ", err) - return + ext.PrintOut("☠️ Failed to import from example ==>", example) + os.Exit(1) } } diff --git a/cmd/root.go b/cmd/root.go index e8454f2..17a6a0e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -31,8 +31,4 @@ func Execute() { } } -func init() { - // Here you will define your flags and configuration settings. - // Cobra supports persistent flags, which, if defined here, - // will be global for your application. -} +func init() {} diff --git a/cmd/verify.go b/cmd/verify.go index e7d55ec..fbafc67 100644 --- a/cmd/verify.go +++ b/cmd/verify.go @@ -9,7 +9,6 @@ import ( "fmt" "framed/pkg/ext" "os" - "strings" "github.com/spf13/cobra" ) @@ -38,7 +37,7 @@ framed verify --template ./framed.yaml // verify files if dir.Files != nil { - verifyFiles(dir, &allGood) + ext.VerifyFiles(dir, &allGood) } // verify minCount @@ -70,12 +69,12 @@ framed verify --template ./framed.yaml // Verify forbidden if dir.ForbiddenPatterns != nil { - verifyForbiddenPatterns(dir, &allGood) + ext.VerifyForbiddenPatterns(dir, &allGood) } // Verify allowed patterns if dir.AllowedPatterns != nil { - verifyAllowedPatterns(dir, &allGood) + ext.VerifyAllowedPatterns(dir, &allGood) } } @@ -87,40 +86,8 @@ framed verify --template ./framed.yaml }, } -func verifyFiles(dir ext.SingleDir, allGood *bool) { - for _, file := range *dir.Files { - if !ext.FileExists(dir.Path + "/" + file) { - ext.PrintOut("❌ File not found ==>", dir.Path+"/"+file) - *allGood = false - } - } -} -func verifyForbiddenPatterns(dir ext.SingleDir, allGood *bool) { - for _, pattern := range *dir.ForbiddenPatterns { - matched := ext.MatchPatternInDir(dir.Path, pattern) - for _, match := range matched { - ext.PrintOut("❌ Forbidden pattern ("+pattern+") matched under ==>", dir.Path+"/"+match) - *allGood = false - } - } -} - -func verifyAllowedPatterns(dir ext.SingleDir, allGood *bool) { - matchedCount := 0 - for _, pattern := range *dir.AllowedPatterns { - matched := ext.MatchPatternInDir(dir.Path, pattern) - matchedCount += len(matched) - } - if matchedCount != ext.CountFiles(dir.Path) && len(*dir.AllowedPatterns) > 0 { - patternsString := strings.Join(*dir.AllowedPatterns, " ") - ext.PrintOut("❌ Not all files match required pattern ("+patternsString+") under ==>", dir.Path) - *allGood = false - } -} - func init() { rootCmd.AddCommand(testCmd) - // Here you will define your flags and configuration settings. testCmd.PersistentFlags().String("template", "./framed.yaml", "path to template file") } diff --git a/cmd/visualize.go b/cmd/visualize.go index 32265f8..ecb19ad 100644 --- a/cmd/visualize.go +++ b/cmd/visualize.go @@ -32,5 +32,6 @@ framed visualize --template ./framed.yaml`, func init() { rootCmd.AddCommand(visualizeCmd) + visualizeCmd.PersistentFlags().String("template", "./framed.yaml", "Path to template file default is ./framed.yaml") } diff --git a/pkg/ext/system.go b/pkg/ext/system.go index a573454..cb62f1e 100644 --- a/pkg/ext/system.go +++ b/pkg/ext/system.go @@ -217,3 +217,34 @@ func CaptureRequiredPatterns(path string, depth int) map[string]string { } return rules } + +func VerifyFiles(dir SingleDir, allGood *bool) { + for _, file := range *dir.Files { + if !FileExists(dir.Path + "/" + file) { + PrintOut("❌ File not found ==>", dir.Path+"/"+file) + *allGood = false + } + } +} +func VerifyForbiddenPatterns(dir SingleDir, allGood *bool) { + for _, pattern := range *dir.ForbiddenPatterns { + matched := MatchPatternInDir(dir.Path, pattern) + for _, match := range matched { + PrintOut("❌ Forbidden pattern ("+pattern+") matched under ==>", dir.Path+"/"+match) + *allGood = false + } + } +} + +func VerifyAllowedPatterns(dir SingleDir, allGood *bool) { + matchedCount := 0 + for _, pattern := range *dir.AllowedPatterns { + matched := MatchPatternInDir(dir.Path, pattern) + matchedCount += len(matched) + } + if matchedCount != CountFiles(dir.Path) && len(*dir.AllowedPatterns) > 0 { + patternsString := strings.Join(*dir.AllowedPatterns, " ") + PrintOut("❌ Not all files match required pattern ("+patternsString+") under ==>", dir.Path) + *allGood = false + } +}