Skip to content

Commit

Permalink
add --create-repo, --repo-name and --private flags to new cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
abdfnx committed Jul 4, 2022
1 parent 633548e commit 363d8d7
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
14 changes: 14 additions & 0 deletions cmd/app/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -27,6 +33,10 @@ func NewCMD() *cobra.Command {
}

new.New(opts)

if newOpts.CreateRepo {
new.CreateRepo(newOpts, opts.BotName)
}
} else {
cmd.Help()
}
Expand Down Expand Up @@ -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
}
6 changes: 0 additions & 6 deletions cmd/app/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 6 additions & 0 deletions internal/options/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions internal/pipes/new/create_repo.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 363d8d7

Please sign in to comment.