Files
todo/main.go

201 lines
4.7 KiB
Go
Raw Normal View History

2025-05-21 00:27:17 -04:00
package main
import (
2025-05-22 23:11:03 -04:00
"database/sql"
2025-05-21 00:27:17 -04:00
"flag"
"fmt"
"log"
"os"
2025-05-22 23:11:03 -04:00
_ "github.com/mattn/go-sqlite3"
2025-05-21 00:27:17 -04:00
)
var fileName = "todos.json"
type Todo struct {
Id int `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Completed bool `json:"completed"`
}
func printTodo(todo Todo) {
2025-05-21 00:45:23 -04:00
fmt.Printf("- ID: %d\n- Title: %s\n- Description: %s\n- Completed: %v\n--------\n",
todo.Id, todo.Title, todo.Description, todo.Completed)
2025-05-21 00:27:17 -04:00
}
2025-05-21 00:45:23 -04:00
func listTodos(todos []Todo) {
2025-05-21 00:27:17 -04:00
fmt.Println("TODO: \n------")
for _, todo := range todos {
2025-05-21 00:45:23 -04:00
if !todo.Completed {
printTodo(todo)
2025-05-21 00:27:17 -04:00
}
}
fmt.Println("\nCompleted: \n------")
for _, todo := range todos {
2025-05-21 00:45:23 -04:00
if todo.Completed {
printTodo(todo)
2025-05-21 00:27:17 -04:00
}
}
2025-05-21 00:45:23 -04:00
}
2025-05-21 00:27:17 -04:00
2025-05-22 23:11:03 -04:00
func loadTodos(db *sql.DB) ([]Todo, error) {
var todos []Todo
rows, err := db.Query("SELECT id, title, description, completed FROM todos")
2025-05-21 00:45:23 -04:00
if err != nil {
return nil, err
}
2025-05-22 23:11:03 -04:00
defer rows.Close()
for rows.Next() {
var todo Todo
err := rows.Scan(&todo.Id, &todo.Title, &todo.Description, &todo.Completed)
if err != nil {
return nil, err
}
todos = append(todos, todo)
2025-05-21 00:45:23 -04:00
}
return todos, nil
2025-05-21 00:27:17 -04:00
}
2025-05-22 23:11:03 -04:00
func getNextId(db *sql.DB) int {
var id int
err := db.QueryRow("SELECT MAX(id) FROM todos").Scan(&id)
2025-05-21 00:27:17 -04:00
if err != nil {
2025-05-22 23:11:03 -04:00
return 0
2025-05-21 00:45:23 -04:00
}
2025-05-22 23:11:03 -04:00
return id + 1
2025-05-21 00:45:23 -04:00
}
2025-05-22 23:11:03 -04:00
func addTodo(db *sql.DB, title, description string) error {
2025-05-21 00:45:23 -04:00
todo := Todo{
2025-05-22 23:11:03 -04:00
Id: getNextId(db),
2025-05-21 00:45:23 -04:00
Title: title,
Description: description,
Completed: false,
2025-05-21 00:27:17 -04:00
}
2025-05-22 23:11:03 -04:00
_, err := db.Exec("INSERT INTO todos (id, title, description, completed) VALUES (?, ?, ?, ?)", todo.Id, todo.Title, todo.Description, todo.Completed)
if err != nil {
return err
}
return nil
2025-05-21 00:27:17 -04:00
}
2025-05-22 23:11:03 -04:00
func completeTodo(db *sql.DB, id int) error {
_, err := db.Exec("UPDATE todos SET completed=true WHERE id=?", id)
2025-05-21 00:45:23 -04:00
if err != nil {
return err
}
2025-05-22 23:11:03 -04:00
return nil
2025-05-21 00:27:17 -04:00
}
2025-05-22 23:11:03 -04:00
func removeTodo(db *sql.DB, id int) error {
_, err := db.Exec("DELETE FROM todos WHERE id=?", id)
2025-05-21 00:45:23 -04:00
if err != nil {
return err
}
2025-05-22 23:11:03 -04:00
return nil
2025-05-21 00:27:17 -04:00
}
2025-05-21 00:45:23 -04:00
var (
title = flag.String("title", "", "Title of the todo item")
description = flag.String("description", "", "Description of the todo item")
id = flag.Int("id", 0, "ID of the todo item to remove or mark as complete")
2025-05-21 00:45:23 -04:00
)
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Todo CLI - A simple todo list manager\n\n")
fmt.Fprintf(os.Stderr, "Usage:\n %s [action] [flags]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Actions:\n")
fmt.Fprintf(os.Stderr, " add Add a new todo item\n")
fmt.Fprintf(os.Stderr, " remove Remove a todo item\n")
fmt.Fprintf(os.Stderr, " complete Mark a todo item as completed\n")
fmt.Fprintf(os.Stderr, " list List all todo items\n\n")
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nExamples:\n")
fmt.Fprintf(os.Stderr, " %s add -t \"Meeting\" -d \"Team standup at 10AM\"\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s complete -i 1\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s remove -i 2\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s list\n", os.Args[0])
}
flag.StringVar(title, "t", "", "shorthand for --title")
flag.StringVar(description, "d", "", "shorthand for --description")
flag.IntVar(id, "i", 0, "shorthand for --id")
}
2025-05-21 00:45:23 -04:00
func main() {
// Check for help flag before any other operations
if len(os.Args) > 1 && (os.Args[1] == "-h" || os.Args[1] == "--help") {
flag.Usage()
os.Exit(0)
}
2025-05-22 23:11:03 -04:00
db, err := sql.Open("sqlite3", "./todos.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
sqlStmt := `CREATE TABLE if not exists todos ( id INT not null primary key, title varchar not null, description text, completed boolean);`
_, err = db.Exec(sqlStmt)
if err != nil {
log.Fatal(err)
}
if len(os.Args) < 2 {
flag.Usage()
os.Exit(1)
}
action := os.Args[1]
os.Args = os.Args[1:]
2025-05-21 00:45:23 -04:00
flag.Parse()
switch action {
2025-05-21 00:45:23 -04:00
case "add":
2025-05-22 23:11:03 -04:00
handleAdd(db)
2025-05-21 00:45:23 -04:00
case "remove":
2025-05-22 23:11:03 -04:00
handleRemove(db)
2025-05-21 00:45:23 -04:00
case "complete":
2025-05-22 23:11:03 -04:00
handleComplete(db)
2025-05-21 00:45:23 -04:00
case "list":
2025-05-22 23:11:03 -04:00
handleList(db)
2025-05-21 00:45:23 -04:00
default:
log.Fatal("Please input a valid action.")
2025-05-21 00:27:17 -04:00
}
}
2025-05-22 23:11:03 -04:00
func handleAdd(db *sql.DB) {
2025-05-21 00:45:23 -04:00
if *title == "" || *description == "" {
log.Fatal("Title and Description must be provided for adding a new TODO")
2025-05-21 00:27:17 -04:00
}
2025-05-22 23:11:03 -04:00
if err := addTodo(db, *title, *description); err != nil {
2025-05-21 00:45:23 -04:00
log.Fatal(err)
2025-05-21 00:27:17 -04:00
}
}
2025-05-22 23:11:03 -04:00
func handleRemove(db *sql.DB) {
2025-05-21 00:45:23 -04:00
if *id == 0 {
log.Fatal("ID must be provided for removing a TODO")
}
2025-05-22 23:11:03 -04:00
if err := removeTodo(db, *id); err != nil {
2025-05-21 00:45:23 -04:00
log.Fatal(err)
}
2025-05-21 00:27:17 -04:00
}
2025-05-22 23:11:03 -04:00
func handleComplete(db *sql.DB) {
2025-05-21 00:45:23 -04:00
if *id == 0 {
log.Fatal("ID must be provided for marking a Todo as complete")
}
2025-05-22 23:11:03 -04:00
if err := completeTodo(db, *id); err != nil {
2025-05-21 00:45:23 -04:00
log.Fatal(err)
2025-05-21 00:27:17 -04:00
}
2025-05-21 00:45:23 -04:00
}
2025-05-21 00:27:17 -04:00
2025-05-22 23:11:03 -04:00
func handleList(db *sql.DB) {
todos, err := loadTodos(db)
2025-05-21 00:45:23 -04:00
if err != nil {
log.Fatal(err)
}
listTodos(todos)
2025-05-21 00:27:17 -04:00
}