|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "log/slog" |
| 7 | + "os" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +func main() { |
| 14 | + //logging 'slog' estándar de go (desde la versión 1.21) |
| 15 | + slog.SetLogLoggerLevel(slog.LevelDebug) |
| 16 | + slog.Debug("log debug") |
| 17 | + slog.Info("log info") |
| 18 | + slog.Warn("log warn") |
| 19 | + slog.Error("log error") |
| 20 | + taskManager() |
| 21 | +} |
| 22 | + |
| 23 | +func taskManager() { |
| 24 | + |
| 25 | + logger := slog.Default() |
| 26 | + tasks := make(map[string]string) |
| 27 | + logger.Info("administrador de tareas iniciado.") |
| 28 | + reader := bufio.NewReader(os.Stdin) |
| 29 | + var option string |
| 30 | + for option != "4" { |
| 31 | + printMenu() |
| 32 | + fmt.Print("Ingrese la opción :") |
| 33 | + option, _ = reader.ReadString('\n') |
| 34 | + option = strings.TrimSpace(option) |
| 35 | + switch option { |
| 36 | + case "1": |
| 37 | + fmt.Println("Tareas registradas") |
| 38 | + timeStart := time.Now() |
| 39 | + if len(tasks) == 0 { |
| 40 | + fmt.Println("No hay tareas registradas") |
| 41 | + } |
| 42 | + for k, v := range tasks { |
| 43 | + fmt.Println(k, "-", v) |
| 44 | + } |
| 45 | + timeEnd := time.Now() |
| 46 | + timeDuration := timeEnd.Compare(timeStart) |
| 47 | + logger.Info("Tiempo de ejecución en listado " + strconv.Itoa(timeDuration) + "ms") |
| 48 | + case "2": |
| 49 | + fmt.Println("Agregar una tarea") |
| 50 | + timeStart := time.Now() |
| 51 | + fmt.Print("Ingrese nombre de la tarea:") |
| 52 | + name, _ := reader.ReadString('\n') |
| 53 | + name = strings.TrimSpace(name) |
| 54 | + fmt.Print("Ingrese descripción de la tarea:") |
| 55 | + description, _ := reader.ReadString('\n') |
| 56 | + description = strings.TrimSpace(description) |
| 57 | + tasks[name] = description |
| 58 | + timeEnd := time.Now() |
| 59 | + timeDuration := timeEnd.Compare(timeStart) |
| 60 | + logger.Info("Tarea creada " + name + " Tiempo de ejecución " + strconv.Itoa(timeDuration) + "ms") |
| 61 | + case "3": |
| 62 | + fmt.Println("Eliminar una tarea") |
| 63 | + timeStart := time.Now() |
| 64 | + fmt.Print("Ingrese nombre de la tarea a eliminar:") |
| 65 | + nameToDelete, _ := reader.ReadString('\n') |
| 66 | + nameToDelete = strings.TrimSpace(nameToDelete) |
| 67 | + if _, ok := tasks[nameToDelete]; ok { |
| 68 | + delete(tasks, nameToDelete) |
| 69 | + timeEnd := time.Now() |
| 70 | + timeDuration := timeEnd.Compare(timeStart) |
| 71 | + logger.Info("Tarea eliminada " + nameToDelete + " ,tiempo de ejecución " + strconv.Itoa(timeDuration) + "ms") |
| 72 | + } else { |
| 73 | + timeEnd := time.Now() |
| 74 | + timeDuration := timeEnd.Compare(timeStart) |
| 75 | + logger.Info("Tarea no registrada " + nameToDelete + " ,tiempo de ejecución " + strconv.Itoa(timeDuration) + "ms") |
| 76 | + } |
| 77 | + case "4": |
| 78 | + logger.Info("saliendo del sistema, gracias por usar") |
| 79 | + default: |
| 80 | + logger.Warn("opción no valida") |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func printMenu() { |
| 86 | + fmt.Println(strings.Repeat("-", 29)) |
| 87 | + fmt.Println("|| ADMINISTRADOR DE TAREAS ||") |
| 88 | + fmt.Println(strings.Repeat("-", 29)) |
| 89 | + fmt.Println("Opciones disponibles") |
| 90 | + fmt.Println("[1] Mostrar tareas") |
| 91 | + fmt.Println("[2] Agregar una tarea") |
| 92 | + fmt.Println("[3] Eliminar una tarea") |
| 93 | + fmt.Println("[4] Salir") |
| 94 | +} |
0 commit comments