-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (102 loc) · 2.36 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
package main
import (
"fmt"
"log"
"os"
"github.com/dorneanu/tw5-cli/tiddlywiki"
cli "github.com/urfave/cli/v2"
)
func main() {
// Define global flags that are always need like the name of the tiddler
globalFlags := []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Value: "name",
Usage: "name of the tiddler",
},
}
var appendText string
app := &cli.App{
Commands: []*cli.Command{
{
Name: "get",
Aliases: []string{"g"},
Usage: "Get a tiddler",
Flags: globalFlags,
Action: func(c *cli.Context) error {
tw := tiddlywiki.NewTW(conf.TWHOST)
_, err := tw.Get("Golang")
if err != nil {
fmt.Printf("Errorf: %s", err)
return err
}
// Try to get tiddler
tiddler, err := tw.Get(c.String("name"))
if err != nil {
fmt.Printf("Couldn't get tiddler %s: %s", c.String("name"), err)
return nil
}
fmt.Printf("%s", tiddler.JSON())
return nil
},
},
{
Name: "put",
Aliases: []string{"p"},
Flags: globalFlags,
Usage: "put a new tiddler",
Action: func(c *cli.Context) error {
fmt.Println("completed task: ", c.Args().First())
return nil
},
},
{
Name: "append",
Aliases: []string{"a"},
Flags: append([]cli.Flag{
&cli.StringFlag{
Name: "text",
Usage: "Text to append",
Destination: &appendText,
},
}, globalFlags...),
Usage: "Append content to an existing tiddler",
Action: func(c *cli.Context) error {
tw := tiddlywiki.NewTW(conf.TWHOST)
_, err := tw.Get("Golang")
if err != nil {
fmt.Printf("Errorf: %s", err)
return err
}
// Try to get tiddler
tiddler, err := tw.Get(c.String("name"))
if err != nil {
fmt.Printf("Couldn't get tiddler %s: %s", c.String("name"), err)
return nil
}
// Append
err = tw.Append(tiddler.Title, appendText)
if err != nil {
fmt.Printf("Couldn't append to %s", c.String("name"))
return nil
}
return nil
},
},
{
Name: "delete",
Aliases: []string{"d"},
Flags: globalFlags,
Usage: "delete tiddler",
Action: func(c *cli.Context) error {
fmt.Println("completed task: ", c.Args().First())
return nil
},
},
}}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}