diff options
| author | n1c00o <34602094+n1c00o@users.noreply.github.com> | 2021-10-16 22:26:09 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-16 22:26:09 +0200 |
| commit | e58e816ceb8caa3c77dd98a952761b7e7f05b6cb (patch) | |
| tree | ca5c9409986ff538e7a674473c1d642627d1055c /novactl/cmd/init.go | |
| parent | 03908129599260587fe7b9fd8254d28ad50b8714 (diff) | |
| parent | b94b0552f81e667bec31352901bbc8c76f1b4216 (diff) | |
Merge branch 'main' into nats-structs-discord-gateway
Diffstat (limited to 'novactl/cmd/init.go')
| -rw-r--r-- | novactl/cmd/init.go | 229 |
1 files changed, 140 insertions, 89 deletions
diff --git a/novactl/cmd/init.go b/novactl/cmd/init.go index 47a5559..084c2da 100644 --- a/novactl/cmd/init.go +++ b/novactl/cmd/init.go @@ -1,89 +1,140 @@ -package cmd
-
-import (
- "fmt"
- "io/fs"
- "io/ioutil"
- "os"
- "path/filepath"
- "strings"
-
- "github.com/TwinProduction/go-color"
- "github.com/go-git/go-git/v5"
- "github.com/spf13/cobra"
-)
-
-var (
- InitializeCommand = &cobra.Command{
- Use: "init",
- Short: "Initialize a new nova based project",
- Run: initNovaRepo,
- }
-)
-
-func initNovaRepo(cmd *cobra.Command, args []string) {
- url := "https://github.com/libgit2/git2go.git"
- path := ""
- name := "test"
-
- if name == "" {
- fmt.Print(
- color.Ize(color.Red, "A name must be specified"),
- )
- return
- }
-
- if path == "" {
- path = fmt.Sprintf("./%s", name)
- }
- cw, err := os.Getwd()
- if err != nil {
- return
- }
- path = filepath.Join(cw, path)
-
- fmt.Println(
- color.Ize(color.Green, fmt.Sprintf("Initializing a new nova project at %s", path)),
- )
- fmt.Println(
- color.Ize(color.Gray, fmt.Sprintf("Using the %s template", url)),
- )
-
- // clone the repo
- _, err = git.PlainClone(path, false, &git.CloneOptions{
- URL: url,
- Progress: os.Stdout,
- })
-
- if err != nil {
- fmt.Println(
- color.Ize(color.Red, fmt.Sprintf("Failed to initialize the repository: %s", err.Error())),
- )
- return
- }
-
- fmt.Println(
- color.Ize(color.Green, "Cloned the repository..."),
- )
-
- filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
- if d.IsDir() || err != nil {
- return nil
- }
-
- read, err := ioutil.ReadFile(path)
- if err != nil {
-
- }
- content := strings.ReplaceAll(string(read), "%PROJECT%", name)
-
- err = ioutil.WriteFile(path, []byte(content), 0)
-
- if err != nil {
- return err
- }
- return nil
- })
-
- err = os.RemoveAll(filepath.Join(path, ".git"))
-}
+package cmd + +import ( + "fmt" + "github.com/go-git/go-git/v5/config" + "github.com/rs/zerolog/log" + "io/fs" + "io/ioutil" + "os" + "path/filepath" + "strings" + + "github.com/go-git/go-git/v5" + "github.com/spf13/cobra" +) + +var ( + InitializeCommand = createInitCommand() + Flavour *string = nil + Name *string = nil +) + +func createInitCommand() *cobra.Command { + command := cobra.Command{ + Use: "init [path]", + Short: "Initialize a new nova based project", + Run: initNovaRepo, + } + + Flavour = command.Flags().String("flavour", "javascript", "the flavour of template to use") + Name = command.Flags().String("name", "", "the name of the project") + + return &command +} + +func determineTemplate() string { + if strings.HasPrefix(*Flavour, "http") || strings.HasPrefix(*Flavour, "ssh") { + return *Flavour + } else { + return fmt.Sprintf("https://github.com/discordnova/template-%s.git", *Flavour) + } +} + +func initNovaRepo(cmd *cobra.Command, args []string) { + + url := determineTemplate() + if len(args) == 0 { + log.Error().Msg("A path must be specified") + os.Exit(1) + } + + path := strings.Join(args, " ") + name := path + + // if the user specified a name different from the folder name + if *Name != "" { + name = *Name + } + + cw, err := os.Getwd() + if err != nil { + return + } + // we get the absolute path of the folder + path = filepath.Join(cw, path) + log.Info().Msgf("Initializing a %s at %s using template %s", name, path, url) + + // clone the repo + _, err = git.PlainClone(path, false, &git.CloneOptions{ + URL: url, + }) + + if err != nil { + log.Err(err).Msg("Failed to initialize the repository") + os.Exit(1) + } + + log.Info().Msg("Successfully cloned the template") + + // replace all the instances of "%PROJECT%" with the project name + err = filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error { + if d.IsDir() || err != nil { + return nil + } + + read, err := ioutil.ReadFile(path) + if err != nil { + + } + content := strings.ReplaceAll(string(read), "%PROJECT%", name) + + err = ioutil.WriteFile(path, []byte(content), 0) + + if err != nil { + return err + } + return nil + }) + + if err != nil { + log.Err(err).Msgf("Failed to bootstrap the project") + // we try to remove the folder + _ = os.Remove(path) + os.Exit(1) + } + // we remove the git folder + err = os.RemoveAll(filepath.Join(path, ".git")) + + repo, err := git.PlainInit(path, false) + if err != nil { + log.Err(err).Msgf("Failed to initialize the git repository") + os.Exit(1) + } + + err = repo.CreateBranch(&config.Branch{ + Name: "main", + }) + if err != nil { + log.Err(err).Msgf("Failed to create the main branch") + os.Exit(1) + } + + tree, err := repo.Worktree() + if err != nil { + log.Err(err).Msgf("Failed to get worktree") + os.Exit(1) + } + _, err = tree.Add(".") + if err != nil { + log.Err(err).Msgf("Failed to index the files") + os.Exit(1) + } + _, err = tree.Commit("first commit", &git.CommitOptions{}) + if err != nil { + log.Err(err).Msgf("Failed to index the first commit") + os.Exit(1) + } + + log.Info().Msgf("Created a new repository at %", path) +} |
