Skip to content

Commit cde27f3

Browse files
committed
Now the program uses the flag std lib to parse the arguments
1 parent 65c1799 commit cde27f3

File tree

2 files changed

+97
-85
lines changed

2 files changed

+97
-85
lines changed

main.go

Lines changed: 71 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"flag"
45
"fmt"
56
"os"
67

@@ -15,74 +16,73 @@ var sh = func() command.Cmd {
1516
return cmd
1617
}()
1718

18-
var HelpStr = `
19-
Usage:
20-
#[bin] [argument] -[option]
21-
22-
Arguments:
23-
- help Print this text
24-
25-
- version Print the current version of the binary
26-
27-
- install Run all the nesesary functions to install completely Arch Linux
28-
29-
- pacstrap Only runs the pacstrap functions
30-
31-
- grub Only installs Grub
32-
33-
- newconfig Creates a new config overwriting the original
34-
35-
- part Open cfdisk to partitionate the install disk
36-
37-
- passwd Only changes the password of the new root
38-
39-
- mount Only mounts the disks in her routes
40-
41-
Options:
42-
Argument options will be applied before the config fil
43-
44-
-nopasswd Skip the passwd set
45-
46-
-nopacstrap Skip the pacstrap prosess
47-
48-
-nopart Skip the partitionating prosess (not open cfdisk)
49-
50-
-nogrub Don't install Grub
51-
52-
-noformat Don't format the partitions
53-
54-
-nomount Don't mount the partitions
55-
56-
-nopacmanconf Don't paste custom pacman config
19+
func main() {
20+
if len(os.Args) == 0 {
21+
fmt.Println("Not enough arguments")
22+
return
23+
}
24+
versionFlag := flag.Bool("version", false, "Show the version of the binary")
25+
helpFlag := flag.Bool("help", false, "Print the help message")
5726

58-
-nowifi Don't configure for wifi
27+
// Only individual functions
28+
installFlag := flag.Bool("install", false, "Run all the nesesary functions to install completely Arch Linux")
29+
pacstrapFlag := flag.Bool("pacstrap", false, "Only runs the pacstrap functions")
30+
grubFlag := flag.Bool("grub", false, "Only installs Grub")
31+
passwdFlag := flag.Bool("passwd", false, "Only changes the password of the new root")
32+
partFlag := flag.Bool("part", false, "Only changes the password of the new root")
33+
mountFlag := flag.Bool("mount", false, "Only mounts the disks in her routes")
34+
newConfigFlag := flag.Bool("newconfig", false, "Creates a new config overwriting the original")
5935

60-
-nofstab Don't generate a fstab for the new system
36+
flag.Parse()
6137

62-
-nokeymap Don't config the keymap for the new system
38+
if *newConfigFlag {
39+
data.NewYamlFile()
40+
err := sh.SetAndRun("vim " + data.Pfilename)
41+
if err != nil {
42+
src.Error("Error oppening vim.\n" + err.Error())
43+
}
44+
return
45+
}
6346

64-
-noreboot Don't reboot the system after the prosess
65-
`
47+
parserFlags := []bool{*installFlag, *pacstrapFlag, *grubFlag, *passwdFlag, *partFlag, *mountFlag}
48+
catchBadFlags := func(flags []bool) (HaveBadFlags bool) {
49+
var trueValues int
50+
for _, parser := range parserFlags {
51+
if parser {
52+
trueValues++
53+
}
54+
}
55+
if trueValues > 1 {
56+
return true
57+
} else {
58+
return false
59+
}
60+
}
6661

67-
func PrintHelp() {
68-
fmt.Print(HelpStr)
69-
}
62+
if catchBadFlags(parserFlags) {
63+
fmt.Println("There are arguments that cannot be used with others!")
64+
flag.PrintDefaults()
65+
return
66+
}
7067

71-
func main() {
72-
if len(os.Args) == 0 {
73-
fmt.Println("Not enough arguments")
68+
if *versionFlag {
69+
fmt.Println("...")
7470
return
7571
}
76-
switch os.Args[1] {
77-
case "version":
78-
fmt.Println()
79-
case "help":
80-
PrintHelp()
81-
case "passwd":
82-
src.ConfigRootPasswd()
83-
case "part":
84-
src.Partitioning()
85-
case "install":
72+
if *helpFlag {
73+
fmt.Println(`
74+
Usage:
75+
#[bin] [argument] -[option]`)
76+
flag.PrintDefaults()
77+
return
78+
}
79+
if *pacstrapFlag {
80+
src.Wifi()
81+
src.PacmanConf()
82+
src.Mount()
83+
src.Pacstrap()
84+
}
85+
if *installFlag {
8686
src.Partitioning()
8787
src.Wifi()
8888
src.PacmanConf()
@@ -95,22 +95,20 @@ func main() {
9595
src.ConfigRootPasswd()
9696
src.FinalCmds()
9797
src.Reboot()
98-
case "pacstrap":
99-
src.Wifi()
100-
src.PacmanConf()
101-
src.Mount()
102-
src.Pacstrap()
103-
case "grub":
98+
}
99+
if *passwdFlag {
100+
src.ConfigRootPasswd()
101+
}
102+
if *partFlag {
103+
src.Partitioning()
104+
}
105+
if *grubFlag {
104106
src.Mount()
105107
src.Fstab()
106108
src.Grub()
107-
case "mount":
109+
}
110+
if *mountFlag {
108111
src.Mount()
109-
case "newconfig":
110-
data.NewYamlFile()
111-
err := sh.SetAndRun("vim " + data.Pfilename)
112-
if err != nil {
113-
src.Error("Error oppening vim.\n" + err.Error())
114-
}
115112
}
113+
116114
}

src/src.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package src
22

33
import (
4+
"flag"
45
"fmt"
56
"io"
67
"os"
@@ -14,6 +15,20 @@ import (
1415
"github.com/gookit/color"
1516
)
1617

18+
var (
19+
NoPart *bool = flag.Bool("nopart", false, "Skip the partitionating prosess (not open cfdisk)")
20+
NoGrub = flag.Bool("nogrub", false, "Don't install Grub")
21+
NoWifi = flag.Bool("nowifi", false, "Don't configure for wifi")
22+
NoFstab = flag.Bool("nofstab", false, "Don't generate a fstab for the new system")
23+
NoKeymap = flag.Bool("nokeymap", false, "Don't config the keymap for the new system")
24+
NoReboot = flag.Bool("noreboot", false, "Don't reboot the system after the prosess")
25+
NoPasswd = flag.Bool("nopasswd", false, "Skip the passwd setting")
26+
NoPacstrap = flag.Bool("nopacstrap", false, "Skip the pacstrap process")
27+
NoPacmanConf = flag.Bool("nopacmanconf", false, "Don't copy the temporal pacman.conf for best performance in the pacstrap")
28+
NoMount = flag.Bool("nomount", false, "Don't mount the partitions")
29+
NoFormat = flag.Bool("noformat", false, "Don't format the partitions")
30+
)
31+
1732
var (
1833
// Functions
1934
sh = func() command.Cmd {
@@ -27,7 +42,6 @@ var (
2742
fmGreen = color.Green.Println
2843
fmYellow = color.Yellow.Println
2944
// Data
30-
args = strings.Join(os.Args, " ")
3145
conf = data.GetYamldata()
3246
partitions = conf.Partitions
3347
wifi = conf.Wifi
@@ -70,7 +84,7 @@ func Warn(err string) {
7084
}
7185

7286
func Partitioning() {
73-
if strings.Contains(args, "-nopart") {
87+
if *NoPart {
7488
return
7589
}
7690
var err error
@@ -81,7 +95,7 @@ func Partitioning() {
8195
}
8296

8397
func Format() {
84-
if strings.Contains(args, "-noformat") {
98+
if *NoFormat {
8599
return
86100
}
87101
var err error
@@ -135,7 +149,7 @@ func Format() {
135149
}
136150
}
137151
func Mount() {
138-
if strings.Contains(args, "-nomount") {
152+
if *NoMount {
139153
return
140154
}
141155
var err error
@@ -209,7 +223,7 @@ func Mount() {
209223
}
210224

211225
func PacmanConf() {
212-
if strings.Contains(args, "-nopacmanconf") {
226+
if *NoPacmanConf {
213227
return
214228
}
215229
if check_pacman_cfg, _ := file.CheckFile("pacman.conf"); conf.CustomPacmanConfig && !check_pacman_cfg {
@@ -235,7 +249,7 @@ func PacmanConf() {
235249
}
236250

237251
func Wifi() {
238-
if strings.Contains(args, "-nowifi") {
252+
if *NoWifi {
239253
return
240254
}
241255
var err error
@@ -271,7 +285,7 @@ func Wifi() {
271285
}
272286

273287
func Pacstrap() {
274-
if strings.Contains(args, "-nopacstrap") {
288+
if *NoPacstrap {
275289
return
276290
}
277291
if !conf.PacstrapSkip {
@@ -288,7 +302,7 @@ func Pacstrap() {
288302
}
289303

290304
func Fstab() {
291-
if strings.Contains(args, "-nofstab") {
305+
if *NoFstab {
292306
return
293307
}
294308
fmYellow("Generating Fstab...")
@@ -302,7 +316,7 @@ func Fstab() {
302316
}
303317

304318
func Grub() {
305-
if strings.Contains(args, "-nogrub") {
319+
if *NoGrub {
306320
return
307321
}
308322
var err error
@@ -334,7 +348,7 @@ func Grub() {
334348
}
335349

336350
func Keymap() {
337-
if strings.Contains(args, "-nokeymap") {
351+
if *NoKeymap {
338352
return
339353
}
340354
sh.UseBashShell(true)
@@ -354,7 +368,7 @@ func Keymap() {
354368
}
355369

356370
func ConfigRootPasswd() {
357-
if strings.Contains(args, "-nopasswd") {
371+
if *NoPasswd {
358372
return
359373
}
360374
fmYellow("Setting the root password:", conf.Password)
@@ -395,7 +409,7 @@ func FinalCmds() {
395409
}
396410

397411
func Reboot() {
398-
if strings.Contains(args, "-noreboot") {
412+
if *NoReboot {
399413
return
400414
}
401415
var err error

0 commit comments

Comments
 (0)