tour.go 3.33 KB
Newer Older
Brian Tiger Chow's avatar
Brian Tiger Chow committed
1 2 3 4 5 6 7 8 9
package main

import (
	"bytes"
	"fmt"
	"io"
	"os"

	cmds "github.com/jbenet/go-ipfs/commands"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
10 11 12
	config "github.com/jbenet/go-ipfs/config"
	internal "github.com/jbenet/go-ipfs/core/commands2/internal"
	tour "github.com/jbenet/go-ipfs/tour"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
13 14 15 16 17
)

var cmdTour = &cmds.Command{

	Arguments: []cmds.Argument{
Brian Tiger Chow's avatar
Brian Tiger Chow committed
18
		cmds.Argument{"number", cmds.ArgString, false, false},
Brian Tiger Chow's avatar
Brian Tiger Chow committed
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
	},

	// TODO UsageLine: "tour [<number>]",
	// TODO Short:     "Take the IPFS Tour.",

	Help: `ipfs tour - Take the IPFS Tour.

    ipfs tour [<number>]   - Show tour topic. Default to current.
    ipfs tour next         - Show the next tour topic.
    ipfs tour list         - Show a list of topics.
    ipfs tour restart      - Restart the tour.

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
`,
	Subcommands: map[string]*cmds.Command{
		"list":    cmdIpfsTourList,
		"next":    cmdIpfsTourNext,
		"restart": cmdIpfsTourRestart,
	},
	Run: func(res cmds.Response, req cmds.Request) {

		out := new(bytes.Buffer)
		cfg := req.Context().Config
		strs, err := internal.ToStrings(req.Arguments())
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		topic := tour.TopicID(cfg.Tour.Last)
		if len(strs) > 0 {
			topic = tour.TopicID(strs[0])
		}

		err = tourShow(out, topic)
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		res.SetOutput(out)
	},
}

var cmdIpfsTourNext = &cmds.Command{
	Help: "Show the next IPFS Tour topic.",
	Run: func(res cmds.Response, req cmds.Request) {
		var w bytes.Buffer
		cfg := req.Context().Config
		path := req.Context().ConfigRoot

		topic := tour.NextTopic(tour.TopicID(cfg.Tour.Last))
		if err := tourShow(&w, topic); err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		// topic changed, not last. write it out.
		if string(topic) != cfg.Tour.Last {
			cfg.Tour.Last = string(topic)
			err := writeConfig(path, cfg)
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}
		}

		w.WriteTo(os.Stdout) // TODO write to res.SetValue
	},
}

var cmdIpfsTourRestart = &cmds.Command{
	Help: "Restart the IPFS Tour.",
	Run: func(res cmds.Response, req cmds.Request) {
		path := req.Context().ConfigRoot
		cfg := req.Context().Config

		cfg.Tour.Last = ""
		err := writeConfig(path, cfg)
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
		}
	},
}

var cmdIpfsTourList = &cmds.Command{
	Help: "Show a list of IPFS Tour topics.",
	Run: func(res cmds.Response, req cmds.Request) {
		var w bytes.Buffer
		tourListCmd(&w, req.Context().Config)
		w.WriteTo(os.Stdout) // TODO use res.SetOutput(output)
	},
}

func tourListCmd(w io.Writer, cfg *config.Config) {

	lastid := tour.TopicID(cfg.Tour.Last)
	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)
	}
}

func tourShow(w io.Writer, id tour.ID) error {
	t, found := tour.Topics[id]
	if !found {
		return fmt.Errorf("no topic with id: %s", id)
	}

	fmt.Fprintf(w, "Tour %s - %s\n\n%s\n", t.ID, t.Title, t.Text)
	return nil
}

// TODO share func
func writeConfig(path string, cfg *config.Config) error {
	filename, err := config.Filename(path)
	if err != nil {
		return err
	}
	return config.WriteConfigFile(filename, cfg)
}