This repository has been archived by the owner on Nov 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
183 lines (161 loc) · 3.83 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
176
177
178
179
180
181
182
183
package main
import (
// "bytes"
"flag"
"os"
"os/exec"
"runtime"
"github.com/theplant/pak/check"
"github.com/theplant/pak/core"
. "github.com/theplant/pak/share"
"github.com/wsxiaoys/terminal/color"
)
var (
// getLatest bool
force bool
skipUncleanPkgs bool
gogeterror bool
concurrent bool
)
func init() {
flag.Usage = func() {
spaces := " "
color.Printf("@gPackages Management Tool Pak.\n@wUsage:\n")
color.Printf("%spak init\n", spaces)
color.Printf("%spak [-sfe] get [package]\n", spaces)
color.Printf("%spak [-se] update [package]\n", spaces)
color.Printf("%spak open [package]\n", spaces)
color.Printf("%spak list\n", spaces)
color.Printf("%spak version\n", spaces)
color.Printf("%spak unpak\n", spaces)
// fmt.Printf("%spak scan\n", spaces)
flag.PrintDefaults()
}
// flag.BoolVar(&getLatest, "u", false, "Download the lastest revisions from remote repo before checkout.")
flag.BoolVar(&force, "f", false, "Force pak to remove pak branch.")
flag.BoolVar(&skipUncleanPkgs, "s", false, "Left out unclean packages.")
flag.BoolVar(&gogeterror, "e", false, "Print out go get error.")
flag.BoolVar(&concurrent, "c", false, "Enable concurrent download.")
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
flag.Parse()
core.PrintGoGetError = gogeterror
switch flag.Arg(0) {
case "init":
initPak()
case "get":
getPakPkgs()
case "update":
updatePakPkgs()
case "open":
openPkgWithPakEditor()
case "list":
listPakfilePkgs()
case "check":
docheck()
case "unpak":
checkToMaster()
case "version":
color.Println("@g1.5.0")
default:
flag.Usage()
}
}
func initPak() {
err := core.Init()
if err != nil {
color.Printf("@r%+v@w\n", err)
os.Exit(1)
}
}
func getPakPkgs() {
err := core.Get(PakOption{
PakMeter: flag.Args()[1:],
UsePakfileLock: true,
Force: force,
SkipUncleanPkgs: skipUncleanPkgs,
Verbose: true,
Concurrent: concurrent,
})
if err != nil {
color.Printf("@r%s\n", err)
color.Println("Pak Failed.")
os.Exit(1)
}
}
func checkToMaster() {
err := core.Remove(PakOption{
UsePakfileLock: true,
Force: true,
SkipUncleanPkgs: true,
PakMeter: flag.Args()[1:],
Verbose: true,
})
if err != nil {
color.Printf("@r%s\n", err)
os.Exit(1)
}
}
func updatePakPkgs() {
err := core.Get(PakOption{
UsePakfileLock: false,
Force: true,
SkipUncleanPkgs: skipUncleanPkgs,
PakMeter: flag.Args()[1:],
Verbose: true,
})
if err != nil {
color.Printf("@r%s\n", err)
os.Exit(1)
}
}
func openPkgWithPakEditor() {
pakEditor := os.Getenv("PAK_OPEN_EDITOR")
if pakEditor == "" {
color.Printf("@rPAK_OPEN_EDITOR is Not Configured.@w")
color.Println(" Please configure it like below in your ~/.bash_profile or anywhere that is accessible to pak.")
color.Println(" @gexport PAK_OPEN_EDITOR={mate or whatever editor that you like}@w")
return
}
name := flag.Args()[1]
pkg, err := core.FindPackage(name)
if err != nil {
color.Printf("@r%s\n", err)
return
}
if pkg.Name == "" {
color.Printf("@rPackage %s Not Exist.\n", name)
return
}
cmd := exec.Command(pakEditor, Gopath+"/src/"+pkg.Name)
err = cmd.Run()
if err != nil {
color.Printf("@r%s@w\n", err)
os.Exit(1)
}
}
func listPakfilePkgs() {
pakInfo, _, err := core.GetPakInfo(core.GpiParams{
Type: "10",
Path: "",
DeepParse: false,
WithBasicDependences: true,
})
if err != nil {
color.Printf("@r%s\n", err)
return
}
var allPakPkgs []core.PakPkg
for _, pkgCfg := range pakInfo.Packages {
pakPkg := core.NewPakPkg(pkgCfg)
allPakPkgs = append(allPakPkgs, pakPkg)
}
color.Println("All Packages Depended In this Package:")
for _, pkg := range allPakPkgs {
color.Printf("@g %s\n", pkg.Name)
}
}
func docheck() {
check.Check()
}