Skip to content

Commit

Permalink
Move business logic to separate package (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
mactat authored Jun 24, 2023
1 parent e552026 commit 1ac027c
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 97 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ You can use framed as a github action to verify your project structure. Minimal
template: https://yourfile.com/framed.yaml # Share templates between projects
```

- [ ] Add some unit tests
- [x] Add some tests
- [ ] Add contributing guidelines
- [ ] Add more examples
- [x] Create a github action for running tests
- [ ] Move remaining business logic to a separate package
23 changes: 12 additions & 11 deletions cmd/capture.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
Copyright © 2023 Maciej Tatarski maciektatarski@gmail.com
*/

// Package cmd represents the command line interface of the application
Expand All @@ -8,6 +8,7 @@ package cmd
import (
"fmt"

"framed/pkg/ext"
"os"
"strconv"

Expand All @@ -29,26 +30,26 @@ framed capture --output ./framed.yaml --name my-project
depthStr := cmd.Flag("depth").Value.String()
depth, err := strconv.Atoi(depthStr)
if err != nil {
print("🚨 Invalid depth value: ", depthStr)
ext.PrintOut("🚨 Invalid depth value: ", depthStr)
os.Exit(1)
}
print("📝 Name:", name+"\n")
ext.PrintOut("📝 Name:", name+"\n")

// capture subdirectories
subdirs := captureSubDirs(".", depth)
print("📂 Directories:", fmt.Sprintf("%v", len(subdirs)))
subdirs := ext.CaptureSubDirs(".", depth)
ext.PrintOut("📂 Directories:", fmt.Sprintf("%v", len(subdirs)))

// capture files
files := captureAllFiles(".", depth)
print("📄 Files:", fmt.Sprintf("%v", len(files)))
files := ext.CaptureAllFiles(".", depth)
ext.PrintOut("📄 Files:", fmt.Sprintf("%v", len(files)))

// capture patterns
patterns := captureRequiredPatterns(".", depth)
print("🔁 Patterns:", fmt.Sprintf("%v", len(patterns)))
patterns := ext.CaptureRequiredPatterns(".", depth)
ext.PrintOut("🔁 Patterns:", fmt.Sprintf("%v", len(patterns)))

// export config
exportConfig(name, output, subdirs, files, patterns)
print("\n✅ Exported to file: ", output)
ext.ExportConfig(name, output, subdirs, files, patterns)
ext.PrintOut("\n✅ Exported to file: ", output)
},
}

Expand Down
10 changes: 6 additions & 4 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
Copyright © 2023 Maciej Tatarski maciektatarski@gmail.com
*/

// Package cmd represents the command line interface of the application
package cmd

import (
"framed/pkg/ext"

"github.com/spf13/cobra"
)

Expand All @@ -23,11 +25,11 @@ framed create --template ./framed.yaml --files true
createFiles := cmd.Flag("files").Value.String() == "true"

// read config
_, dirList := readConfig(path)
_, dirList := ext.ReadConfig(path)

// create directories
for _, dir := range dirList {
createDir(dir.Path)
ext.CreateDir(dir.Path)
}

// create files
Expand All @@ -37,7 +39,7 @@ framed create --template ./framed.yaml --files true
continue
}
for _, file := range *dir.Files {
createFile(dir.Path, file)
ext.CreateFile(dir.Path, file)
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/import.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
Copyright © 2023 Maciej Tatarski maciektatarski@gmail.com
*/

// Package cmd represents the command line interface of the application
package cmd

import (
"fmt"
"framed/pkg/ext"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -45,11 +46,11 @@ framed import --example python --output ./python.yaml
}
}

print("✅ Saved to ==>", output)
ext.PrintOut("✅ Saved to ==>", output)

// try to load
configTree, _ := readConfig(output)
print("✅ Imported successfully ==>", configTree.Name)
configTree, _ := ext.ReadConfig(output)
ext.PrintOut("✅ Imported successfully ==>", configTree.Name)

},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
Copyright © 2023 Maciej Tatarski maciektatarski@gmail.com
*/

// Package cmd represents the command line interface of the application
Expand Down
43 changes: 22 additions & 21 deletions cmd/verify.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
Copyright © 2023 Maciej Tatarski maciektatarski@gmail.com
*/

// Package cmd represents the command line interface of the application
package cmd

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

Expand All @@ -25,13 +26,13 @@ framed verify --template ./framed.yaml
Run: func(cmd *cobra.Command, args []string) {
path := cmd.Flag("template").Value.String()
// read config
_, dirList := readConfig(path)
_, dirList := ext.ReadConfig(path)

allGood := true
// verify directories
for _, dir := range dirList {
if !dirExists(dir.Path) {
print("❌ Directory not found ==>", dir.Path)
if !ext.DirExists(dir.Path) {
ext.PrintOut("❌ Directory not found ==>", dir.Path)
allGood = false
}

Expand All @@ -41,27 +42,27 @@ framed verify --template ./framed.yaml
}

// verify minCount and maxCount
numFiles := countFiles(dir.Path)
numFiles := ext.CountFiles(dir.Path)
if numFiles < dir.MinCount {
print("❌ Min count ("+fmt.Sprint(dir.MinCount)+") not met ==>", dir.Path)
ext.PrintOut("❌ Min count ("+fmt.Sprint(dir.MinCount)+") not met ==>", dir.Path)
allGood = false
}
if numFiles > dir.MaxCount {
print("❌ Max count ("+fmt.Sprint(dir.MaxCount)+") exceeded ==>", dir.Path)
ext.PrintOut("❌ Max count ("+fmt.Sprint(dir.MaxCount)+") exceeded ==>", dir.Path)
allGood = false
}

// verify childrenAllowed
if !dir.AllowChildren {
if hasDirs(dir.Path) {
print("❌ Children not allowed ==>", dir.Path)
if ext.HasDirs(dir.Path) {
ext.PrintOut("❌ Children not allowed ==>", dir.Path)
allGood = false
}
}

// verify maxDepth
if checkDepth(dir.Path) > dir.MaxDepth {
print("❌ Max depth exceeded ("+fmt.Sprint(dir.MaxDepth)+") ==>", dir.Path)
if ext.CheckDepth(dir.Path) > dir.MaxDepth {
ext.PrintOut("❌ Max depth exceeded ("+fmt.Sprint(dir.MaxDepth)+") ==>", dir.Path)
allGood = false
}

Expand All @@ -84,33 +85,33 @@ framed verify --template ./framed.yaml
},
}

func verifyFiles(dir SingleDir, allGood *bool) {
func verifyFiles(dir ext.SingleDir, allGood *bool) {
for _, file := range *dir.Files {
if !fileExists(dir.Path + "/" + file) {
print("❌ File not found ==>", dir.Path+"/"+file)
if !ext.FileExists(dir.Path + "/" + file) {
ext.PrintOut("❌ File not found ==>", dir.Path+"/"+file)
*allGood = false
}
}
}
func verifyForbiddenPatterns(dir SingleDir, allGood *bool) {
func verifyForbiddenPatterns(dir ext.SingleDir, allGood *bool) {
for _, pattern := range *dir.ForbiddenPatterns {
matched := matchPatternInDir(dir.Path, pattern)
matched := ext.MatchPatternInDir(dir.Path, pattern)
for _, match := range matched {
print("❌ Forbidden pattern ("+pattern+") matched under ==>", dir.Path+"/"+match)
ext.PrintOut("❌ Forbidden pattern ("+pattern+") matched under ==>", dir.Path+"/"+match)
*allGood = false
}
}
}

func verifyAllowedPatterns(dir SingleDir, allGood *bool) {
func verifyAllowedPatterns(dir ext.SingleDir, allGood *bool) {
matchedCount := 0
for _, pattern := range *dir.AllowedPatterns {
matched := matchPatternInDir(dir.Path, pattern)
matched := ext.MatchPatternInDir(dir.Path, pattern)
matchedCount += len(matched)
}
if matchedCount != countFiles(dir.Path) && len(*dir.AllowedPatterns) > 0 {
if matchedCount != ext.CountFiles(dir.Path) && len(*dir.AllowedPatterns) > 0 {
patternsString := strings.Join(*dir.AllowedPatterns, " ")
print("❌ Not all files match required pattern ("+patternsString+") under ==>", dir.Path)
ext.PrintOut("❌ Not all files match required pattern ("+patternsString+") under ==>", dir.Path)
*allGood = false
}
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/visualize.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
Copyright © 2023 Maciej Tatarski maciektatarski@gmail.com
*/

// Package cmd represents the command line interface of the application
package cmd

import (
"framed/pkg/ext"

"github.com/spf13/cobra"
)

Expand All @@ -21,10 +23,10 @@ framed visualize --template ./framed.yaml`,
path := cmd.Flag("template").Value.String()

// read config
_, dirList := readConfig(path)
_, dirList := ext.ReadConfig(path)

// visualize template
visualizeTemplate(dirList)
ext.VisualizeTemplate(dirList)
},
}

Expand Down
9 changes: 5 additions & 4 deletions dockerfiles/dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ ARG ALPINE_VERSION=3.18
FROM golang:${GO_VERSION}-alpine${ALPINE_VERSION} as builder

WORKDIR /app
COPY go.mod go.sum ./
COPY go.mod go.sum /app/
RUN go mod download
COPY framed.go ./
COPY cmd ./cmd
RUN go build -o framed ./framed.go
COPY framed.go /app/
COPY cmd /app/cmd
COPY pkg /app/pkg
RUN go build -o framed /app/framed.go

FROM alpine:${ALPINE_VERSION} as release

Expand Down
2 changes: 1 addition & 1 deletion framed.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
Copyright © 2023 Maciej Tatarski maciektatarski@gmail.com
*/
package main

Expand Down
7 changes: 6 additions & 1 deletion framed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ structure:
allowedPatterns:
- ".go"
forbiddenPatterns:
- "_test.go" # Disallow tests in /src
- "_test.go" # Disallow tests in /cmd
- name: pkg
dirs:
- name: ext
allowedPatterns:
- ".go"
- name: .github
dirs:
- name: workflows
Expand Down
21 changes: 8 additions & 13 deletions cmd/decoder.go → pkg/ext/decoder.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/

// Package cmd represents the command line interface of the application
package cmd
package ext

import (
"io/ioutil"
Expand Down Expand Up @@ -45,29 +40,29 @@ type config struct {
Structure *SingleDir `yaml:"structure"`
}

func readConfig(path string) (config, []SingleDir) {
func ReadConfig(path string) (config, []SingleDir) {
yamlFile, err := ioutil.ReadFile(path)
if err != nil {
// add emoji
print("☠️ Can not read file ==>", path)
PrintOut("☠️ Can not read file ==>", path)
os.Exit(1)
}
print("✅ Loaded template from ==>", path)
PrintOut("✅ Loaded template from ==>", path)
// Map to store the parsed YAML data
var curConfig config

// Unmarshal the YAML string into the data map
err = yaml.Unmarshal([]byte(yamlFile), &curConfig)
if err != nil {
print("☠️ Can not decode file ==>", path)
PrintOut("☠️ Can not decode file ==>", path)
os.Exit(1)
}

if curConfig.Structure == nil {
print("☠️ Can not find correct structure in ==>", path)
PrintOut("☠️ Can not find correct structure in ==>", path)
os.Exit(1)
} else {
print("✅ Read structure for ==>", curConfig.Name)
PrintOut("✅ Read structure for ==>", curConfig.Name)
}

dirList := []SingleDir{}
Expand All @@ -78,7 +73,7 @@ func readConfig(path string) (config, []SingleDir) {
func traverseStructure(dir *SingleDir, path string, dirsList *[]SingleDir) {
// Change path
if dir == nil {
print("☠️ Can't traverse nil dir ==>", path)
PrintOut("☠️ Can't traverse nil dir ==>", path)
os.Exit(1)
}
dir.Path = path
Expand Down
11 changes: 3 additions & 8 deletions cmd/encoder.go → pkg/ext/encoder.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/

// Package cmd represents the command line interface of the application
package cmd
package ext

import (
"fmt"
Expand All @@ -17,7 +12,7 @@ import (
type SingleDirOut struct {
Name string `yaml:"name"`
Path string `yaml:"-"`
Files *[]string `yaml:"files",omitempty`
Files *[]string `yaml:"files,omitempty"`
Dirs *[]SingleDirOut `yaml:"dirs,omitempty"`
AllowedPatterns *[]string `yaml:"allowedPatterns,omitempty"`
ForbiddenPatterns *[]string `yaml:"forbiddenPatterns,omitempty"`
Expand All @@ -32,7 +27,7 @@ type configOut struct {
Structure *SingleDirOut `yaml:"structure"`
}

func exportConfig(name string, path string, subdirs []string, files []string, patterns map[string]string) {
func ExportConfig(name string, path string, subdirs []string, files []string, patterns map[string]string) {
// create config, files and dirs are empty
config := configOut{
Name: name,
Expand Down
Loading

0 comments on commit 1ac027c

Please sign in to comment.