-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (74 loc) · 1.49 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"sort"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
)
func main() {
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Aliases: []string{"v"},
Usage: "Print Version.",
}
app := &cli.App{
Name: "discordgo",
Version: "v 1.0.0",
Commands: []*cli.Command{
{
Name: "create",
Aliases: []string{"c"},
Usage: "Create A new Project",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
},
},
Action: CreateProject,
},
},
}
sort.Sort(cli.CommandsByName(app.Commands))
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func CreateFile(file_name string) error {
file, err := os.Create(file_name)
if err != nil {
return err
}
defer file.Close()
return nil
}
func CreateDir(name string) error {
err := os.Mkdir(name, 0750)
if err != nil {
return err
}
return nil
}
func CreateProject(ctx *cli.Context) error {
name := ctx.String("name")
err := CreateDir(name)
file, err := os.Create("./" + name + "/main.go")
if err != nil {
return err
}
color.Red("go mod init " + name + ".me command runnings")
command := exec.Command("go", "mod", "init", name+".me")
command.Dir = name
cmdOut, err := command.CombinedOutput()
if err != nil {
return err
}
fmt.Println(string(cmdOut))
err = os.WriteFile("./"+name+"/main.go", []byte("func main() {\n\treturn nil\n}"), 0660)
fmt.Println("Create Project.", file.Name())
return nil
}