Skip to content

Instantly share code, notes, and snippets.

@cauesmelo
Created February 26, 2023 15:26
Show Gist options
  • Select an option

  • Save cauesmelo/04b6a5e18297a49020a238bb89012c6d to your computer and use it in GitHub Desktop.

Select an option

Save cauesmelo/04b6a5e18297a49020a238bb89012c6d to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
)
type People struct {
Id int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
Job string `json:"job"`
}
func main() {
buf := bufio.NewReader(os.Stdin)
data := make(map[int]People)
id := 0
for {
clear()
fmt.Print("> ")
rawString, err := buf.ReadString('\n')
if err != nil {
fmt.Print(err)
}
sentence := strings.Split(strings.ToLower(rawString[:len(rawString)-1]), " ")
command := sentence[0]
args := sentence[1:]
switch command {
case "help":
help()
case "add":
add(args, data, &id)
case "list":
list(data)
case "remove":
remove(data, args)
case "exit":
exit()
case "save":
save(data)
case "load":
load(data)
default:
unknown()
}
}
}
func add(args []string, data map[int]People, id *int) {
if len(args) < 3 {
fmt.Printf("\nToo few arguments, required NAME AGE JOB")
return
}
if len(args) > 3 {
fmt.Printf("\nToo many arguments, please type NAME AGE JOB only")
return
}
age, err := strconv.Atoi(args[1])
if err != nil {
fmt.Printf("\nInvalid age, please type a number.")
return
}
data[*id] = People{
Age: age,
Id: *id,
Job: args[2],
Name: args[0],
}
*id++
}
func list(data map[int]People) {
fmt.Printf("\n List of users:")
for _, i := range data {
fmt.Printf("\n ID: %v \t NAME: %v \t AGE: %v \t JOB: %v", i.Id, i.Name, i.Age, i.Job)
}
next()
}
func remove(data map[int]People, arg []string) {
id, err := strconv.Atoi(arg[0])
if err != nil {
fmt.Printf("\nInvalid Id. Please type a number.")
return
}
if (data[id] == People{}) {
fmt.Printf("\nUser does not exist.")
return
}
delete(data, id)
}
func save(data map[int]People) {
arr := []People{}
for _, i := range data {
arr = append(arr, i)
}
d, err := json.Marshal(arr)
if err != nil {
log.Fatal(err)
}
f, err := os.Create("data.json")
if err != nil {
log.Fatal(err)
}
f.Write(d)
next()
}
func load(data map[int]People) {
f, err := os.ReadFile("data.json")
if err != nil {
log.Fatal(err)
}
d := []People{}
json.Unmarshal(f, &d)
for _, i := range d {
data[i.Id] = i
}
}
func next() {
fmt.Printf("\n\nPress any key to continue...")
fmt.Scanln()
}
func unknown() {
fmt.Printf("\nCommand does not exist. Type 'help' to show manual.")
next()
}
func exit() {
os.Exit(0)
}
func clear() {
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
}
func help() {
fmt.Printf("\nAvailable commands:")
fmt.Printf("\n\t help - Show manual")
fmt.Printf("\n\t add [name] [age] [job] - Add new user with name, age and job")
fmt.Printf("\n\t list - List all users")
fmt.Printf("\n\t remove [id] - Remove user by id")
fmt.Printf("\n\t save - Save all data locally")
fmt.Printf("\n\t load - Load data previously saved")
fmt.Printf("\n\t exit - End program")
next()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment