-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
147 lines (130 loc) · 2.65 KB
/
cli.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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"github.com/notwithering/argo"
)
const (
ps1 string = "> "
)
func cli() error {
var license string = "Assemblix Server"
func() {
file, err := os.Open("LICENSE")
if err != nil {
logWarning(err)
return
}
scanner := bufio.NewScanner(file)
var line uint8 = 1
for scanner.Scan() {
if line == 3 {
license += " " + scanner.Text()
break
}
line++
}
}()
fmt.Println(license)
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print(ps1)
in, err := reader.ReadString('\n')
if err != nil {
fmt.Println(err)
continue
}
in = strings.TrimRight(in, "\n")
args, invalid := argo.Parse(in)
if invalid {
fmt.Println("argo: syntax error")
continue
}
if len(args) == 0 {
continue
}
switch args[0] {
case "help":
fmt.Println(" help : show this menu")
fmt.Println(" clear, cls : clear the screen")
fmt.Println(" db : open sqlite3")
fmt.Println(" useradd : create a new user : useradd [options...] username password")
fmt.Println(" quit, exit : exit the program")
case "":
case "quit", "exit":
return nil
case "clear", "cls":
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
fmt.Printf("%s: %s", args[0], err)
continue
}
case "db":
cmd := exec.Command("sqlite3", "data.db")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Println(err)
continue
}
case "useradd":
var cash int = joinCash
var admin bool = false
var username string
var password string
var defaults int
useradd:
for i := 1; i < len(args); i++ {
switch args[i] {
case "-c", "--cash":
if args[i+1] == "" {
fmt.Printf("useradd: options %s: requires parameter\n", args[i])
break
}
i++
cashBuf, err := strconv.Atoi(args[i])
if err != nil {
fmt.Println(err)
break
}
cash = cashBuf
case "-a", "--admin":
admin = true
case "":
break useradd
default:
defaults++
switch defaults {
case 1:
username = args[i]
case 2:
password = args[i]
}
}
}
if defaults < 2 {
fmt.Println("useradd: not enough arguments")
continue
}
if defaults > 2 {
fmt.Println("useradd: too many arguments")
continue
}
id, token, err := createUser(username, password, cash, admin, db)
if err != nil {
fmt.Println(err)
continue
}
fmt.Printf("id: %d\n", id)
fmt.Printf("token: %s\n", token)
default:
fmt.Printf("%s: command not found\n", args[0])
}
}
}