@cryptotaxi247 / kubo / commits / 520240d38

feat(commands2/tour) impl

Brian Tiger Chow committed Nov 7, 2014 at 07:47 UTC 520240d3850b2242d0b91cb32989bc5af3bf18ef
2 files changed +154 -2
cmd/ipfs2/ipfs.go
+3 -2
@@ -9,7 +9,8 @@ var Root = &cmds.Command{
9 Options: commands.Root.Options,
10 Help: commands.Root.Help,
11 Subcommands: map[string]*cmds.Command{
12 - "daemon": daemonCmd,
13 - "init": initCmd,
12 + "daemon": daemonCmd, // TODO name
13 + "init": initCmd, // TODO name
14 + "tour": cmdTour,
15 },
16 }
cmd/ipfs2/tour.go new
+151
@@ -0,0 +1,151 @@
1 +package main
2 +
3 +import (
4 + "bytes"
5 + "fmt"
6 + "io"
7 + "os"
8 +
9 + cmds "github.com/jbenet/go-ipfs/commands"
10 + "github.com/jbenet/go-ipfs/config"
11 + "github.com/jbenet/go-ipfs/core/commands2/internal"
12 + "github.com/jbenet/go-ipfs/tour"
13 +)
14 +
15 +var cmdTour = &cmds.Command{
16 +
17 + Arguments: []cmds.Argument{
18 + cmds.Argument{"number", cmds.ArgString, false, true},
19 + },
20 +
21 + // TODO UsageLine: "tour [<number>]",
22 + // TODO Short: "Take the IPFS Tour.",
23 +
24 + Help: `ipfs tour - Take the IPFS Tour.
25 +
26 + ipfs tour [<number>] - Show tour topic. Default to current.
27 + ipfs tour next - Show the next tour topic.
28 + ipfs tour list - Show a list of topics.
29 + ipfs tour restart - Restart the tour.
30 +
31 +This is a tour that takes you through various IPFS concepts,
32 +features, and tools to make sure you get up to speed with
33 +IPFS very quickly. To start, run:
34 +
35 + ipfs tour
36 +`,
37 + Subcommands: map[string]*cmds.Command{
38 + "list": cmdIpfsTourList,
39 + "next": cmdIpfsTourNext,
40 + "restart": cmdIpfsTourRestart,
41 + },
42 + Run: func(res cmds.Response, req cmds.Request) {
43 +
44 + out := new(bytes.Buffer)
45 + cfg := req.Context().Config
46 + strs, err := internal.ToStrings(req.Arguments())
47 + if err != nil {
48 + res.SetError(err, cmds.ErrNormal)
49 + return
50 + }
51 +
52 + topic := tour.TopicID(cfg.Tour.Last)
53 + if len(strs) > 0 {
54 + topic = tour.TopicID(strs[0])
55 + }
56 +
57 + err = tourShow(out, topic)
58 + if err != nil {
59 + res.SetError(err, cmds.ErrNormal)
60 + return
61 + }
62 +
63 + res.SetOutput(out)
64 + },
65 +}
66 +
67 +var cmdIpfsTourNext = &cmds.Command{
68 + Help: "Show the next IPFS Tour topic.",
69 + Run: func(res cmds.Response, req cmds.Request) {
70 + var w bytes.Buffer
71 + cfg := req.Context().Config
72 + path := req.Context().ConfigRoot
73 +
74 + topic := tour.NextTopic(tour.TopicID(cfg.Tour.Last))
75 + if err := tourShow(&w, topic); err != nil {
76 + res.SetError(err, cmds.ErrNormal)
77 + return
78 + }
79 +
80 + // topic changed, not last. write it out.
81 + if string(topic) != cfg.Tour.Last {
82 + cfg.Tour.Last = string(topic)
83 + err := writeConfig(path, cfg)
84 + if err != nil {
85 + res.SetError(err, cmds.ErrNormal)
86 + return
87 + }
88 + }
89 +
90 + w.WriteTo(os.Stdout) // TODO write to res.SetValue
91 + },
92 +}
93 +
94 +var cmdIpfsTourRestart = &cmds.Command{
95 + Help: "Restart the IPFS Tour.",
96 + Run: func(res cmds.Response, req cmds.Request) {
97 + path := req.Context().ConfigRoot
98 + cfg := req.Context().Config
99 +
100 + cfg.Tour.Last = ""
101 + err := writeConfig(path, cfg)
102 + if err != nil {
103 + res.SetError(err, cmds.ErrNormal)
104 + }
105 + },
106 +}
107 +
108 +var cmdIpfsTourList = &cmds.Command{
109 + Help: "Show a list of IPFS Tour topics.",
110 + Run: func(res cmds.Response, req cmds.Request) {
111 + var w bytes.Buffer
112 + tourListCmd(&w, req.Context().Config)
113 + w.WriteTo(os.Stdout) // TODO use res.SetOutput(output)
114 + },
115 +}
116 +
117 +func tourListCmd(w io.Writer, cfg *config.Config) {
118 +
119 + lastid := tour.TopicID(cfg.Tour.Last)
120 + for _, id := range tour.IDs {
121 + c := ' '
122 + switch {
123 + case id == lastid:
124 + c = '*'
125 + case id.LessThan(lastid):
126 + c = '✓'
127 + }
128 +
129 + t := tour.Topics[id]
130 + fmt.Fprintf(w, "- %c %-5.5s %s\n", c, id, t.Title)
131 + }
132 +}
133 +
134 +func tourShow(w io.Writer, id tour.ID) error {
135 + t, found := tour.Topics[id]
136 + if !found {
137 + return fmt.Errorf("no topic with id: %s", id)
138 + }
139 +
140 + fmt.Fprintf(w, "Tour %s - %s\n\n%s\n", t.ID, t.Title, t.Text)
141 + return nil
142 +}
143 +
144 +// TODO share func
145 +func writeConfig(path string, cfg *config.Config) error {
146 + filename, err := config.Filename(path)
147 + if err != nil {
148 + return err
149 + }
150 + return config.WriteConfigFile(filename, cfg)
151 +}