mirror of
https://github.com/AmanTahiliani/todo.git
synced 2026-10-06 17:31:56 -04:00
Compare commits
8 Commits
v0.2.0-alp
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8da19ea968 | ||
|
|
8038757726 | ||
|
|
39a282695d | ||
|
|
b71171d5a8 | ||
|
|
3180461d94 | ||
|
|
fcf0cb3653 | ||
|
|
3dbf9a2b3d | ||
|
|
91ab19fc02 |
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
|
||||
23
Makefile
Normal file
23
Makefile
Normal file
@@ -0,0 +1,23 @@
|
||||
BINARY_NAME=todo
|
||||
INSTALL_PATH=/usr/local/bin
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
go build -o $(BINARY_NAME) ./cmd/todo
|
||||
|
||||
.PHONY: install
|
||||
install: build
|
||||
mkdir -p $(INSTALL_PATH)
|
||||
cp $(BINARY_NAME) $(INSTALL_PATH)/$(BINARY_NAME)
|
||||
|
||||
.PHONY: uninstall
|
||||
uninstall:
|
||||
rm -f $(INSTALL_PATH)/$(BINARY_NAME)
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -f $(BINARY_NAME)
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
go test -v ./...
|
||||
122
README.md
122
README.md
@@ -1,6 +1,6 @@
|
||||
# Go Todo CLI App
|
||||
# Todo CLI
|
||||
|
||||
A simple command-line Todo application written in Go. Todos are stored in a SQLite database.
|
||||
A powerful command-line todo application written in Go. Manage your tasks efficiently with a simple, intuitive interface. All data is automatically stored in a SQLite database in your home directory.
|
||||
|
||||
## Features
|
||||
|
||||
@@ -8,77 +8,87 @@ A simple command-line Todo application written in Go. Todos are stored in a SQLi
|
||||
- List all todos, grouped by incomplete and completed
|
||||
- Mark todos as completed
|
||||
- Remove todos by ID
|
||||
- Data is persisted in SQLite database
|
||||
- User-friendly command-line interface with action commands
|
||||
- Automatic SQLite database management
|
||||
- User-friendly command-line interface with intuitive commands
|
||||
- Data stored securely in user's home directory
|
||||
|
||||
## Dependencies
|
||||
## Installation
|
||||
|
||||
- Go 1.x
|
||||
- github.com/mattn/go-sqlite3
|
||||
### Using Go
|
||||
|
||||
```bash
|
||||
go install github.com/AmanTahiliani/todo/cmd/todo@latest
|
||||
```
|
||||
|
||||
### Using Make
|
||||
|
||||
```bash
|
||||
git clone https://github.com/AmanTahiliani/todo.git
|
||||
cd todo
|
||||
make install
|
||||
```
|
||||
|
||||
### Using Homebrew (macOS)
|
||||
|
||||
```bash
|
||||
brew tap AmanTahiliani/todo
|
||||
brew install todo
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Build the app:
|
||||
|
||||
```sh
|
||||
go build -o todo
|
||||
### Add a new todo
|
||||
```bash
|
||||
todo add -t "Meeting" -d "Team standup at 10AM"
|
||||
```
|
||||
|
||||
### Command Structure
|
||||
|
||||
The app uses a command-based interface where the action comes first, followed by flags:
|
||||
|
||||
```sh
|
||||
./todo [action] [flags]
|
||||
### List all todos
|
||||
```bash
|
||||
todo list
|
||||
```
|
||||
|
||||
Available actions:
|
||||
- `add` - Add a new todo item
|
||||
- `remove` - Remove a todo item
|
||||
- `complete` - Mark a todo item as completed
|
||||
- `list` - List all todo items
|
||||
|
||||
### Flags
|
||||
|
||||
- `-t, --title` - Title for the todo (required for add)
|
||||
- `-d, --description` - Description for the todo (required for add)
|
||||
- `-i, --id` - ID of the todo (required for remove and complete)
|
||||
- `-h, --help` - Display help information
|
||||
|
||||
### Examples
|
||||
|
||||
Add a new todo:
|
||||
```sh
|
||||
./todo add -t "Buy milk" -d "Get 2 liters of milk"
|
||||
### Mark a todo as completed
|
||||
```bash
|
||||
todo complete -i 1
|
||||
```
|
||||
|
||||
List all todos:
|
||||
```sh
|
||||
./todo list
|
||||
### Remove a todo
|
||||
```bash
|
||||
todo remove -i 1
|
||||
```
|
||||
|
||||
Complete a todo:
|
||||
```sh
|
||||
./todo complete -i 1
|
||||
### Get help
|
||||
```bash
|
||||
todo --help
|
||||
```
|
||||
|
||||
Remove a todo:
|
||||
```sh
|
||||
./todo remove -i 2
|
||||
## Data Storage
|
||||
|
||||
Todos are automatically stored in a SQLite database located at `~/.todo/todos.db`. The application handles all database operations transparently, so you don't need to worry about database management.
|
||||
|
||||
## Development
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- Go 1.21 or later
|
||||
- Make (optional, for easier building)
|
||||
|
||||
### Building from source
|
||||
|
||||
```bash
|
||||
make build
|
||||
```
|
||||
|
||||
Display help:
|
||||
```sh
|
||||
./todo -h
|
||||
### Running tests
|
||||
|
||||
```bash
|
||||
make test
|
||||
```
|
||||
|
||||
## Next Improvements
|
||||
## License
|
||||
|
||||
- Use a CLI library like [cobra](https://github.com/spf13/cobra) for better UX
|
||||
- Add edit and search features
|
||||
- Add unit tests
|
||||
- Improve error handling
|
||||
- Add binary release for different platforms and make it available through Homebrew, Winget and Apt
|
||||
- Add due dates and priority levels for todos
|
||||
- Implement categories/tags for better organization
|
||||
- Add data export/import functionality
|
||||
MIT License
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions are welcome! Please feel free to submit a Pull Request.
|
||||
|
||||
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)
|
||||
}
|
||||
12
go.mod
12
go.mod
@@ -1,5 +1,13 @@
|
||||
module github.com/AmanTahiliani/todo
|
||||
|
||||
go 1.24.0
|
||||
go 1.21.4
|
||||
|
||||
require github.com/mattn/go-sqlite3 v1.14.28
|
||||
require (
|
||||
github.com/mattn/go-sqlite3 v1.14.28
|
||||
github.com/spf13/cobra v1.9.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/spf13/pflag v1.0.6 // indirect
|
||||
)
|
||||
|
||||
10
go.sum
10
go.sum
@@ -1,2 +1,12 @@
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A=
|
||||
github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
|
||||
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
|
||||
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
|
||||
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
Reference in New Issue
Block a user