-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
executable file
·175 lines (160 loc) · 5.08 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
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
173
174
175
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/snowmerak/prew/color"
"gopkg.in/alecthomas/kingpin.v2"
)
func main() {
app := kingpin.New("prew", "a python management tool")
init := app.Command("init", "Initialize a new project")
initPath := init.Arg("path", "Path to the project").Required().String()
search := app.Command("search", "Search for a package's versions")
searchPackage := search.Arg("package", "Package to search for").Required().String()
install := app.Command("install", "Install a package")
installName := install.Arg("name", "Name of the package").Required().String()
installVersion := install.Arg("version", "Version of the package").String()
list := app.Command("list", "List all installed packages")
remove := app.Command("remove", "Remove a package")
removeName := remove.Arg("name", "Name of the package").Required().String()
removeYes := remove.Flag("yes", "Remove without confirmation").Short('y').Bool()
removeDeps := remove.Flag("deps", "Remove dependencies").Short('d').Bool()
run := app.Command("run", "Run python code")
restore := app.Command("restore", "Restore a package")
restorePath := restore.Arg("path", "Path to the package").Required().String()
makes := app.Command("make", "Make something")
makesDockerfile := makes.Flag("dockerfile", "Make dockerfile").Short('d').Bool()
tidy := app.Command("tidy", "Tidy up the project")
tidyYes := tidy.Flag("yes", "Auto remove unused packages").Short('y').Bool()
checkType := app.Command("check", "Check type of a python code")
checkTypeAll := checkType.Flag("all", "Check all files").Short('a').Bool()
checkTypeName := checkType.Arg("name", "Name of the file").String()
app.Version("0.2.0")
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case init.FullCommand():
log.Println("Initializing project in current path")
if err := initProjectToDirectory(*initPath); err != nil {
log.Fatal(err)
}
log.Println(color.Green + "success" + color.Reset)
case search.FullCommand():
log.Println("Searching for package " + *searchPackage)
if err := searchPackageVersion(*searchPackage); err != nil {
log.Fatal(err)
}
log.Println(color.Green + "success" + color.Reset)
case install.FullCommand():
log.Println("Installing package", *installName)
path, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
spec, err := readSpecFromPath(path)
if err != nil {
log.Fatal(err)
}
if err := appendDependencyToSpec(&spec, *installName, *installVersion); err != nil {
log.Fatal(err)
}
if err := writeSpecToPath(path, spec); err != nil {
log.Fatal(err)
}
log.Println(color.Green + "success" + color.Reset)
case list.FullCommand():
log.Println("Listing all installed packages")
if err := printPackages(); err != nil {
log.Fatal(err)
}
case remove.FullCommand():
log.Println("Removing package")
path, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
spec, err := readSpecFromPath(path)
if err != nil {
log.Fatal(err)
}
if err := subductDependencyFromSpec(&spec, *removeName, *removeYes, *removeDeps); err != nil {
log.Fatal(err)
}
if err := writeSpecToPath(path, spec); err != nil {
log.Fatal(err)
}
log.Println(color.Green + "success" + color.Reset)
case run.FullCommand():
spec, err := readSpecFromPath(".")
if err != nil {
log.Fatal(err)
}
if err := runPythonCode(spec); err != nil {
log.Fatal(err)
}
case restore.FullCommand():
log.Println("Restore project")
if err := restoreProject(*restorePath); err != nil {
log.Fatal(err)
}
log.Println(color.Green + "success" + color.Reset)
case tidy.FullCommand():
log.Println("Tidying project")
if err := tidyUpProject(".", *tidyYes); err != nil {
log.Fatal(err)
}
log.Println(color.Green + "success" + color.Reset)
case makes.FullCommand():
spec, err := readSpecFromPath(".")
if err != nil {
log.Fatal(err)
}
if *makesDockerfile {
log.Println("Making Dockerfile")
data := convertSpecToDockerfile(&spec)
if err := os.WriteFile("./dockerfile", []byte(data), 0644); err != nil {
log.Fatal(err)
}
log.Println(color.Green + "success" + color.Reset)
}
case checkType.FullCommand():
// path, err := os.Getwd()
// if err != nil {
// log.Fatal(err)
// }
path := "."
path = filepath.Join(path, "src")
if err := installMypy(path); err != nil {
log.Fatal(err)
}
if *checkTypeAll {
log.Println("Checking all files")
em, err := checkTypeFiles(path)
if err != nil {
log.Fatal(err)
}
for _, el := range em {
for _, e := range el {
e = strings.ReplaceAll(e, "error", color.Red+"error"+color.Reset)
e = strings.ReplaceAll(e, "Success", color.Green+"Success"+color.Reset)
fmt.Println(e)
}
}
}
if *checkTypeName != "" {
log.Println("Checking file", *checkTypeName)
el, err := checkTypeFile(path, *checkTypeName)
if err != nil {
log.Fatal(err)
}
for _, e := range el {
e = strings.ReplaceAll(e, "error", color.Red+"error"+color.Reset)
e = strings.ReplaceAll(e, "Success", color.Green+"Success"+color.Reset)
fmt.Println(e)
}
}
default:
kingpin.Usage()
}
}