forked from ipfs/kubo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tour.go
202 lines (171 loc) · 4.15 KB
/
tour.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package commands
import (
"bytes"
"fmt"
"html/template"
"io"
cmds "github.com/ipfs/go-ipfs/commands"
config "github.com/ipfs/go-ipfs/repo/config"
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
tour "github.com/ipfs/go-ipfs/tour"
)
var tourCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "An introduction to IPFS",
ShortDescription: `
This is a tour that takes you through various IPFS concepts,
features, and tools to make sure you get up to speed with
IPFS very quickly. To start, run:
ipfs tour
`,
},
Arguments: []cmds.Argument{
cmds.StringArg("id", false, false, "The id of the topic you would like to tour"),
},
Subcommands: map[string]*cmds.Command{
"list": cmdIpfsTourList,
"next": cmdIpfsTourNext,
"restart": cmdIpfsTourRestart,
},
Run: tourRunFunc,
}
func tourRunFunc(req cmds.Request, res cmds.Response) {
cfg, err := req.InvocContext().GetConfig()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
id := tour.TopicID(cfg.Tour.Last)
if len(req.Arguments()) > 0 {
id = tour.TopicID(req.Arguments()[0])
}
w := new(bytes.Buffer)
t, err := tourGet(id)
if err != nil {
// If no topic exists for this id, we handle this error right here.
// To help the user achieve the task, we construct a response
// comprised of...
// 1) a simple error message
// 2) the full list of topics
fmt.Fprintln(w, "ERROR")
fmt.Fprintln(w, err)
fmt.Fprintln(w, "")
fprintTourList(w, tour.TopicID(cfg.Tour.Last))
res.SetOutput(w)
return
}
fprintTourShow(w, t)
res.SetOutput(w)
}
var cmdIpfsTourNext = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Show the next IPFS Tour topic",
},
Run: func(req cmds.Request, res cmds.Response) {
w := new(bytes.Buffer)
path := req.InvocContext().ConfigRoot
cfg, err := req.InvocContext().GetConfig()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
id := tour.NextTopic(tour.TopicID(cfg.Tour.Last))
topic, err := tourGet(id)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
if err := fprintTourShow(w, topic); err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
// topic changed, not last. write it out.
if string(id) != cfg.Tour.Last {
cfg.Tour.Last = string(id)
err := writeConfig(path, cfg)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
}
res.SetOutput(w)
},
}
var cmdIpfsTourRestart = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Restart the IPFS Tour",
},
Run: func(req cmds.Request, res cmds.Response) {
path := req.InvocContext().ConfigRoot
cfg, err := req.InvocContext().GetConfig()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
cfg.Tour.Last = ""
err = writeConfig(path, cfg)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
},
}
var cmdIpfsTourList = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Show a list of IPFS Tour topics",
},
Run: func(req cmds.Request, res cmds.Response) {
cfg, err := req.InvocContext().GetConfig()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
w := new(bytes.Buffer)
fprintTourList(w, tour.TopicID(cfg.Tour.Last))
res.SetOutput(w)
},
}
func fprintTourList(w io.Writer, lastid tour.ID) {
for _, id := range tour.IDs {
c := ' '
switch {
case id == lastid:
c = '*'
case id.LessThan(lastid):
c = '✓'
}
t := tour.Topics[id]
fmt.Fprintf(w, "- %c %-5.5s %s\n", c, id, t.Title)
}
}
// fprintTourShow writes a text-formatted topic to the writer
func fprintTourShow(w io.Writer, t *tour.Topic) error {
tmpl := `
Tour {{ .ID }} - {{ .Title }}
{{ .Text }}
`
ttempl, err := template.New("tour").Parse(tmpl)
if err != nil {
return err
}
return ttempl.Execute(w, t)
}
// tourGet returns the topic given its ID. Returns an error if topic does not
// exist.
func tourGet(id tour.ID) (*tour.Topic, error) {
t, found := tour.Topics[id]
if !found {
return nil, fmt.Errorf("no topic with id: %s", id)
}
return &t, nil
}
// TODO share func
func writeConfig(path string, cfg *config.Config) error {
// NB: This needs to run on the daemon.
r, err := fsrepo.Open(path)
if err != nil {
return err
}
defer r.Close()
return r.SetConfig(cfg)
}