diff --git a/.gitignore b/.gitignore index 55371e5..91b123d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -.vscode \ No newline at end of file +.vscode +bin/test diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 418c6d3..3376b39 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -16,6 +16,10 @@ or by [gh](https://github.com/cli/cli) : - Change directory to your local copy: `cd ` +- Setup the project with: + ``` + make setup + ``` - `go mod tidy` (Optional, if bash errors out) - Build the tool using make: `make build` @@ -24,4 +28,4 @@ Now you will have your tool in `bin` folder, you can try it out following `Readm - \ No newline at end of file + diff --git a/Makefile b/Makefile index 4fa5660..ffffb20 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ dev: - nodemon --exec "go fmt && go build -o bin/baren" main.go + nodemon --exec "go fmt && go build -o bin/test/baren" main.go build: go build -o bin/bare main.go @@ -11,3 +11,7 @@ release: nightly: go build -o bin/baren main.go + +setup: + echo "Setting up" + go mod download diff --git a/bin/bare b/bin/bare index 8785915..9e1732d 100755 Binary files a/bin/bare and b/bin/bare differ diff --git a/bin/baren b/bin/baren deleted file mode 100755 index 766a854..0000000 Binary files a/bin/baren and /dev/null differ diff --git a/cmd/add.go b/cmd/add.go index c3c88f5..92b495c 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -3,7 +3,7 @@ package cmd import ( "bare/parser" "bare/styles" - "bare/utils" + "bare/utils/osutil" "fmt" "os" "path/filepath" @@ -32,7 +32,7 @@ func addBare() { for _, objPath := range parser.BareObj.Include { sourcePath := filepath.Join(".", objPath) destiPath := filepath.Join(barePath, objPath) - err := utils.CopyFileDirectory(sourcePath, destiPath) + err := osutil.CopyFileDirectory(sourcePath, destiPath) if err != nil { fmt.Print(styles.InitError.Render("[Error] "), styles.AddFileStlyle.Render(objPath)) fmt.Println("") diff --git a/cmd/bareinit.go b/cmd/bareinit.go index 2b4b369..8bf298e 100644 --- a/cmd/bareinit.go +++ b/cmd/bareinit.go @@ -3,7 +3,7 @@ package cmd import ( "bare/parser" "bare/styles" - "bare/utils" + "bare/utils/osutil" "encoding/json" "fmt" "io/ioutil" @@ -39,8 +39,8 @@ func bareInit(bareName string) { currDir, _ := os.Getwd() homePath := os.Getenv("HOME") - bareFolderExsists := utils.Exists(filepath.Join(homePath, ".bare", bareName)) - recipeFileExsists := utils.Exists(filepath.Join(currDir, "recipe.json")) + bareFolderExsists := osutil.Exists(filepath.Join(homePath, ".bare", bareName)) + recipeFileExsists := osutil.Exists(filepath.Join(currDir, "recipe.json")) if bareFolderExsists { fmt.Println(styles.InitError.Render("[Error] Bare with similar name exsists")) diff --git a/cmd/get.go b/cmd/get.go index 3c64675..8e08187 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -2,8 +2,9 @@ package cmd import ( "bare/styles" - "bare/utils" + "bare/utils/git" "bare/utils/host" + "bare/utils/osutil" "fmt" "os" "path" @@ -30,12 +31,12 @@ func getGithub(args []string) { currDir, _ := os.Getwd() targetPath := path.Join(currDir, args[1]) - if utils.Exists(path.Join(currDir, args[1])) { + if osutil.Exists(path.Join(currDir, args[1])) { fmt.Println(styles.InitError.Render("Directory with similar name already exsists !")) os.Exit(1) } - if err := utils.CloneRepo(targetPath, utils.CloneOptions{ + if err := git.CloneRepo(targetPath, git.CloneOptions{ URL: host.URL(args[0]), }); err != nil { fmt.Println(styles.InitError.Render("There was error cloning the repo"), args[0]) diff --git a/cmd/include.go b/cmd/include.go index 88ead5c..aeea46b 100644 --- a/cmd/include.go +++ b/cmd/include.go @@ -3,7 +3,7 @@ package cmd import ( "bare/parser" "bare/styles" - "bare/utils" + "bare/utils/osutil" "fmt" "os" "path/filepath" @@ -35,7 +35,7 @@ func includeFiles(objects []string) { if incMap[objs] { continue } else { - if utils.Exists(filepath.Join(currDir, objs)) { + if osutil.Exists(filepath.Join(currDir, objs)) { parser.BareObj.Include = append(parser.BareObj.Include, objs) fmt.Println(styles.InitSuccess.Render("[Add] " + objs)) } else { diff --git a/cmd/rm.go b/cmd/rm.go index 1ecc67f..3be2304 100644 --- a/cmd/rm.go +++ b/cmd/rm.go @@ -2,7 +2,7 @@ package cmd import ( "bare/styles" - "bare/utils" + "bare/utils/osutil" "fmt" "os" "path/filepath" @@ -30,7 +30,7 @@ func rmBare(delBares []string) { fmt.Println(styles.InitStyle.Render("Bare rm")) barePath := filepath.Join(os.Getenv("HOME"), ".bare") for _, bare := range delBares { - if utils.Exists(filepath.Join(barePath, bare)) { + if osutil.Exists(filepath.Join(barePath, bare)) { fmt.Println(styles.InitError.Render("[Deleting] "), bare) os.RemoveAll(filepath.Join(barePath, bare)) } diff --git a/cmd/temp.go b/cmd/temp.go new file mode 100644 index 0000000..c84832a --- /dev/null +++ b/cmd/temp.go @@ -0,0 +1,35 @@ +package cmd + +import ( + "bare/utils/template" + "fmt" + "log" + "os" + "path/filepath" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(templateCmd) +} + +var templateCmd = &cobra.Command{ + Use: "temp", + Short: "add you current bare to repo", + Long: "same as short for addCmd", + Run: func(cmd *cobra.Command, args []string) { + tempBare() + }, +} + +func tempBare() { + fmt.Println("Hello world") + bareName := "test" + // currDir, _ := os.Getwd() + barePath := filepath.Join(os.Getenv("HOME"), ".bare", bareName) + err := template.Execute(barePath, "working") + if err != nil { + log.Fatal(err) + } +} diff --git a/cmd/touch.go b/cmd/touch.go index 89f8bef..252e411 100644 --- a/cmd/touch.go +++ b/cmd/touch.go @@ -3,7 +3,7 @@ package cmd import ( "bare/parser" "bare/styles" - "bare/utils" + "bare/utils/osutil" "bufio" "fmt" "os" @@ -35,7 +35,7 @@ func touchFile(touchName, newName string) { fmt.Println(styles.InitError.Render("No such touch present")) os.Exit(1) } else { - if utils.Exists(filepath.Join(currDir, newName)) { + if osutil.Exists(filepath.Join(currDir, newName)) { fmt.Println("Already exsists !!") inp := "n" // warning reader := bufio.NewReader(os.Stdin) @@ -43,12 +43,12 @@ func touchFile(touchName, newName string) { inp, _ = reader.ReadString('\n') inp = strings.Trim(inp, " ") if inp == "y" || inp == "Y" { - utils.CopyFileDirectory(filepath.Join(barePath+"/"+touchMap[touchName]), filepath.Join(currDir, newName)) + osutil.CopyFileDirectory(filepath.Join(barePath+"/"+touchMap[touchName]), filepath.Join(currDir, newName)) } else { os.Exit(0) } } else { - utils.CopyFileDirectory(filepath.Join(barePath, touchMap[touchName]), filepath.Join(currDir, newName)) + osutil.CopyFileDirectory(filepath.Join(barePath, touchMap[touchName]), filepath.Join(currDir, newName)) } } } diff --git a/cmd/use.go b/cmd/use.go index dac3e3d..9c201a2 100644 --- a/cmd/use.go +++ b/cmd/use.go @@ -2,7 +2,7 @@ package cmd import ( "bare/styles" - "bare/utils" + "bare/utils/osutil" "fmt" "os" "path/filepath" @@ -27,18 +27,18 @@ func useBare(bareName, desti string) { currDir, _ := os.Getwd() barePath := filepath.Join(os.Getenv("HOME"), ".bare") - if !utils.Exists(filepath.Join(barePath, bareName)) { + if !osutil.Exists(filepath.Join(barePath, bareName)) { fmt.Println(styles.InitError.Render("Bare doesn't exsist")) fmt.Println("User `bare list` to get list of all the bares") os.Exit(1) } - if utils.Exists(filepath.Join(currDir, desti)) { + if osutil.Exists(filepath.Join(currDir, desti)) { fmt.Println(styles.InitError.Render("File name already exsists")) os.Exit(1) } else { - utils.CreateIfNotExists(filepath.Join(currDir, desti), 0755) - utils.CopyDirectory(filepath.Join(barePath, bareName), filepath.Join(currDir, desti)) + osutil.CreateIfNotExists(filepath.Join(currDir, desti), 0755) + osutil.CopyDirectory(filepath.Join(barePath, bareName), filepath.Join(currDir, desti)) } } diff --git a/go.mod b/go.mod index 1a527a0..711a5b6 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( require ( github.com/go-git/go-git/v5 v5.4.2 github.com/golang/protobuf v1.5.2 // indirect - github.com/google/go-github/v39 v39.1.0 // indirect + github.com/google/go-github/v39 v39.1.0 github.com/google/go-querystring v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect @@ -24,7 +24,7 @@ require ( github.com/spf13/pflag v1.0.5 // indirect golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect - golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect + golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f google.golang.org/appengine v1.6.7 // indirect google.golang.org/protobuf v1.26.0 // indirect ) diff --git a/main.go b/main.go index 7ba1fc6..cc59c5c 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,11 @@ package main import ( "bare/cmd" - "bare/utils" + "bare/utils/osutil" ) func main() { // Check for .bare folder - utils.MakeInitFolder() + osutil.MakeInitFolder() cmd.Execute() } diff --git a/parser/parser.go b/parser/parser.go index bb01665..94485bb 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -1,7 +1,7 @@ package parser import ( - "bare/utils" + "bare/utils/osutil" "encoding/json" "io/ioutil" "log" @@ -42,7 +42,7 @@ func UpdateRecipe() { if err != nil { log.Fatal(err) } - if utils.Exists(recipePath) { + if osutil.Exists(recipePath) { err = ioutil.WriteFile(recipePath, updatedRecipe, 0644) if err != nil { log.Fatal(err) diff --git a/utils/gitutils.go b/utils/git/gitutils.go similarity index 98% rename from utils/gitutils.go rename to utils/git/gitutils.go index d0740d8..b170efa 100644 --- a/utils/gitutils.go +++ b/utils/git/gitutils.go @@ -1,4 +1,4 @@ -package utils +package git import ( "context" diff --git a/utils/host/github.go b/utils/host/github.go index 23aa86a..777ce2e 100644 --- a/utils/host/github.go +++ b/utils/host/github.go @@ -10,18 +10,6 @@ const ( githubStorageURL = "https://codeload.github.com" ) -// func ZipURL(repo string) string { -// var version = "main" - -// repo = strings.TrimSuffix(strings.TrimPrefix(repo, "/"), "/") - -// zipRegex := regexp.MustCompile(`zip/(\S+)$`) -// if zipRegex.MatchString(repo) { -// return repo -// } - -// } - func URL(repo string) string { githubRegex := regexp.MustCompile(githubURL + `/(\S+)$`) if githubRegex.MatchString(repo) { diff --git a/utils/copy.go b/utils/osutil/fs.go similarity index 99% rename from utils/copy.go rename to utils/osutil/fs.go index 97e1ee1..ec27b41 100644 --- a/utils/copy.go +++ b/utils/osutil/fs.go @@ -1,4 +1,4 @@ -package utils +package osutil import ( "fmt" diff --git a/utils/initFolder.go b/utils/osutil/init.go similarity index 93% rename from utils/initFolder.go rename to utils/osutil/init.go index 0b9169d..2ac7715 100644 --- a/utils/initFolder.go +++ b/utils/osutil/init.go @@ -1,4 +1,4 @@ -package utils +package osutil import ( "log" diff --git a/utils/stringutils/stringutil.go b/utils/stringutils/stringutil.go new file mode 100644 index 0000000..ba1ea63 --- /dev/null +++ b/utils/stringutils/stringutil.go @@ -0,0 +1,33 @@ +package stringutils + +import "io" + +type String interface { + io.ReadWriter + String() string +} + +func NewString(contents string) String { + s := str(contents) + return &s +} + +type str string + +func (s *str) Read(buf []byte) (int, error) { + ss := string(*s) + for i := range buf { + buf[i] = byte(ss[i]) + } + + return len(buf), nil +} + +func (s *str) Write(buf []byte) (int, error) { + *s = str(string(*s) + string(buf)) + return len(buf), nil +} + +func (s *str) String() string { + return string(*s) +} diff --git a/utils/template/template.go b/utils/template/template.go new file mode 100644 index 0000000..6cdab5b --- /dev/null +++ b/utils/template/template.go @@ -0,0 +1,55 @@ +package template + +import ( + "os" + "path/filepath" + "text/template" + + "bare/utils/stringutils" +) + +type Interface interface { + Execute(string) error + + UseDefaultValues() +} + +type Template struct { + Path string + ShouldUseDefaults bool +} + +/* +- download the template +- run through the template and change the {{AppName}} +*/ + +var Options = []string{ + "missingkey=invalid", +} + +type AppNameReplace struct { + AppName string +} + +// Replaces {{AppName}} from the template +func Execute(path string, fileName string) error { + return filepath.Walk(path, func(fileName string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + oldName, err := filepath.Rel(path, fileName) + if err != nil { + return err + } + + buf := stringutils.NewString("") + appName := AppNameReplace{"working"} + fnameTmpl := template.Must(template.New("File name template").Option(Options...).Parse(oldName)) + if err := fnameTmpl.Execute(buf, appName); err != nil { + return err + } + return nil + }) +}