Skip to content

Commit

Permalink
Refactor (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
mactat authored Jun 25, 2023
1 parent 02cdb38 commit f16c0d4
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 51 deletions.
2 changes: 0 additions & 2 deletions cmd/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
4 changes: 1 addition & 3 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
10 changes: 5 additions & 5 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Copyright © 2023 Maciej Tatarski [email protected]
package cmd

import (
"fmt"
"framed/pkg/ext"
"os"

"github.com/spf13/cobra"
)
Expand All @@ -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)
}
}

Expand Down
6 changes: 1 addition & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
39 changes: 3 additions & 36 deletions cmd/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"framed/pkg/ext"
"os"
"strings"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -38,7 +37,7 @@ framed verify --template ./framed.yaml

// verify files
if dir.Files != nil {
verifyFiles(dir, &allGood)
ext.VerifyFiles(dir, &allGood)
}

// verify minCount
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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")
}
1 change: 1 addition & 0 deletions cmd/visualize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
31 changes: 31 additions & 0 deletions pkg/ext/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit f16c0d4

Please sign in to comment.