Skip to content

Commit 6a38ebc

Browse files
committed
fix: failed to create database error
1 parent 56634a9 commit 6a38ebc

File tree

9 files changed

+59
-9
lines changed

9 files changed

+59
-9
lines changed

cmd/add.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ func addRun(cmd *cobra.Command, args []string) {
5353
fmt.Println("Usage: tasks add [task]")
5454
os.Exit(1)
5555
}
56+
// if db doesn't exists, create
57+
if _, err := os.Stat(todo.DatabaseFile); os.IsNotExist(err) {
58+
todo.CreateDatabase()
59+
}
5660
tasks, err := todo.ReadTasks(todo.DatabaseFile)
5761
if err != nil {
5862
file := []byte("[]")

cmd/clear.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cmd
1717

1818
import (
1919
"fmt"
20+
"os"
2021

2122
"github.com/mrinjamul/tasks/todo"
2223
"github.com/spf13/cobra"
@@ -33,6 +34,9 @@ var clearCmd = &cobra.Command{
3334
// Main func
3435
func clearRun(cmd *cobra.Command, args []string) {
3536
tasks := []todo.Task{}
37+
if _, err := os.Stat(todo.DatabaseFile); os.IsNotExist(err) {
38+
todo.CreateDatabase()
39+
}
3640
if forceOpt {
3741
err := todo.SaveTasks(todo.DatabaseFile, tasks)
3842
if err != nil {

cmd/done.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ func doneRun(cmd *cobra.Command, args []string) {
5757
fmt.Println("Usage: tasks done [task id]")
5858
os.Exit(1)
5959
}
60+
if _, err := os.Stat(todo.DatabaseFile); os.IsNotExist(err) {
61+
todo.CreateDatabase()
62+
}
6063
tasks, _ := todo.ReadTasks(todo.DatabaseFile)
6164
i, err := strconv.Atoi(args[0])
6265

cmd/modify.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ var modifyCmd = &cobra.Command{
3636

3737
// Main func
3838
func modifyRun(cmd *cobra.Command, args []string) {
39+
if _, err := os.Stat(todo.DatabaseFile); os.IsNotExist(err) {
40+
todo.CreateDatabase()
41+
}
3942
if len(args) == 0 {
4043
fmt.Println("Error: Too short argument")
4144
fmt.Println("Usage: tasks modify [task id] [new]")

cmd/remove.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ var (
5050

5151
// Main func
5252
func removeRun(cmd *cobra.Command, args []string) {
53+
if _, err := os.Stat(todo.DatabaseFile); os.IsNotExist(err) {
54+
todo.CreateDatabase()
55+
}
5356
// remove only done tasks func
5457
if doOpt {
5558
response := todo.ConfirmPrompt("Do you want to remove all done task(s)?")

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func Execute() {
4747
}
4848

4949
func init() {
50+
// todo.MigrateDB()
5051
// cobra.OnInitialize(initConfig)
5152

5253
// Here you will define your flags and configuration settings.

cmd/undone.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ func undoneRun(cmd *cobra.Command, args []string) {
5656
fmt.Println("Usage: tasks undone [task id]")
5757
os.Exit(1)
5858
}
59+
if _, err := os.Stat(todo.DatabaseFile); os.IsNotExist(err) {
60+
todo.CreateDatabase()
61+
}
5962
tasks, _ := todo.ReadTasks(todo.DatabaseFile)
6063
i, err := strconv.Atoi(args[0])
6164

snap/snapcraft.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: tasks
22
base: core18 # the base snap is the execution environment for this snap
3-
version: "1.1.0" # just for humans, typically '1.2+git' or '1.3.2'
3+
version: "1.1.1" # just for humans, typically '1.2+git' or '1.3.2'
44
summary: A command-line todolist application # 79 char long summary
55
description: |
66
tasks will help you get more done in less time.

todo/todo.go

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ import (
1515

1616
// Task : structure
1717
type Task struct {
18-
Text string
19-
Priority int
20-
position int
21-
Done bool
18+
Text string `json:"text"`
19+
Priority int `json:"prority"`
20+
Position int `json:"position"`
21+
Done bool `json:"done"`
2222
}
2323

2424
// DatabaseFile is the db file
2525
var DatabaseFile string = GetHomeDir() + "/.config/tasks/tasks.json"
2626
var (
2727
Appname = "tasks"
28-
Version = "1.0.0"
28+
Version = "dev"
2929
GitCommit = "0000000000000000000000000000000000000000"
3030
)
3131

@@ -79,7 +79,7 @@ func ReadTasks(filename string) ([]Task, error) {
7979
return []Task{}, err
8080
}
8181
for i := range tasks {
82-
tasks[i].position = i + 1
82+
tasks[i].Position = i + 1
8383
}
8484
return tasks, nil
8585
}
@@ -110,7 +110,7 @@ func (i *Task) PrettyP() string {
110110

111111
// Label : index lists
112112
func (i *Task) Label() string {
113-
return strconv.Itoa(i.position) + "."
113+
return strconv.Itoa(i.Position) + "."
114114
}
115115

116116
// ByPri implements sort.Interface for []Item base on
@@ -127,7 +127,7 @@ func (s ByPri) Less(i, j int) bool {
127127
if s[i].Priority != s[j].Priority {
128128
return s[i].Priority < s[j].Priority
129129
}
130-
return s[i].position < s[j].position
130+
return s[i].Position < s[j].Position
131131
}
132132

133133
// PrettyDone : prettify
@@ -181,3 +181,32 @@ func RemoveDuplicate(slice []int) []int {
181181
}
182182
return list
183183
}
184+
185+
func MigrateDB() {
186+
type TaskOld struct {
187+
Text string
188+
Priority int
189+
position int
190+
Done bool
191+
}
192+
// old db retrieve
193+
var old []TaskOld
194+
b, _ := ioutil.ReadFile(DatabaseFile)
195+
_ = json.Unmarshal(b, &old)
196+
for i := range old {
197+
old[i].position = i + 1
198+
}
199+
// new db restoration
200+
var new []Task
201+
for _, t := range old {
202+
var tasks Task
203+
tasks.Text = t.Text
204+
tasks.Priority = t.Priority
205+
tasks.Position = t.position
206+
tasks.Done = t.Done
207+
new = append(new, tasks)
208+
}
209+
b, _ = json.Marshal(new)
210+
_ = ioutil.WriteFile(DatabaseFile, b, 0644)
211+
212+
}

0 commit comments

Comments
 (0)