-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
172 lines (135 loc) · 3.58 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package cmd
import (
"fmt"
"os"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/briandowns/spinner"
"github.com/kekda-py/create-pycord-app/utils"
"github.com/spf13/cobra"
)
var qs = []*survey.Question{
{
Name: "project-name",
Prompt: &survey.Input{Message: "What is the bot's name?", Default: "the greatest bot"},
Validate: survey.Required,
},
{
Name: "dir",
Prompt: &survey.Input{Message: "What should be the directory's name?", Default: "./"},
Validate: func(ans interface{}) error {
dir := ans.(string)
if dir == "." || dir == "./" {
return nil
}
if _, err := os.Stat("./" + dir); err != nil {
return nil
}
return fmt.Errorf("directory `%s` already exists", dir)
},
},
{
Name: "token",
Prompt: &survey.Input{
Message: "Bot's token?",
},
},
}
var rootCmd = &cobra.Command{
Use: "create-pycord-app",
Short: "Set up a pycord app by running a single command.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(utils.Colorize("blue", "Creating a new pycord app...\n"))
var (
Name string
Dir string
Token string
err error
)
utils.Ask(utils.Question{
Message: "What is the bot's name?",
Validate: func(value string) error {
return nil
},
Default: "the greatest bot",
}, &Name)
utils.Ask(utils.Question{
Message: "What should be the directory's name?",
Validate: func(value string) error {
if value == "." || value == "./" {
return nil
}
if _, err := os.Stat("./" + value); err != nil {
return nil
}
return fmt.Errorf("directory `%s` already exists", value)
},
Default: "./",
}, &Dir)
utils.Ask(utils.Question{
Message: "Bots token?",
Validate: func(value string) error {
return nil
},
Default: "",
}, &Token)
ans := struct {
Name string
Dir string
Token string
}{}
ans.Name = Name
ans.Dir = Dir
ans.Token = Token
fmt.Println()
s := spinner.New(spinner.CharSets[34], 100*time.Millisecond)
s.Suffix = utils.Colorize("blue", " Creating the directory...")
s.Start()
if err = utils.CreateDir(ans.Dir); err != nil {
fmt.Println(utils.Colorize("red", "Error: "+err.Error()))
os.Exit(1)
}
time.Sleep(1 * time.Second)
s.Stop()
s = spinner.New(spinner.CharSets[34], 100*time.Millisecond)
s.Suffix = utils.Colorize("blue", " Creating the files...")
s.Start()
token := ans.Token
if token == "" {
token = "TOKEN"
}
if err = utils.CreateFiles(ans.Dir, token); err != nil {
fmt.Println(utils.Colorize("red", "Error: "+err.Error()))
os.Exit(1)
}
time.Sleep(1 * time.Second)
s.Stop()
s = spinner.New(spinner.CharSets[34], 100*time.Millisecond)
s.Suffix = utils.Colorize("blue", " Initializing a github repository...")
s.Start()
if err = utils.InitializeGit(ans.Dir); err != nil {
fmt.Println(utils.Colorize("red", "Error: "+err.Error()))
os.Exit(1)
}
time.Sleep(1 * time.Second)
s.Stop()
s = spinner.New(spinner.CharSets[34], 100*time.Millisecond)
s.Suffix = utils.Colorize("blue", " Creating a virtual environment and installing packages...")
s.Start()
if err = utils.InitializeVenv(ans.Dir); err != nil {
fmt.Println(utils.Colorize("red", "Error: "+err.Error()))
os.Exit(1)
}
time.Sleep(1 * time.Second)
s.Stop()
fmt.Println(utils.Colorize("green", "Successfully created a pycord app!\n"))
fmt.Println("To run the app do -\n ")
fmt.Println(utils.Colorize("blue", "\tcd "+ans.Dir+"\n"+"\tenv/Scripts/activate\n"+"\tpython main.py"))
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}