-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
198 lines (184 loc) · 4.84 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/nbebaw/archub/lib"
)
var Version = "No Version Provided"
func main() {
lib.CreateArchubDir()
archDir := lib.GetArchhubDirPath()
lib.DefineFlags()
switch {
// --info
case lib.InfoOfSystem:
lib.CpuInfo()
lib.HostInfo()
lib.MemoryInfo()
return
// -d, --delete
case lib.DeletePkg || lib.DeletePkgAlias:
lib.MainDeletePackage()
return
// -version || --version
case lib.ShowVersion:
if len(os.Args) > 2 {
fmt.Println("Error: Illegal option or argument.")
flag.Usage()
return
}
if flag.NArg() <= 0 {
fmt.Printf("archub v%s\n", Version)
return
}
fmt.Println(os.Args[1], "this flag do not need any argument")
flag.Usage()
return
// -help || --help
case lib.Help:
if len(os.Args) > 2 {
fmt.Println("Error: Illegal option or argument.")
flag.Usage()
return
}
lib.MainHelp()
return
// -c || --clean
case lib.CleanUp || lib.CleanUpAlias:
if len(os.Args) > 2 {
fmt.Println("Error: Illegal option or argument.")
flag.Usage()
return
}
lib.MainCleanUp()
return
// -s || --search
case lib.SearchPkg || lib.SearchPkgAlias:
if flag.NArg() > 0 {
argumentPkg := flag.Arg(0)
lib.MainSearch(argumentPkg)
} else {
fmt.Println("Please provide an argument with " + os.Args[1])
flag.Usage()
os.Exit(1)
}
return
// -i || --install
case lib.InstallPkg || lib.InstallPkgAlias:
if flag.NArg() > 0 {
argumentPkg := flag.Arg(0)
lib.BuildAndInstall(argumentPkg, archDir)
} else {
fmt.Println("Please provide an argument with " + os.Args[1])
flag.Usage()
os.Exit(1)
}
return
// -check || --check
case lib.CheckUpdatePkg:
if len(os.Args) > 2 {
fmt.Println("Options do not allowed together")
flag.Usage()
return
}
if flag.NArg() <= 0 {
updateExists := false
packageMap := lib.ListPackages()
for packageName, packageVersion := range packageMap {
newVersion, err := lib.GetLatestVersionFromAUR(packageName)
if err != nil {
fmt.Println(err)
}
if packageVersion != newVersion {
if len(newVersion) != 0 {
if !strings.Contains(newVersion, "${") {
fmt.Println("There is an update for the following packages:")
fmt.Printf("%s%s%s %s -> %s\n", lib.ColorLightRed, packageName, lib.ColorNone, packageVersion, newVersion)
updateExists = true
}
}
}
}
if !updateExists {
fmt.Println("There is nothing to do.")
fmt.Println("Have a good day!")
}
return
}
fmt.Println(os.Args[1], "this flag do not need any argument")
flag.Usage()
return
// -u || --u [package]
case lib.UpdatePkg || lib.UpdatePkgAlias:
if lib.UpdateAllPkg {
updateExists := false
var response string
// If both -u and --all flags are provided without an argument
if flag.NArg() == 0 {
packageMap := lib.ListPackages()
for packageName, packageVersion := range packageMap {
newVersion, err := lib.GetLatestVersionFromAUR(packageName)
if err != nil {
fmt.Println(err)
}
if packageVersion != newVersion {
fmt.Println("There is an update for the following packages:")
fmt.Printf("%s%s%s %s -> %s\n", lib.ColorRed, packageName, lib.ColorNone, packageVersion, newVersion)
if len(response) == 0 {
fmt.Print("Do you want to update all? (y/n)")
fmt.Scan(&response)
if response == "y" || response == "Y" {
lib.BuildAndInstall(packageName, archDir)
}
} else {
lib.BuildAndInstall(packageName, archDir)
}
updateExists = true
}
}
if !updateExists {
fmt.Println("There is no update")
}
} else {
fmt.Println("Please provide -u --all without an argument")
flag.Usage()
os.Exit(1) // Exit the program or handle the case based on your application's requirements
}
} else {
// If only -u flag is provided or -u flag is provided with an argument
if flag.NArg() > 0 {
argumentPkg := flag.Arg(0)
lib.BuildAndInstall(argumentPkg, archDir)
} else {
fmt.Println("Please provide an argument with -u or use -u --all")
os.Exit(1) // Exit the program or handle the case based on your application's requirements
}
}
return
// -l || --list
case lib.ListPkg || lib.ListPkgAlias:
if flag.NArg() <= 0 {
packageMap := lib.ListPackages()
for packageName, packageVersion := range packageMap {
fmt.Printf("%sPackage:%s %s, %sVersion:%s %s\n", lib.ColorGreen, lib.ColorNone, packageName, lib.ColorLightRed, lib.ColorNone, packageVersion)
}
return
}
fmt.Println(os.Args[1], "this flag do not need any argument")
flag.Usage()
return
}
// Show help when user give only the main options
if len(os.Args) == 1 {
flag.Usage()
return
}
// Show error and help when user write an illegal arguments/options
if flag.NArg() > 0 {
fmt.Println("Error: Illegal option or argument.")
flag.Usage()
return
}
}