mirror of
https://github.com/AmanTahiliani/todo.git
synced 2026-10-06 17:31:56 -04:00
Compare commits
6 Commits
v0.3.0-alp
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8da19ea968 | ||
|
|
8038757726 | ||
|
|
39a282695d | ||
|
|
b71171d5a8 | ||
|
|
3180461d94 | ||
|
|
fcf0cb3653 |
9
.gitignore
vendored
9
.gitignore
vendored
@@ -1,9 +1,5 @@
|
||||
# Binaries
|
||||
todo
|
||||
todo.exe
|
||||
todo-mac
|
||||
todo-linux
|
||||
main
|
||||
|
||||
|
||||
# OS files
|
||||
.DS_Store
|
||||
@@ -28,4 +24,5 @@ todos.json
|
||||
*.log
|
||||
|
||||
# DB files
|
||||
*.db
|
||||
*.db# Added by goreleaser init:
|
||||
dist/
|
||||
|
||||
79
.goreleaser.yaml
Normal file
79
.goreleaser.yaml
Normal file
@@ -0,0 +1,79 @@
|
||||
# This is an example .goreleaser.yml file with some sensible defaults.
|
||||
# Make sure to check the documentation at https://goreleaser.com
|
||||
|
||||
# The lines below are called `modelines`. See `:help modeline`
|
||||
# Feel free to remove those if you don't want/need to use them.
|
||||
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
|
||||
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
|
||||
|
||||
version: 2
|
||||
|
||||
before:
|
||||
hooks:
|
||||
# You may remove this if you don't use go modules.
|
||||
- go mod tidy
|
||||
# you may remove this if you don't need go generate
|
||||
- go generate ./...
|
||||
|
||||
builds:
|
||||
- env:
|
||||
- CGO_ENABLED=0
|
||||
goos:
|
||||
- linux
|
||||
- windows
|
||||
- darwin
|
||||
goarch:
|
||||
- amd64
|
||||
- arm64
|
||||
|
||||
|
||||
checksum:
|
||||
name_template: 'checksums.txt'
|
||||
algorithm: sha256
|
||||
|
||||
changelog:
|
||||
sort: asc
|
||||
filters:
|
||||
exclude:
|
||||
- "^docs:"
|
||||
- 'README'
|
||||
- Merge pull request
|
||||
- Merge branch
|
||||
|
||||
archives:
|
||||
- formats: [tar.gz]
|
||||
# this name template makes the OS and Arch compatible with the results of `uname`.
|
||||
name_template: >-
|
||||
{{ .ProjectName }}_
|
||||
{{- title .Os }}_
|
||||
{{- if eq .Arch "amd64" }}x86_64
|
||||
{{- else if eq .Arch "386" }}i386
|
||||
{{- else }}{{ .Arch }}{{ end }}
|
||||
{{- if .Arm }}v{{ .Arm }}{{ end }}
|
||||
# use zip for windows archives
|
||||
format_overrides:
|
||||
- goos: windows
|
||||
formats: [zip]
|
||||
|
||||
brews:
|
||||
-
|
||||
name: todo
|
||||
homepage: https://github.com/AmanTahiliani/todo
|
||||
description: "A simple CLI tool to manage your todo list."
|
||||
license: "MIT"
|
||||
repository:
|
||||
owner: AmanTahiliani
|
||||
name: homebrew-tools
|
||||
commit_author:
|
||||
name: AmanTahiliani
|
||||
email: amantahiliani7437@gmail.com
|
||||
|
||||
snapshot:
|
||||
name_template: '{{ .Tag }}-next'
|
||||
|
||||
|
||||
release:
|
||||
prerelease: auto
|
||||
|
||||
universal_binaries:
|
||||
- replace: true
|
||||
@@ -1,16 +0,0 @@
|
||||
class Todo < Formula
|
||||
desc "A simple CLI todo application written in Go"
|
||||
homepage "https://github.com/AmanTahiliani/todo"
|
||||
version "0.1.0"
|
||||
license "MIT"
|
||||
|
||||
depends_on "go" => :build
|
||||
|
||||
def install
|
||||
system "go", "build", "-o", bin/"todo", "./cmd/todo"
|
||||
end
|
||||
|
||||
test do
|
||||
system "#{bin}/todo", "--help"
|
||||
end
|
||||
end
|
||||
227
cmd/todo/main.go
Normal file
227
cmd/todo/main.go
Normal file
@@ -0,0 +1,227 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type Todo struct {
|
||||
Id int `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Completed bool `json:"completed"`
|
||||
}
|
||||
|
||||
var (
|
||||
dbPath string
|
||||
rootCmd = &cobra.Command{
|
||||
Use: "todo",
|
||||
Short: "A simple CLI todo application",
|
||||
Long: `Todo is a CLI application for managing your tasks.
|
||||
It provides simple commands to add, list, complete, and remove todos.
|
||||
All data is stored locally in a SQLite database.`,
|
||||
}
|
||||
|
||||
addCmd = &cobra.Command{
|
||||
Use: "add",
|
||||
Short: "Add a new todo",
|
||||
Run: handleAdd,
|
||||
}
|
||||
|
||||
listCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List all todos",
|
||||
Run: handleList,
|
||||
}
|
||||
|
||||
completeCmd = &cobra.Command{
|
||||
Use: "complete",
|
||||
Short: "Mark a todo as completed",
|
||||
Run: handleComplete,
|
||||
}
|
||||
|
||||
removeCmd = &cobra.Command{
|
||||
Use: "remove",
|
||||
Short: "Remove a todo",
|
||||
Run: handleRemove,
|
||||
}
|
||||
|
||||
title string
|
||||
description string
|
||||
id int
|
||||
)
|
||||
|
||||
func init() {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Create .todo directory if it doesn't exist
|
||||
todoDir := filepath.Join(homeDir, ".todo")
|
||||
if err := os.MkdirAll(todoDir, 0755); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Set database path
|
||||
dbPath = filepath.Join(todoDir, "todos.db")
|
||||
|
||||
// Add commands
|
||||
rootCmd.AddCommand(addCmd, listCmd, completeCmd, removeCmd)
|
||||
|
||||
// Add flags
|
||||
addCmd.Flags().StringVarP(&title, "title", "t", "", "Title of the todo")
|
||||
addCmd.Flags().StringVarP(&description, "description", "d", "", "Description of the todo")
|
||||
addCmd.MarkFlagRequired("title")
|
||||
addCmd.MarkFlagRequired("description")
|
||||
|
||||
completeCmd.Flags().IntVarP(&id, "id", "i", 0, "ID of the todo to complete")
|
||||
completeCmd.MarkFlagRequired("id")
|
||||
|
||||
removeCmd.Flags().IntVarP(&id, "id", "i", 0, "ID of the todo to remove")
|
||||
removeCmd.MarkFlagRequired("id")
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func getDB() *sql.DB {
|
||||
db, err := sql.Open("sqlite3", dbPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Create table if it doesn't exist
|
||||
sqlStmt := `CREATE TABLE IF NOT EXISTS todos (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
title TEXT NOT NULL,
|
||||
description TEXT,
|
||||
completed BOOLEAN DEFAULT 0
|
||||
);`
|
||||
|
||||
_, err = db.Exec(sqlStmt)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
func handleAdd(_ *cobra.Command, _ []string) {
|
||||
db := getDB()
|
||||
defer db.Close()
|
||||
|
||||
_, err := db.Exec("INSERT INTO todos (title, description) VALUES (?, ?)", title, description)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("Todo added successfully!")
|
||||
}
|
||||
|
||||
func handleList(_ *cobra.Command, _ []string) {
|
||||
db := getDB()
|
||||
defer db.Close()
|
||||
|
||||
rows, err := db.Query("SELECT id, title, description, completed FROM todos ORDER BY completed, id")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
fmt.Println("\nPending Todos:")
|
||||
fmt.Println("-------------")
|
||||
pendingFound := false
|
||||
|
||||
for rows.Next() {
|
||||
var todo Todo
|
||||
if err := rows.Scan(&todo.Id, &todo.Title, &todo.Description, &todo.Completed); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if !todo.Completed {
|
||||
pendingFound = true
|
||||
fmt.Printf("[%d] %s\n %s\n\n", todo.Id, todo.Title, todo.Description)
|
||||
}
|
||||
}
|
||||
|
||||
if !pendingFound {
|
||||
fmt.Println("No pending todos!\n")
|
||||
}
|
||||
|
||||
// Reset the rows for completed todos
|
||||
rows, err = db.Query("SELECT id, title, description, completed FROM todos WHERE completed = 1 ORDER BY id")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
fmt.Println("Completed Todos:")
|
||||
fmt.Println("---------------")
|
||||
completedFound := false
|
||||
|
||||
for rows.Next() {
|
||||
var todo Todo
|
||||
if err := rows.Scan(&todo.Id, &todo.Title, &todo.Description, &todo.Completed); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
completedFound = true
|
||||
fmt.Printf("[%d] %s\n %s\n\n", todo.Id, todo.Title, todo.Description)
|
||||
}
|
||||
|
||||
if !completedFound {
|
||||
fmt.Println("No completed todos!")
|
||||
}
|
||||
}
|
||||
|
||||
func handleComplete(_ *cobra.Command, _ []string) {
|
||||
db := getDB()
|
||||
defer db.Close()
|
||||
|
||||
result, err := db.Exec("UPDATE todos SET completed = 1 WHERE id = ?", id)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
fmt.Printf("No todo found with ID %d\n", id)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Todo %d marked as completed!\n", id)
|
||||
}
|
||||
|
||||
func handleRemove(_ *cobra.Command, _ []string) {
|
||||
db := getDB()
|
||||
defer db.Close()
|
||||
|
||||
result, err := db.Exec("DELETE FROM todos WHERE id = ?", id)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
fmt.Printf("No todo found with ID %d\n", id)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Todo %d removed successfully!\n", id)
|
||||
}
|
||||
Reference in New Issue
Block a user