-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
127 lines (108 loc) · 2.38 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"os"
"strconv"
taqqueCli "github.com/kounosukexxx/taqque/internal/services/cli"
"github.com/urfave/cli/v2"
)
var listCmd = &cli.Command{
Name: "list",
Usage: "list tasks",
Action: func(ctx *cli.Context) error {
cli, err := taqqueCli.NewCli()
if err != nil {
return err
}
err = cli.Api.ListTasks(ctx.Context)
if err != nil {
return err
}
return nil
},
}
var pushCmd = &cli.Command{
Name: "push",
Usage: "push a task with priority. by default, priority is 1",
Action: func(ctx *cli.Context) error {
taskTitle := ctx.Args().Get(0)
priorityStr := ctx.Args().Get(1)
priority := 1.0
switch {
case priorityStr == "":
break
default:
var err error
priority, err = strconv.ParseFloat(priorityStr, 64)
if err != nil || priority < 0 {
return fmt.Errorf("priority must be positive float number. got %s", priorityStr)
}
}
cli, err := taqqueCli.NewCli()
if err != nil {
return err
}
err = cli.Api.PushTask(ctx.Context, taskTitle, priority)
if err != nil {
return err
}
return nil
},
}
var popCmd = &cli.Command{
Name: "pop",
Usage: "pop a task of specific priority",
Action: func(ctx *cli.Context) error {
priorityStr := ctx.Args().Get(0)
priority := 1.0
switch {
case priorityStr == "":
break
default:
var err error
priority, err = strconv.ParseFloat(priorityStr, 64)
if err != nil || priority < 0 {
return fmt.Errorf("priority must be positive float number. got %s", priorityStr)
}
}
cli, err := taqqueCli.NewCli()
if err != nil {
return err
}
err = cli.Api.PopTask(ctx.Context, priority)
if err != nil {
return err
}
return nil
},
}
// var cleanCmd = &cli.Command{
// Name: "clean",
// Usage: "clean all tasks",
// Action: func(ctx *cli.Context) error {
// cli := taqqueCli.NewCli()
// if err := cli.Api.CleanTasks(); err != nil {
// _, _ = fmt.Fprintf(os.Stderr, "%+v\n", err.Error())
// os.Exit(2)
// }
// return nil
// },
// }
func main() {
app := cli.NewApp()
app.Name = "taqque"
app.Usage = listCmd.Usage
app.Description = "This is a task manegement tool using queues with priority concept."
app.DefaultCommand = "list"
app.Commands = []*cli.Command{
listCmd,
pushCmd,
popCmd,
// cleanCmd,
}
app.SkipFlagParsing = true
if err := app.Run(os.Args); err != nil {
fmt.Fprint(os.Stderr, err.Error()+"\n")
os.Exit(1)
}
}