From 017bc14d8856b4d1021841dda99f4d5d9d604fb7 Mon Sep 17 00:00:00 2001 From: Shlok Patel Date: Fri, 5 Nov 2021 23:36:37 +0530 Subject: [PATCH] Add use functionality - we remove add, bareinit, include, touch commands - Redesig "use" command according to new bare - move parser into util pkg - add "gitDownload" to download package --- cmd/add.go | 46 ---------------- cmd/bareinit.go | 73 ------------------------- cmd/include.go | 47 ---------------- cmd/touch.go | 54 ------------------- cmd/use.go | 55 ++++++++++++------- go.mod | 7 ++- go.sum | 21 ++++++++ parser/parser.go | 51 ------------------ test/recipe.json | 13 ++++- utils/git/gitDownload.go | 37 +++++++++++++ utils/osutil/init.go | 13 ++++- utils/parser/parser.go | 97 ++++++++++++++++++++++++++++++++++ utils/{prompt => ui}/prompt.go | 25 +++++++-- 13 files changed, 242 insertions(+), 297 deletions(-) delete mode 100644 cmd/add.go delete mode 100644 cmd/bareinit.go delete mode 100644 cmd/include.go delete mode 100644 cmd/touch.go delete mode 100644 parser/parser.go create mode 100644 utils/git/gitDownload.go create mode 100644 utils/parser/parser.go rename utils/{prompt => ui}/prompt.go (58%) diff --git a/cmd/add.go b/cmd/add.go deleted file mode 100644 index 92b495c..0000000 --- a/cmd/add.go +++ /dev/null @@ -1,46 +0,0 @@ -package cmd - -import ( - "bare/parser" - "bare/styles" - "bare/utils/osutil" - "fmt" - "os" - "path/filepath" - - "github.com/spf13/cobra" -) - -func init() { - rootCmd.AddCommand(addCmd) -} - -var addCmd = &cobra.Command{ - Use: "add", - Short: "add you current bare to repo", - Long: "same as short for addCmd", - Run: func(cmd *cobra.Command, args []string) { - fmt.Println(styles.InitStyle.Render("Bare Add")) - addBare() - }, -} - -func addBare() { - recipePath := "./recipe.json" - parser.Parser(recipePath) - barePath := parser.BareObj.BarePath - for _, objPath := range parser.BareObj.Include { - sourcePath := filepath.Join(".", objPath) - destiPath := filepath.Join(barePath, objPath) - err := osutil.CopyFileDirectory(sourcePath, destiPath) - if err != nil { - fmt.Print(styles.InitError.Render("[Error] "), styles.AddFileStlyle.Render(objPath)) - fmt.Println("") - os.Exit(1) - } else { - fmt.Print(styles.InitSuccess.Render("[Success] "), styles.AddFileStlyle.Render(objPath)) - fmt.Println("") - } - } - -} diff --git a/cmd/bareinit.go b/cmd/bareinit.go deleted file mode 100644 index 8bf298e..0000000 --- a/cmd/bareinit.go +++ /dev/null @@ -1,73 +0,0 @@ -package cmd - -import ( - "bare/parser" - "bare/styles" - "bare/utils/osutil" - "encoding/json" - "fmt" - "io/ioutil" - "log" - "os" - "path/filepath" - - "github.com/spf13/cobra" -) - -func init() { - rootCmd.AddCommand(initCmd) -} - -var initCmd = &cobra.Command{ - Use: "init", - Short: "inits the project with bare", - Run: func(cmd *cobra.Command, args []string) { - fmt.Println(styles.InitStyle.Render("Bare Init")) - var bareName string - if len(args) == 0 { - fmt.Println(styles.InitError.Render("[Error] Provide bare name > bare init ")) - os.Exit(1) - } else { - bareName = args[0] - } - bareInit(bareName) - }, -} - -func bareInit(bareName string) { - - currDir, _ := os.Getwd() - homePath := os.Getenv("HOME") - - 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")) - os.Exit(1) - } else if !bareFolderExsists { - if !recipeFileExsists { - newBare := parser.Bare{ - BareName: bareName, - BarePath: filepath.Join(homePath, ".bare", bareName), - Include: []string{"recipe.json"}, - } - res, err := json.MarshalIndent(newBare, "", " ") - if err != nil { - log.Fatal(err) - } - recipeErr := ioutil.WriteFile(filepath.Join(currDir, "recipe.json"), res, 0644) - if recipeErr != nil { - fmt.Println(styles.InitError.Render("[Error] Cannot create recipe.json file")) - } else { - fmt.Println(styles.InitSuccess.Render("[Success] Created recipe.json")) - } - } - - if err := os.Mkdir(filepath.Join(homePath, ".bare", bareName), os.ModePerm); err != nil { - log.Fatal(err) - } else { - fmt.Println(styles.InitSuccess.Render("[Success] Created new bare "), bareName) - } - } -} diff --git a/cmd/include.go b/cmd/include.go deleted file mode 100644 index aeea46b..0000000 --- a/cmd/include.go +++ /dev/null @@ -1,47 +0,0 @@ -package cmd - -import ( - "bare/parser" - "bare/styles" - "bare/utils/osutil" - "fmt" - "os" - "path/filepath" - - "github.com/spf13/cobra" -) - -func init() { - rootCmd.AddCommand(includeCmd) -} - -var includeCmd = &cobra.Command{ - Use: "include", - Short: "include files to recipe.json `Include` field", - Run: func(cmd *cobra.Command, args []string) { - includeFiles(args) - }, -} - -func includeFiles(objects []string) { - parser.Parser("./recipe.json") - incMap := make(map[string]bool) - currDir, _ := os.Getwd() - for _, objs := range parser.BareObj.Include { - incMap[objs] = true - } - - for _, objs := range objects { - if incMap[objs] { - continue - } else { - if osutil.Exists(filepath.Join(currDir, objs)) { - parser.BareObj.Include = append(parser.BareObj.Include, objs) - fmt.Println(styles.InitSuccess.Render("[Add] " + objs)) - } else { - fmt.Println(styles.InitError.Render("[Not found] "), objs) - } - } - } - parser.UpdateRecipe() -} diff --git a/cmd/touch.go b/cmd/touch.go deleted file mode 100644 index 252e411..0000000 --- a/cmd/touch.go +++ /dev/null @@ -1,54 +0,0 @@ -package cmd - -import ( - "bare/parser" - "bare/styles" - "bare/utils/osutil" - "bufio" - "fmt" - "os" - "path/filepath" - "strings" - - "github.com/spf13/cobra" -) - -func init() { - rootCmd.AddCommand(touchCmd) -} - -var touchCmd = &cobra.Command{ - Use: "touch", - Short: "add a single boilerplate file from a bare", - Run: func(cmd *cobra.Command, args []string) { - touchFile(args[0], args[1]) - }, -} - -func touchFile(touchName, newName string) { - recipePath := "./recipe.json" - parser.Parser(recipePath) - touchMap := parser.BareObj.Touch - barePath := parser.BareObj.BarePath - currDir, _ := os.Getwd() - if touchMap[touchName] == "" { - fmt.Println(styles.InitError.Render("No such touch present")) - os.Exit(1) - } else { - if osutil.Exists(filepath.Join(currDir, newName)) { - fmt.Println("Already exsists !!") - inp := "n" // warning - reader := bufio.NewReader(os.Stdin) - fmt.Print(styles.Warning.Render("Do you want to override " + newName + " (y/N) > ")) - inp, _ = reader.ReadString('\n') - inp = strings.Trim(inp, " ") - if inp == "y" || inp == "Y" { - osutil.CopyFileDirectory(filepath.Join(barePath+"/"+touchMap[touchName]), filepath.Join(currDir, newName)) - } else { - os.Exit(0) - } - } else { - osutil.CopyFileDirectory(filepath.Join(barePath, touchMap[touchName]), filepath.Join(currDir, newName)) - } - } -} diff --git a/cmd/use.go b/cmd/use.go index 9c201a2..17450dc 100644 --- a/cmd/use.go +++ b/cmd/use.go @@ -1,11 +1,10 @@ package cmd import ( - "bare/styles" + "bare/utils/git" "bare/utils/osutil" - "fmt" - "os" - "path/filepath" + "bare/utils/parser" + "bare/utils/ui" "github.com/spf13/cobra" ) @@ -23,22 +22,42 @@ var useCmd = &cobra.Command{ }, } -func useBare(bareName, desti string) { - currDir, _ := os.Getwd() - barePath := filepath.Join(os.Getenv("HOME"), ".bare") +type NewTemplate struct { + Name string + Template string + Variables map[string]string + BarePath string +} - 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) - } +var TempObj NewTemplate - if osutil.Exists(filepath.Join(currDir, desti)) { - fmt.Println(styles.InitError.Render("File name already exsists")) - os.Exit(1) - } else { - osutil.CreateIfNotExists(filepath.Join(currDir, desti), 0755) - osutil.CopyDirectory(filepath.Join(barePath, bareName), filepath.Join(currDir, desti)) +func useBare(bareName, desti string) { + // TODO + // [x] Parse github repo + // [x] Get recipe.json + // [x] prompt preferences + // [x] Prompt Project name + // [x] Prompt template + // [x] Prompt variables + // [ ] If successful download .tar.gz + // [ ] extract .tar.gz + // [ ] If -C flag is present cache it. + // [ ] Execute the required template + // - Download the repo as .tar.gz + + user, repo, branch := parser.ParseGithubRepo(bareName) + parser.GetRecipe(user, repo, branch) + //ui.PromptStringNew("Enter project name", parser.BareObj.BareName) + TempObj.Name = ui.PromptString("Enter project name", parser.BareObj.BareName) + TempObj.Template = ui.PromptSelect("Select template", parser.BareObj.Variants) + + // Prompt variables + TempObj.Variables = make(map[string]string) + for k, e := range parser.BareObj.Variables { + varName := ui.PromptString(k, e) + TempObj.Variables[k] = varName } + osutil.MakeDownloadFolder() + git.DownloadZip("bare-cli", "vanilla-js-template", "main", parser.BareObj.BareName) } diff --git a/go.mod b/go.mod index 8c71840..dba69eb 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module bare go 1.17 -require github.com/spf13/cobra v1.2.1 +require ( + github.com/manifoldco/promptui v0.9.0 + github.com/spf13/cobra v1.2.1 +) require ( github.com/charmbracelet/lipgloss v0.4.0 @@ -40,9 +43,9 @@ require ( github.com/imdario/mergo v0.3.12 // indirect github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect - github.com/manifoldco/promptui v0.9.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/sergi/go-diff v1.1.0 // indirect github.com/xanzy/ssh-agent v0.3.0 // indirect + golang.org/x/term v0.0.0-20210422114643-f5beecf764ed // indirect gopkg.in/warnings.v0 v0.1.2 // indirect ) diff --git a/go.sum b/go.sum index 5877fae..c3b86a1 100644 --- a/go.sum +++ b/go.sum @@ -46,20 +46,24 @@ github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 h1:YoJbenK9C6 github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo= github.com/acomagu/bufpipe v1.0.3 h1:fxAGrHZTgQ9w5QqVItgzwj235/uYZYgbXitB+dLupOk= github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4= +github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/charmbracelet/lipgloss v0.4.0 h1:768h64EFkGUr8V5yAKV7/Ta0NiVceiPaV+PphaW1K9g= github.com/charmbracelet/lipgloss v0.4.0/go.mod h1:vmdkHvce7UzX6xkyf4cca8WlwdQ5RQr8fzta+xl7BOM= +github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= @@ -70,6 +74,7 @@ github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg= github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= @@ -84,12 +89,14 @@ github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0= github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4= github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E= github.com/go-git/go-billy/v5 v5.2.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0= github.com/go-git/go-billy/v5 v5.3.1 h1:CPiOUAzKtMRvolEKw+bG1PLRpT7D3LIs3/3ey4Aiu34= github.com/go-git/go-billy/v5 v5.3.1/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0= +github.com/go-git/go-git-fixtures/v4 v4.2.1 h1:n9gGL1Ct/yIw+nfsfr8s4+sbhT+Ncu2SubfXjIWgci8= github.com/go-git/go-git-fixtures/v4 v4.2.1/go.mod h1:K8zd3kDUAykwTdDCr+I0per6Y6vMiRR/nnVTBtavnB0= github.com/go-git/go-git/v5 v5.4.2 h1:BXyZu9t0VkbiHtqrsvdq39UDhGJTl1h55VW6CSC4aY4= github.com/go-git/go-git/v5 v5.4.2/go.mod h1:gQ1kArt6d+n+BGd+/B/I74HwRTLhth2+zti4ihgckDc= @@ -141,6 +148,7 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-github/v39 v39.1.0 h1:1vf4gM0D1e+Df2HMxaYC3+o9+Huj3ywGTtWc3VVYaDA= github.com/google/go-github/v39 v39.1.0/go.mod h1:C1s8C5aCC9L+JXIYpJM5GYytdX52vC1bLvHEF1IhBrE= @@ -207,15 +215,18 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA= github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg= +github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A= github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= @@ -249,8 +260,10 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= @@ -283,6 +296,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI= @@ -467,12 +481,15 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210923061019-b8560ed6a9b7 h1:c20P3CcPbopVp2f7099WLOqSNKURf30Z0uq66HpijZY= golang.org/x/sys v0.0.0-20210923061019-b8560ed6a9b7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210422114643-f5beecf764ed h1:Ei4bQjjpYUsS4efOUz+5Nz++IVkHk87n2zBA0NxBWc0= +golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -531,6 +548,7 @@ golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= @@ -640,6 +658,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= @@ -650,8 +669,10 @@ gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/parser/parser.go b/parser/parser.go deleted file mode 100644 index 94485bb..0000000 --- a/parser/parser.go +++ /dev/null @@ -1,51 +0,0 @@ -package parser - -import ( - "bare/utils/osutil" - "encoding/json" - "io/ioutil" - "log" - "os" -) - -type Bare struct { - BareName string - Version string - Include []string - BarePath string - Touch map[string]string -} - -var BareObj Bare - -func Parser(filePath string) { - data, err := ioutil.ReadFile(filePath) - if err != nil { - log.Fatal(err) - } - - // var bareObj Bare; - err = json.Unmarshal(data, &BareObj) - if err != nil { - log.Fatal(err) - } - - if BareObj.BareName == "" { - log.Fatal("Bare name not present") - } -} - -func UpdateRecipe() { - currDir, _ := os.Getwd() - recipePath := currDir + "/recipe.json" - updatedRecipe, err := json.MarshalIndent(BareObj, "", " ") - if err != nil { - log.Fatal(err) - } - if osutil.Exists(recipePath) { - err = ioutil.WriteFile(recipePath, updatedRecipe, 0644) - if err != nil { - log.Fatal(err) - } - } -} diff --git a/test/recipe.json b/test/recipe.json index c4c43a5..90ba08f 100644 --- a/test/recipe.json +++ b/test/recipe.json @@ -5,5 +5,14 @@ "Include" : [ "src/index.js", "Dockerfile" - ] -} \ No newline at end of file + ], + "Variants" : [ + "Var1", + "Var2" + ], + "Variables" : { + "AppName" : "example", + "x" : "x", + "y" : "y" + } +} diff --git a/utils/git/gitDownload.go b/utils/git/gitDownload.go new file mode 100644 index 0000000..601f6ff --- /dev/null +++ b/utils/git/gitDownload.go @@ -0,0 +1,37 @@ +package git + +import ( + "io" + "log" + "net/http" + "os" + "path/filepath" +) + +const zipDownloadUrl = "https://codeload.github.com/" + +func DownloadZip(user, repo, branch, fileName string) { + zipUrl := zipDownloadUrl + user + "/" + repo + "/" + "tar.gz/" + branch + + resp, err := http.Get(zipUrl) + if err != nil { + log.Fatal(err) + } + + defer resp.Body.Close() + + if resp.StatusCode != 200 { + return + } + homePath := os.Getenv("HOME") + out, err := os.Create(filepath.Join(homePath, ".bare", "tmp", fileName+".tar.gz")) + if err != nil { + log.Fatal(err) + } + defer out.Close() + + _, err = io.Copy(out, resp.Body) + if err != nil { + log.Fatal(err) + } +} diff --git a/utils/osutil/init.go b/utils/osutil/init.go index 2ac7715..d971845 100644 --- a/utils/osutil/init.go +++ b/utils/osutil/init.go @@ -3,14 +3,25 @@ package osutil import ( "log" "os" + "path/filepath" ) func MakeInitFolder() { // Get home directory homePath := os.Getenv("HOME") - if !Exists(homePath + "/.bare") { + if !Exists(filepath.Join(homePath, ".bare")) { if err := os.Mkdir(homePath+"/.bare", os.ModePerm); err != nil { log.Fatal(err) } } + +} + +func MakeDownloadFolder() { + homePath := os.Getenv("HOME") + if !Exists(filepath.Join(homePath, ".bare", "tmp")) { + if err := os.Mkdir(filepath.Join(homePath, ".bare", "tmp"), os.ModePerm); err != nil { + log.Fatal(err) + } + } } diff --git a/utils/parser/parser.go b/utils/parser/parser.go new file mode 100644 index 0000000..b08d908 --- /dev/null +++ b/utils/parser/parser.go @@ -0,0 +1,97 @@ +package parser + +import ( + "bare/utils/osutil" + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "strings" +) + +const raw_url = "https://raw.githubusercontent.com/" + +type Bare struct { + BareName string + Version string + BarePath string + Variants []string // Template name -> description (to be asked in prompt) + Variables map[string]string +} + +var BareObj Bare + +func Parser(filePath string) { + data, err := ioutil.ReadFile(filePath) + if err != nil { + log.Fatal(err) + } + + // var bareObj Bare; + err = json.Unmarshal(data, &BareObj) + if err != nil { + log.Fatal(err) + } + + if BareObj.BareName == "" { + log.Fatal("Bare name not present") + } +} + +func UpdateRecipe() { + currDir, _ := os.Getwd() + recipePath := currDir + "/recipe.json" + updatedRecipe, err := json.MarshalIndent(BareObj, "", " ") + if err != nil { + log.Fatal(err) + } + if osutil.Exists(recipePath) { + err = ioutil.WriteFile(recipePath, updatedRecipe, 0644) + if err != nil { + log.Fatal(err) + } + } +} + +func GetRecipe(user string, repo string, branch string) { + req_url := raw_url + user + "/" + repo + "/" + branch + "/recipe.json" + resp, err := http.Get(req_url) + if err != nil { + log.Fatal(err) + } + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatalln(err) + } + //Convert the body to type string + err = json.Unmarshal(body, &BareObj) + if err != nil { + log.Fatal(err) + } +} + +// Return user, repo, branch +func ParseGithubRepo(bareName string) (string, string, string) { + + userRepo := strings.Split(bareName, "/") + + if len(userRepo) <= 1 { + fmt.Println("Invalid Bare path") + os.Exit(-1) + } + + user := userRepo[0] + repo := userRepo[1] + + repoBranch := strings.Split(repo, "#") + branch := "main" + if len(repoBranch) > 1 { + branch = repoBranch[1] + } + + repo = repoBranch[0] + + return user, repo, branch +} diff --git a/utils/prompt/prompt.go b/utils/ui/prompt.go similarity index 58% rename from utils/prompt/prompt.go rename to utils/ui/prompt.go index 88c2d95..56517fd 100644 --- a/utils/prompt/prompt.go +++ b/utils/ui/prompt.go @@ -1,4 +1,4 @@ -package prompt +package ui import ( "errors" @@ -33,9 +33,10 @@ func Prompt() { fmt.Printf("You choose %q\n", result) } -func PromptString(label string) string { +func PromptString(label string, def string) string { prompt := promptui.Prompt{ - Label: label, + Label: label, + Default: def, } result, err := prompt.Run() @@ -45,3 +46,21 @@ func PromptString(label string) string { return result } + +func PromptSelect(label string, items []string) string { + + prompt := promptui.Select{ + Label: label, + Items: items, + HideHelp: true, + HideSelected: true, + } + + _, result, err := prompt.Run() + + if err != nil { + log.Fatal("Error encountered in Prompt.") + } + fmt.Printf("Template : %s\n", result) + return result +}