Skip to content

Commit

Permalink
refactoring (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
mactat authored Jun 25, 2023
1 parent 1ac027c commit 02cdb38
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build/
build/
.vscode/
13 changes: 2 additions & 11 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,11 @@ framed create --template ./framed.yaml --files true
_, dirList := ext.ReadConfig(path)

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

// create files
if createFiles {
for _, dir := range dirList {
if dir.Files == nil {
continue
}
for _, file := range *dir.Files {
ext.CreateFile(dir.Path, file)
}
}
ext.CreateAllFiles(dirList)
}
},
}
Expand Down
31 changes: 2 additions & 29 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ package cmd
import (
"fmt"
"framed/pkg/ext"
"io"
"net/http"
"os"

"github.com/spf13/cobra"
)
Expand All @@ -31,15 +28,15 @@ framed import --example python --output ./python.yaml
output := cmd.Flag("output").Value.String()

if url != "" {
err := importFromUrl(output, url)
err := ext.ImportFromUrl(output, url)
if err != nil {
fmt.Println("Error importing from url: ", err)
return
}
}

if example != "" {
err := importFromUrl(output, exampleToUrl(example))
err := ext.ImportFromUrl(output, ext.ExampleToUrl(example))
if err != nil {
fmt.Println("Error importing from example: ", err)
return
Expand All @@ -55,30 +52,6 @@ framed import --example python --output ./python.yaml
},
}

func exampleToUrl(example string) string {
return "https://raw.githubusercontent.com/mactat/framed/master/examples/" + example + ".yaml"
}

func importFromUrl(path string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

// Create the file
out, err := os.Create(path)
if err != nil {
return err
}
defer out.Close()

// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}

func init() {
rootCmd.AddCommand(importCmd)

Expand Down
5 changes: 0 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,8 @@ templates for defining project structures and ensures that your projects adhere
the defined structure, enabling consistency and reusability.
`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}

// 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 := rootCmd.Execute()
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion cmd/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ framed verify --template ./framed.yaml
verifyFiles(dir, &allGood)
}

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

// verify maxCount
if numFiles > dir.MaxCount {
ext.PrintOut("❌ Max count ("+fmt.Sprint(dir.MaxCount)+") exceeded ==>", dir.Path)
allGood = false
Expand Down
3 changes: 0 additions & 3 deletions cmd/visualize.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,5 @@ framed visualize --template ./framed.yaml`,

func init() {
rootCmd.AddCommand(visualizeCmd)

// Here you will define your flags and configuration settings.

visualizeCmd.PersistentFlags().String("template", "./framed.yaml", "Path to template file default is ./framed.yaml")
}
31 changes: 31 additions & 0 deletions pkg/ext/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ext

import (
"io"
"net/http"
"os"
)

func ExampleToUrl(example string) string {
return "https://raw.githubusercontent.com/mactat/framed/master/examples/" + example + ".yaml"
}

func ImportFromUrl(path string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

// Create the file
out, err := os.Create(path)
if err != nil {
return err
}
defer out.Close()

// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
17 changes: 17 additions & 0 deletions pkg/ext/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ func CreateDir(path string) {

}

func CreateAllDirs(dirList []SingleDir) {
for _, dir := range dirList {
CreateDir(dir.Path)
}
}

func CreateFile(path string, name string) {
// Check if file exists
if _, err := os.Stat(path + "/" + name); errors.Is(err, os.ErrNotExist) {
Expand All @@ -35,6 +41,17 @@ func CreateFile(path string, name string) {
}
}

func CreateAllFiles(dirList []SingleDir) {
for _, dir := range dirList {
if dir.Files == nil {
continue
}
for _, file := range *dir.Files {
CreateFile(dir.Path, file)
}
}
}

// Check if directory exists on given path and is type dir
func DirExists(path string) bool {
if path == "." {
Expand Down

0 comments on commit 02cdb38

Please sign in to comment.