1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-03 13:00:37 +08:00

fix(tour) patch up and verify tour output

License: MIT
Signed-off-by: Brian Tiger Chow <brian@perfmode.com>
This commit is contained in:
Brian Tiger Chow
2014-11-13 20:56:50 -08:00
committed by Juan Batiz-Benet
parent c99e9e6000
commit 35da357dc5

View File

@ -47,6 +47,8 @@ IPFS very quickly. To start, run:
// tourOutput is a union type. It either contains a Topic or it contains the // tourOutput is a union type. It either contains a Topic or it contains the
// list of Topics and an Error. // list of Topics and an Error.
type tourOutput struct { type tourOutput struct {
Last tour.ID
Topic *tour.Topic Topic *tour.Topic
Topics []tour.Topic Topics []tour.Topic
@ -68,23 +70,14 @@ func tourTextMarshaler(r cmds.Response) ([]byte, error) {
} }
func printTourOutput(w io.Writer, output *tourOutput) error { func printTourOutput(w io.Writer, output *tourOutput) error {
tmpl := `{{ if .Error }} if output.Error != nil {
ERROR fmt.Fprintln(w, "ERROR")
{{ .Error }} fmt.Fprintln(w, output.Error.Error())
TOPICS fmt.Fprintln(w, "")
{{ range $topic := .Topics }} fprintTourList(w, output.Last)
{{ $topic.ID }} - {{ $topic.Title }} {{ end }} return nil // TODO err
{{ else if .Topic }}
Tour {{ .Topic.ID }} - {{ .Topic.Title }}
{{ .Topic.Text }}
{{ end }}
`
tourTmpl, err := template.New("tour").Parse(tmpl)
if err != nil {
return err
} }
return tourTmpl.Execute(w, output) return fprintTourShow(w, output.Topic)
} }
func tourRunFunc(req cmds.Request) (interface{}, error) { func tourRunFunc(req cmds.Request) (interface{}, error) {
@ -115,23 +108,15 @@ func tourRunFunc(req cmds.Request) (interface{}, error) {
output := &tourOutput{ output := &tourOutput{
Error: err, Error: err,
} Last: tour.TopicID(cfg.Tour.Last),
for _, id := range tour.IDs {
t, ok := tour.Topics[id]
if !ok {
return nil, err
}
output.Topics = append(output.Topics, t)
} }
return output, nil return output, nil
// return nil, cmds.ClientError(err.Error())
} }
return &tourOutput{Topic: t}, nil return &tourOutput{Topic: t}, nil
} }
// TODO use tourOutput like parent command
var cmdIpfsTourNext = &cmds.Command{ var cmdIpfsTourNext = &cmds.Command{
Helptext: cmds.HelpText{ Helptext: cmds.HelpText{
Tagline: "Show the next IPFS Tour topic", Tagline: "Show the next IPFS Tour topic",
@ -150,7 +135,7 @@ var cmdIpfsTourNext = &cmds.Command{
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := tourShow(&w, topic); err != nil { if err := fprintTourShow(&w, topic); err != nil {
return nil, err return nil, err
} }
@ -163,8 +148,8 @@ var cmdIpfsTourNext = &cmds.Command{
} }
} }
w.WriteTo(os.Stdout) // TODO write to res.SetValue w.WriteTo(os.Stdout)
return w, nil return nil, nil
}, },
} }
@ -189,7 +174,6 @@ var cmdIpfsTourRestart = &cmds.Command{
}, },
} }
// TODO use tourOutput like parent command
var cmdIpfsTourList = &cmds.Command{ var cmdIpfsTourList = &cmds.Command{
Helptext: cmds.HelpText{ Helptext: cmds.HelpText{
Tagline: "Show a list of IPFS Tour topics", Tagline: "Show a list of IPFS Tour topics",
@ -202,15 +186,13 @@ var cmdIpfsTourList = &cmds.Command{
} }
var w bytes.Buffer var w bytes.Buffer
tourListCmd(&w, cfg) fprintTourList(&w, tour.TopicID(cfg.Tour.Last))
w.WriteTo(os.Stdout) // TODO use res.SetOutput(output) w.WriteTo(os.Stdout) // TODO use res.SetOutput(output)
return nil, nil return nil, nil
}, },
} }
func tourListCmd(w io.Writer, cfg *config.Config) { func fprintTourList(w io.Writer, lastid tour.ID) {
lastid := tour.TopicID(cfg.Tour.Last)
for _, id := range tour.IDs { for _, id := range tour.IDs {
c := ' ' c := ' '
switch { switch {
@ -225,7 +207,8 @@ func tourListCmd(w io.Writer, cfg *config.Config) {
} }
} }
func tourShow(w io.Writer, t *tour.Topic) error { // fprintTourShow writes a text-formatted topic to the writer
func fprintTourShow(w io.Writer, t *tour.Topic) error {
tmpl := ` tmpl := `
Tour {{ .ID }} - {{ .Title }} Tour {{ .ID }} - {{ .Title }}
@ -239,7 +222,8 @@ Tour {{ .ID }} - {{ .Title }}
return ttempl.Execute(w, t) return ttempl.Execute(w, t)
} }
// tourGet returns an error if topic does not exist // tourGet returns the topic given its ID. Returns an error if topic does not
// exist.
func tourGet(id tour.ID) (*tour.Topic, error) { func tourGet(id tour.ID) (*tour.Topic, error) {
t, found := tour.Topics[id] t, found := tour.Topics[id]
if !found { if !found {