/
main.go
186 lines (162 loc) · 3.74 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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
var reader *bufio.Reader
type User struct {
id int
username string
email string
age int
}
var users map[int]User
var id int
func crateUser() {
clearConsole()
fmt.Print("Ingresa Username: ")
username := readLine()
fmt.Print("Ingresa Email: ")
email := readLine()
fmt.Print("Ingresa Edad: ")
age := checkParseInt(readLine())
id++
user := User{id, username, email, age}
users[id] = user
fmt.Print("\n>>>Usuario Creado Exitosamente\n")
}
func readUser() {
clearConsole()
if _, ok := users[1]; !ok {
fmt.Println("No hay usuarios creados")
} else {
fmt.Print("Listado de Usuarios\n\n")
for id, user := range users {
fmt.Println("ID:", id, "/ Username:", user.username, "/ Email:", user.email, "/ Age:", user.age)
}
}
}
func updateUser() {
clearConsole()
fmt.Print("Ingresa el id del Usuario a Eliminar: ")
id := checkParseInt(readLine())
if user, ok := users[id]; ok {
clearConsole()
fmt.Println("Seleccione el campo que desea Actualizar del usuario:", user.username)
fmt.Println("1) Username")
fmt.Println("2) Email")
fmt.Println("3) Age")
fmt.Println("4) Todos los campos")
fmt.Println("5) Cancelar")
opcion := readLine()
if opcion == "5" {
clearConsole()
fmt.Print("Actualizacion Cancelada\n\n")
main()
}
switch opcion {
case "1":
clearConsole()
fmt.Print("Ingrese el nuevo Username: ")
user.username = readLine()
users[id] = user
case "2":
clearConsole()
fmt.Print("Ingrese el nuevo Email: ")
user.email = readLine()
users[id] = user
case "3":
clearConsole()
fmt.Print("Ingrese la nueva Age: ")
age := checkParseInt(readLine())
user.age = age
users[id] = user
case "4":
clearConsole()
fmt.Print("Ingrese el nuevo Username: ")
user.username = readLine()
fmt.Print("Ingrese el nuevo Email: ")
user.email = readLine()
fmt.Print("Ingrese la nueva Age: ")
age := checkParseInt(readLine())
user.age = age
users[id] = user
}
fmt.Print(">>>Usuario Actualizado Extitosamente\n\n")
} else {
clearConsole()
fmt.Print("El usuario con ID: '", id, "' no existe\n\n")
}
}
func deleteUser() {
clearConsole()
fmt.Print("Ingresa el id del Usuario a Eliminar: ")
id := checkParseInt(readLine())
if _, ok := users[id]; ok {
delete(users, id)
fmt.Print(">>>Usuario Eliminado Extitosamente\n\n")
} else {
fmt.Print("El usuario con id:", id, "no existe\n\n")
}
}
func readLine() string {
if opcion, err := reader.ReadString('\n'); err != nil {
panic("No es posible obtener el valor")
} else {
return strings.TrimSuffix(opcion, "\n")
}
}
func checkParseInt(readline string) int {
if data, err := strconv.Atoi(readline); err != nil {
panic("No se puede convertir un String a un Entero")
} else {
return data
}
}
func clearConsole() {
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
}
func main() {
users = make(map[int]User)
reader = bufio.NewReader(os.Stdin)
for {
fmt.Print("\nCRUD CLI\n\n")
fmt.Println("1) Crear")
fmt.Println("2) Listar")
fmt.Println("3) Actualizar")
fmt.Println("4) Eliminar")
fmt.Print("5) Salir del Sistema\n\n")
fmt.Print("Seleccione una Opcion ")
opcion := readLine()
if opcion == "5" || opcion == "exit" {
break
}
switch opcion {
case "1", "Crear", "crear":
crateUser()
case "2", "Listar", "listar":
readUser()
case "3", "Actualizar", "actualizar":
updateUser()
case "4", "Eliminar", "eliminar":
deleteUser()
default:
clearConsole()
fmt.Println("Funcion no valida")
}
}
clearConsole()
fmt.Print("Saliendo del Sitema ")
for i := 3; i > 0; i-- {
fmt.Print(".")
time.Sleep(time.Millisecond * 800)
}
fmt.Println("\nSalida Exitosa, Vuelva Pronto!")
}