github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/cmd/ipfs/tour.go (about)

     1  // +build linux darwin freebsd
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  
     8  	config "github.com/jbenet/go-ipfs/config"
     9  	tour "github.com/jbenet/go-ipfs/tour"
    10  
    11  	commander "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
    12  )
    13  
    14  var cmdIpfsTour = &commander.Command{
    15  	UsageLine: "tour [<number>]",
    16  	Short:     "Take the IPFS Tour.",
    17  	Long: `ipfs tour - Take the IPFS Tour.
    18  
    19      ipfs tour [<number>]   - Show tour topic. Default to current.
    20      ipfs tour next         - Show the next tour topic.
    21      ipfs tour list         - Show a list of topics.
    22      ipfs tour restart      - Restart the tour.
    23  
    24  This is a tour that takes you through various IPFS concepts,
    25  features, and tools to make sure you get up to speed with
    26  IPFS very quickly. To start, run:
    27  
    28      ipfs tour
    29  `,
    30  	Run: tourCmd,
    31  	Subcommands: []*commander.Command{
    32  		cmdIpfsTourNext,
    33  		cmdIpfsTourList,
    34  		cmdIpfsTourRestart,
    35  	},
    36  }
    37  
    38  var cmdIpfsTourNext = &commander.Command{
    39  	UsageLine: "next",
    40  	Short:     "Show the next IPFS Tour topic.",
    41  	Run:       tourNextCmd,
    42  }
    43  
    44  var cmdIpfsTourList = &commander.Command{
    45  	UsageLine: "list",
    46  	Short:     "Show a list of IPFS Tour topics.",
    47  	Run:       tourListCmd,
    48  }
    49  
    50  var cmdIpfsTourRestart = &commander.Command{
    51  	UsageLine: "restart",
    52  	Short:     "Restart the IPFS Tour.",
    53  	Run:       tourRestartCmd,
    54  }
    55  
    56  func tourCmd(c *commander.Command, inp []string) error {
    57  	cfg, err := getConfig(c)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	topic := tour.TopicID(cfg.Tour.Last)
    63  	if len(inp) > 0 {
    64  		topic = tour.TopicID(inp[0])
    65  	}
    66  	return tourShow(topic)
    67  }
    68  
    69  func tourNextCmd(c *commander.Command, _ []string) error {
    70  	cfg, err := getConfig(c)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	topic := tour.NextTopic(tour.TopicID(cfg.Tour.Last))
    76  	if err := tourShow(topic); err != nil {
    77  		return err
    78  	}
    79  
    80  	// if topic didn't change (last) done
    81  	if string(topic) == cfg.Tour.Last {
    82  		return nil
    83  	}
    84  
    85  	// topic changed, not last. write it out.
    86  	cfg.Tour.Last = string(topic)
    87  	return writeConfig(c, cfg)
    88  }
    89  
    90  func tourListCmd(c *commander.Command, _ []string) error {
    91  	cfg, err := getConfig(c)
    92  	if err != nil {
    93  		return err
    94  	}
    95  	lastid := tour.TopicID(cfg.Tour.Last)
    96  
    97  	for _, id := range tour.IDs {
    98  		c := ' '
    99  		switch {
   100  		case id == lastid:
   101  			c = '*'
   102  		case id.LessThan(lastid):
   103  			c = '✓'
   104  		}
   105  
   106  		t := tour.Topics[id]
   107  		fmt.Printf("- %c %-5.5s %s\n", c, id, t.Title)
   108  	}
   109  	return nil
   110  }
   111  
   112  func tourRestartCmd(c *commander.Command, _ []string) error {
   113  	cfg, err := getConfig(c)
   114  	if err != nil {
   115  		return err
   116  	}
   117  
   118  	cfg.Tour.Last = ""
   119  	return writeConfig(c, cfg)
   120  }
   121  
   122  func tourShow(id tour.ID) error {
   123  	t, found := tour.Topics[id]
   124  	if !found {
   125  		return fmt.Errorf("no topic with id: %s", id)
   126  	}
   127  
   128  	fmt.Printf("Tour %s - %s\n\n%s\n", t.ID, t.Title, t.Text)
   129  	return nil
   130  }
   131  
   132  func lastTour(cfg *config.Config) string {
   133  	return ""
   134  }