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

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