diff --git a/cmd/app/new.go b/cmd/app/new.go index a89a3dd7..b11494a6 100755 --- a/cmd/app/new.go +++ b/cmd/app/new.go @@ -13,6 +13,12 @@ import ( "github.com/spf13/cobra" ) +var newOpts = &options.NewOptions{ + CreateRepo: false, + RepoName: "", + IsPrivate: false, +} + func NewCMD() *cobra.Command { cmd := &cobra.Command{ Use: "new", @@ -27,6 +33,10 @@ func NewCMD() *cobra.Command { } new.New(opts) + + if newOpts.CreateRepo { + new.CreateRepo(newOpts, opts.BotName) + } } else { cmd.Help() } @@ -64,5 +74,9 @@ func NewCMD() *cobra.Command { }, } + cmd.Flags().BoolVarP(&newOpts.CreateRepo, "repo", "r", false, "Create a new github repository under your account") + cmd.Flags().BoolVarP(&newOpts.IsPrivate, "private", "p", false, "Make your repository private") + cmd.Flags().StringVarP(&newOpts.RepoName, "repo-name", "n", "", "Name of the repository, if not specified, it will be the same as the bot name") + return cmd } diff --git a/cmd/app/tools.go b/cmd/app/tools.go index aeff3091..7f6de0b8 100644 --- a/cmd/app/tools.go +++ b/cmd/app/tools.go @@ -60,9 +60,3 @@ func CheckDir() { panic(constants.FAIL_FOREGROUND.Render("You need to run this command in your bot directory")) } } - -var addCmd = func(cmd, cmdx *cobra.Command) *cobra.Command { - cmd.AddCommand(cmdx) - - return cmd -} diff --git a/internal/options/opts.go b/internal/options/opts.go index 156a92f8..522d15f0 100755 --- a/internal/options/opts.go +++ b/internal/options/opts.go @@ -12,6 +12,12 @@ type CommonOptions struct { BotName string } +type NewOptions struct { + CreateRepo bool + RepoName string + IsPrivate bool +} + type TokenAddOptions struct { BotName string Discord bool diff --git a/internal/pipes/new/create_repo.go b/internal/pipes/new/create_repo.go new file mode 100644 index 00000000..b9d1746b --- /dev/null +++ b/internal/pipes/new/create_repo.go @@ -0,0 +1,69 @@ +package new + +import ( + "fmt" + "log" + "os" + "os/exec" + "runtime" + + "github.com/abdfnx/botway/internal/options" +) + +func createRepoWindows(botName string, isPrivate bool) string { + privateFlag := "" + + if isPrivate { + privateFlag = "--private" + } + + return fmt.Sprintf(` + $username = botway gh get-username + git init + botway gh-repo create %s -d "My Awesome bot" %s -y + git add . + git commit -m "new botway bot project" + git branch -M main + git push -u origin main + `, botName, privateFlag) +} + +func createRepoUnix(botName string, isPrivate bool) string { + repoStatus := "--public" + + if isPrivate { + repoStatus = "--private" + } + + return fmt.Sprintf(` + username=$(botway gh get-username) + git init + botway gh-repo create %s -d "My Awesome bot" %s -y + git add . + git commit -m "new botway bot project" + git branch -M main + git push -u origin main + `, botName, repoStatus) +} + +func CreateRepo(o *options.NewOptions, botName string) { + if o.RepoName == "" { + o.RepoName = botName + } + + createRepoCmd := exec.Command("bash", "-c", createRepoUnix(o.RepoName, o.IsPrivate)) + + if runtime.GOOS == "windows" { + createRepoCmd = exec.Command("powershell.exe", "-Command", createRepoWindows(o.RepoName, o.IsPrivate)) + } + + createRepoCmd.Dir = botName + createRepoCmd.Stdin = os.Stdin + createRepoCmd.Stdout = os.Stdout + createRepoCmd.Stderr = os.Stderr + err := createRepoCmd.Run() + + if err != nil { + log.Printf("error: %v\n", err) + } +}