Skip to content

Commit 89b6f81

Browse files
committed
finish task
1 parent a337f4b commit 89b6f81

File tree

7 files changed

+154
-34
lines changed

7 files changed

+154
-34
lines changed

task/cmd/add.go

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strings"
66

7+
"github.com/dhanusaputra/go-exercises/task/db"
78
"github.com/spf13/cobra"
89
)
910

@@ -12,6 +13,10 @@ var addCmd = &cobra.Command{
1213
Short: "Add a task to your list",
1314
Run: func(cmd *cobra.Command, args []string) {
1415
task := strings.Join(args, " ")
16+
_, err := db.CreateTask(task)
17+
if err != nil {
18+
fmt.Println("something went wrong:", err.Error())
19+
}
1520
fmt.Printf("Add \"%s\" to task list\n", task)
1621
},
1722
}

task/cmd/do.go

+29-16
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
1-
/*
2-
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
161
package cmd
172

183
import (
194
"fmt"
5+
"strconv"
206

7+
"github.com/dhanusaputra/go-exercises/task/db"
218
"github.com/spf13/cobra"
229
)
2310

@@ -26,7 +13,33 @@ var doCmd = &cobra.Command{
2613
Use: "do",
2714
Short: "Marks a task as complete",
2815
Run: func(cmd *cobra.Command, args []string) {
29-
fmt.Println("do called")
16+
var ids []int
17+
for _, arg := range args {
18+
id, err := strconv.Atoi(arg)
19+
if err != nil {
20+
fmt.Println("failed to parse the argument:", id)
21+
continue
22+
}
23+
ids = append(ids, id)
24+
}
25+
tasks, err := db.AllTasks()
26+
if err != nil {
27+
fmt.Println(err)
28+
return
29+
}
30+
for _, id := range ids {
31+
if id <= 0 || id > len(tasks) {
32+
fmt.Println("invalid task number:", id)
33+
continue
34+
}
35+
task := tasks[id-1]
36+
err := db.DeleteTask(task.Key)
37+
if err != nil {
38+
fmt.Println(err)
39+
continue
40+
}
41+
fmt.Printf("Marked \"%d\" as completed\n.", id)
42+
}
3043
},
3144
}
3245

task/cmd/list.go

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
1-
/*
2-
Copyright © 2020 NAME HERE <EMAIL ADDRESS>
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
161
package cmd
172

183
import (
194
"fmt"
5+
"os"
206

7+
"github.com/dhanusaputra/go-exercises/task/db"
218
"github.com/spf13/cobra"
229
)
2310

@@ -26,7 +13,19 @@ var listCmd = &cobra.Command{
2613
Use: "list",
2714
Short: "List all of your tasks",
2815
Run: func(cmd *cobra.Command, args []string) {
29-
fmt.Println("list called")
16+
tasks, err := db.AllTasks()
17+
if err != nil {
18+
fmt.Println("Something wnet wrong:", err.Error())
19+
os.Exit(1)
20+
}
21+
if len(tasks) == 0 {
22+
fmt.Println("You have no tasks to complete! Why not take a vacation?")
23+
return
24+
}
25+
fmt.Println("You have the following tasks:")
26+
for i, task := range tasks {
27+
fmt.Printf("%d. %s\n", i+1, task.Value)
28+
}
3029
},
3130
}
3231

task/cmd/root.go

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import "github.com/spf13/cobra"
44

5+
// RootCmd ...
56
var RootCmd = &cobra.Command{
67
Use: "task",
78
Short: "Task is a CLI manager",

task/db/tasks.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package db
2+
3+
import (
4+
"encoding/binary"
5+
"time"
6+
7+
"github.com/boltdb/bolt"
8+
)
9+
10+
var taskBucket = []byte("tasks")
11+
var db *bolt.DB
12+
13+
// Task ...
14+
type Task struct {
15+
Key int
16+
Value string
17+
}
18+
19+
// Init ...
20+
func Init(dbPath string) error {
21+
var err error
22+
db, err = bolt.Open(dbPath, 0600, &bolt.Options{Timeout: 1 * time.Second})
23+
if err != nil {
24+
return err
25+
}
26+
return db.Update(func(tx *bolt.Tx) error {
27+
_, err := tx.CreateBucketIfNotExists(taskBucket)
28+
return err
29+
})
30+
}
31+
32+
// CreateTask ...
33+
func CreateTask(task string) (int, error) {
34+
var id int
35+
err := db.Update(func(tx *bolt.Tx) error {
36+
b := tx.Bucket(taskBucket)
37+
id64, _ := b.NextSequence()
38+
id = int(id64)
39+
key := itob(id)
40+
return b.Put(key, []byte(task))
41+
})
42+
if err != nil {
43+
return -1, err
44+
}
45+
return 0, nil
46+
}
47+
48+
// AllTasks ...
49+
func AllTasks() ([]Task, error) {
50+
var tasks []Task
51+
err := db.View(func(tx *bolt.Tx) error {
52+
b := tx.Bucket(taskBucket)
53+
c := b.Cursor()
54+
for k, v := c.First(); k != nil; k, v = c.Next() {
55+
tasks = append(tasks, Task{
56+
Key: btoi(k),
57+
Value: string(v),
58+
})
59+
}
60+
return nil
61+
})
62+
if err != nil {
63+
return nil, err
64+
}
65+
return tasks, nil
66+
}
67+
68+
// DeleteTask ...
69+
func DeleteTask(key int) error {
70+
return db.Update(func(tx *bolt.Tx) error {
71+
b := tx.Bucket(taskBucket)
72+
return b.Delete(itob(key))
73+
})
74+
}
75+
76+
func itob(v int) []byte {
77+
b := make([]byte, 8)
78+
binary.BigEndian.PutUint64(b, uint64(v))
79+
return b
80+
}
81+
82+
func btoi(b []byte) int {
83+
return int(binary.BigEndian.Uint64(b))
84+
}

task/main.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
package main
22

3-
import "github.com/dhanusaputra/go-exercises/task/cmd"
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/dhanusaputra/go-exercises/task/cmd"
9+
"github.com/dhanusaputra/go-exercises/task/db"
10+
"github.com/mitchellh/go-homedir"
11+
)
412

513
func main() {
6-
cmd.RootCmd.Execute()
14+
home, _ := homedir.Dir()
15+
dbPath := filepath.Join(home, "tasks.db")
16+
must(db.Init(dbPath))
17+
must(cmd.RootCmd.Execute())
18+
}
19+
20+
func must(err error) {
21+
if err != nil {
22+
fmt.Println(err.Error())
23+
os.Exit(1)
24+
}
725
}

task/my.db

16 KB
Binary file not shown.

0 commit comments

Comments
 (0)