fix(tour) patch up and verify tour output
License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
Brian Tiger Chow committed
Nov 13, 2014 at 20:56 UTC
35da357dc56a6688286c5f6feb3df689cfbbdc3d
1 file changed
+19
-35
cmd/ipfs2/tour.go
+19
-35
@@ -47,6 +47,8 @@ IPFS very quickly. To start, run:
47
// tourOutput is a union type. It either contains a Topic or it contains the
48
// list of Topics and an Error.
49
type tourOutput struct {
50
+ Last tour.ID
51
+
52
Topic *tour.Topic
53
54
Topics []tour.Topic
@@ -68,23 +70,14 @@ func tourTextMarshaler(r cmds.Response) ([]byte, error) {
70
}
71
72
func printTourOutput(w io.Writer, output *tourOutput) error {
71
- tmpl := `{{ if .Error }}
72
-ERROR
73
- {{ .Error }}
74
-TOPICS
75
- {{ range $topic := .Topics }}
76
- {{ $topic.ID }} - {{ $topic.Title }} {{ end }}
77
-{{ else if .Topic }}
78
-Tour {{ .Topic.ID }} - {{ .Topic.Title }}
79
-
80
-{{ .Topic.Text }}
81
-{{ end }}
82
-`
83
- tourTmpl, err := template.New("tour").Parse(tmpl)
84
- if err != nil {
85
- return err
73
+ if output.Error != nil {
74
+ fmt.Fprintln(w, "ERROR")
75
+ fmt.Fprintln(w, output.Error.Error())
76
+ fmt.Fprintln(w, "")
77
+ fprintTourList(w, output.Last)
78
+ return nil // TODO err
79
}
87
- return tourTmpl.Execute(w, output)
80
+ return fprintTourShow(w, output.Topic)
81
}
82
83
func tourRunFunc(req cmds.Request) (interface{}, error) {
@@ -115,23 +108,15 @@ func tourRunFunc(req cmds.Request) (interface{}, error) {
108
109
output := &tourOutput{
110
Error: err,
118
- }
119
- for _, id := range tour.IDs {
120
- t, ok := tour.Topics[id]
121
- if !ok {
122
- return nil, err
123
- }
124
- output.Topics = append(output.Topics, t)
111
+ Last: tour.TopicID(cfg.Tour.Last),
112
}
113
114
return output, nil
128
- // return nil, cmds.ClientError(err.Error())
115
}
116
117
return &tourOutput{Topic: t}, nil
118
}
119
134
-// TODO use tourOutput like parent command
120
var cmdIpfsTourNext = &cmds.Command{
121
Helptext: cmds.HelpText{
122
Tagline: "Show the next IPFS Tour topic",
@@ -150,7 +135,7 @@ var cmdIpfsTourNext = &cmds.Command{
135
if err != nil {
136
return nil, err
137
}
153
- if err := tourShow(&w, topic); err != nil {
138
+ if err := fprintTourShow(&w, topic); err != nil {
139
return nil, err
140
}
141
@@ -163,8 +148,8 @@ var cmdIpfsTourNext = &cmds.Command{
148
}
149
}
150
166
- w.WriteTo(os.Stdout) // TODO write to res.SetValue
167
- return w, nil
151
+ w.WriteTo(os.Stdout)
152
+ return nil, nil
153
},
154
}
155
@@ -189,7 +174,6 @@ var cmdIpfsTourRestart = &cmds.Command{
174
},
175
}
176
192
-// TODO use tourOutput like parent command
177
var cmdIpfsTourList = &cmds.Command{
178
Helptext: cmds.HelpText{
179
Tagline: "Show a list of IPFS Tour topics",
@@ -202,15 +186,13 @@ var cmdIpfsTourList = &cmds.Command{
186
}
187
188
var w bytes.Buffer
205
- tourListCmd(&w, cfg)
189
+ fprintTourList(&w, tour.TopicID(cfg.Tour.Last))
190
w.WriteTo(os.Stdout) // TODO use res.SetOutput(output)
191
return nil, nil
192
},
193
}
194
211
-func tourListCmd(w io.Writer, cfg *config.Config) {
212
-
213
- lastid := tour.TopicID(cfg.Tour.Last)
195
+func fprintTourList(w io.Writer, lastid tour.ID) {
196
for _, id := range tour.IDs {
197
c := ' '
198
switch {
@@ -225,7 +207,8 @@ func tourListCmd(w io.Writer, cfg *config.Config) {
207
}
208
}
209
228
-func tourShow(w io.Writer, t *tour.Topic) error {
210
+// fprintTourShow writes a text-formatted topic to the writer
211
+func fprintTourShow(w io.Writer, t *tour.Topic) error {
212
tmpl := `
213
Tour {{ .ID }} - {{ .Title }}
214
@@ -239,7 +222,8 @@ Tour {{ .ID }} - {{ .Title }}
222
return ttempl.Execute(w, t)
223
}
224
242
-// tourGet returns an error if topic does not exist
225
+// tourGet returns the topic given its ID. Returns an error if topic does not
226
+// exist.
227
func tourGet(id tour.ID) (*tour.Topic, error) {
228
t, found := tour.Topics[id]
229
if !found {