heybran / gtodo Goto Github PK
View Code? Open in Web Editor NEWA simple Golang CLI TODO app.
License: MIT License
A simple Golang CLI TODO app.
License: MIT License
When listing tasks that belong to specific category or complete status, tasks IDs starts from 1 again, did not match with their original IDs, which will cause unexpected result if users try to delete tasks based on the IDs provided by list
subcommand.
After clone the repo in a new folder, delete the .todos.json
file in my home directory, build the binary with GOOS=darwin GOARCH=amd64 go build -o todo cmd/main.go
, then run ./todo init
or any other subcommands in terminal, there is this error in terminal:
2023/05/05 18:24:54 open /Users/brandon/.todos.json: no such file or directory
If I put back the .todos.json
file, then init
subcommand works again, so some codes are trying to open that file initially before any command is run.
What about an user has already been using cli todo app for a while and they have todo list stored inside todos.json
file, and later down the road, they need to upgrade the cli todo app to a higher version, will their data inside todos.json
get lost?
If users have already been using the CLI todo app and have data stored in their todos.json
file, you should take care to preserve their data when they upgrade to a new version of the app.
One way to do this is to include a version number in the todos.json
file and check it when the app starts up. If the version number in the file is older than the current version of the app, you can migrate the data to the new format.
Here's an example of how to do this:
Define a Version
struct that includes the version number of the app and the version number of the todos.json
file:
type Version struct {
AppVersion string `json:"app_version"`
DataVersion string `json:"data_version"`
}
When the app starts up, read the todos.json
file and check the version number:
func main() {
// Read the contents of the todos.json file from the data directory
data, err := ioutil.ReadFile("data/todos.json")
if err != nil {
fmt.Println("Error reading todos.json:", err)
return
}
// Unmarshal the JSON data into a Version struct
var version Version
err = json.Unmarshal(data, &version)
if err != nil {
fmt.Println("Error unmarshaling todos.json:", err)
return
}
// Check the version number and migrate the data if necessary
if version.DataVersion < "2.0.0" {
migrateData()
}
// Unmarshal the JSON data into a slice of Todo structs
var todos []Todo
err = json.Unmarshal(data, &todos)
if err != nil {
fmt.Println("Error unmarshaling todos.json:", err)
return
}
// Do something with the todos...
}
In this example, we first unmarshal the JSON data into a Version
struct and check the DataVersion
field. If the version number is older than 2.0.0
, we call a migrateData
function to migrate the data to the new format.
Define a migrateData
function that migrates the data to the new format:
func migrateData() {
// Read the contents of the todos.json file from the data directory
data, err := ioutil.ReadFile("data/todos.json")
if err != nil {
fmt.Println("Error reading todos.json:", err)
return
}
// Unmarshal the JSON data into a slice of old Todo structs
var oldTodos []OldTodo
err = json.Unmarshal(data, &oldTodos)
if err != nil {
fmt.Println("Error unmarshaling todos.json:", err)
return
}
// Migrate the data to the new format
var newTodos []Todo
for _, oldTodo := range oldTodos {
newTodo := Todo{
Title: oldTodo.Title,
Done: oldTodo.Done,
}
newTodos = append(newTodos, newTodo)
}
// Marshal the new data to JSON and write it back to the todos.json file
newData, err := json.Marshal(newTodos)
if err != nil {
fmt.Println("Error marshaling new data:", err)
return
}
err = ioutil.WriteFile("data/todos.json", newData, 0644)
if err != nil {
fmt.Println("Error writing new data:", err)
return
}
fmt.Println("Data migrated to new format.")
}
go install github.com/heybran/gtodo@latest
package github.com/heybran/gtodo is not a main package
As todo task grows, we might want to list tasks that belong to specific category, so need to add --cat
flag support to list
subcommand so we can run:
gtodo list --cat "Job Search"
Need a release workflow.
At the moment, it uses .wav audio file downloaded from internet for the guitar metronome sounds, not ideal. Best to use Web Audio API to do same.
It'd be nice to add a clear
subcommand so people can clear out their existing todo tasks, and probably need to prompt a confirm if such action is required.
Add an init
subcommand to allow users to create an empty .todos.json
file for storing their todo tasks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.