Commit 8edfee2a authored by Matt Bell's avatar Matt Bell Committed by Juan Batiz-Benet

cmd/ipfs: Removed new commands (to be refactored later)

parent c6797830
package main
import (
"errors"
"strings"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
config "github.com/jbenet/go-ipfs/config"
peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util"
)
var cmdIpfsBootstrap = &commander.Command{
UsageLine: "bootstrap",
Short: "Show a list of bootstrapped addresses.",
Long: `ipfs bootstrap - show, or manipulate bootstrap node addresses
Running 'ipfs bootstrap' with no arguments will run 'ipfs bootstrap list'.
Commands:
list Show the boostrap list.
add <address> Add a node's address to the bootstrap list.
remove <address> Remove an address from the bootstrap list.
` + bootstrapSecurityWarning,
Run: bootstrapListCmd,
Subcommands: []*commander.Command{
cmdIpfsBootstrapRemove,
cmdIpfsBootstrapAdd,
cmdIpfsBootstrapList,
},
Flag: *flag.NewFlagSet("ipfs-bootstrap", flag.ExitOnError),
}
var cmdIpfsBootstrapRemove = &commander.Command{
UsageLine: "remove <address | peerid>",
Short: "Remove addresses from the bootstrap list.",
Long: `ipfs bootstrap remove - remove addresses from the bootstrap list
` + bootstrapSecurityWarning,
Run: bootstrapRemoveCmd,
Flag: *flag.NewFlagSet("ipfs-bootstrap-remove", flag.ExitOnError),
}
var cmdIpfsBootstrapAdd = &commander.Command{
UsageLine: "add <address | peerid>",
Short: "Add addresses to the bootstrap list.",
Long: `ipfs bootstrap add - add addresses to the bootstrap list
` + bootstrapSecurityWarning,
Run: bootstrapAddCmd,
Flag: *flag.NewFlagSet("ipfs-bootstrap-add", flag.ExitOnError),
}
var cmdIpfsBootstrapList = &commander.Command{
UsageLine: "list",
Short: "Show addresses in the bootstrap list.",
Run: bootstrapListCmd,
Flag: *flag.NewFlagSet("ipfs-bootstrap-list", flag.ExitOnError),
}
func bootstrapRemoveCmd(c *commander.Command, inp []string) error {
if len(inp) == 0 {
return errors.New("remove: no address or peerid specified")
}
toRemove, err := bootstrapInputToPeers(inp)
if err != nil {
return err
}
cfg, err := getConfig(c)
if err != nil {
return err
}
keep := []*config.BootstrapPeer{}
remove := []*config.BootstrapPeer{}
// function to filer what to keep
shouldKeep := func(bp *config.BootstrapPeer) bool {
for _, skipBP := range toRemove {
// IDs must match to skip.
if bp.PeerID != skipBP.PeerID {
continue
}
// if Addresses match, or skipBP has no addr (wildcard)
if skipBP.Address == bp.Address || skipBP.Address == "" {
return false
}
}
return true
}
// filter all the existing peers
for _, currBP := range cfg.Bootstrap {
if shouldKeep(currBP) {
keep = append(keep, currBP)
} else {
remove = append(remove, currBP)
}
}
// if didn't remove anyone, bail.
if len(keep) == len(cfg.Bootstrap) {
return errors.New("remove: peer given did not match any in list")
}
// write new config
cfg.Bootstrap = keep
if err := writeConfig(c, cfg); err != nil {
return err
}
for _, bp := range remove {
u.POut("removed %s\n", bp)
}
return nil
}
func bootstrapAddCmd(c *commander.Command, inp []string) error {
if len(inp) == 0 {
return errors.New("add: no address specified")
}
toAdd, err := bootstrapInputToPeers(inp)
if err != nil {
return err
}
cfg, err := getConfig(c)
if err != nil {
return err
}
// function to check whether a peer is already in the list.
combine := func(lists ...[]*config.BootstrapPeer) []*config.BootstrapPeer {
set := map[string]struct{}{}
final := []*config.BootstrapPeer{}
for _, list := range lists {
for _, peer := range list {
// if already in the set, continue
_, found := set[peer.String()]
if found {
continue
}
set[peer.String()] = struct{}{}
final = append(final, peer)
}
}
return final
}
// combine both lists, removing dups.
cfg.Bootstrap = combine(cfg.Bootstrap, toAdd)
if err := writeConfig(c, cfg); err != nil {
return err
}
for _, bp := range toAdd {
u.POut("added %s\n", bp)
}
return nil
}
func bootstrapListCmd(c *commander.Command, inp []string) error {
cfg, err := getConfig(c)
if err != nil {
return err
}
for _, bp := range cfg.Bootstrap {
u.POut("%s\n", bp)
}
return nil
}
func bootstrapInputToPeers(input []string) ([]*config.BootstrapPeer, error) {
split := func(addr string) (string, string) {
idx := strings.LastIndex(addr, "/")
if idx == -1 {
return "", addr
}
return addr[:idx], addr[idx+1:]
}
peers := []*config.BootstrapPeer{}
for _, addr := range input {
addrS, peeridS := split(addr)
// make sure addrS parses as a multiaddr.
if len(addrS) > 0 {
maddr, err := ma.NewMultiaddr(addrS)
if err != nil {
return nil, err
}
addrS = maddr.String()
}
// make sure idS parses as a peer.ID
peerid, err := mh.FromB58String(peeridS)
if err != nil {
return nil, err
}
// construct config entry
peers = append(peers, &config.BootstrapPeer{
Address: addrS,
PeerID: peer.ID(peerid).Pretty(),
})
}
return peers, nil
}
const bootstrapSecurityWarning = `
SECURITY WARNING:
The bootstrap command manipulates the "bootstrap list", which contains
the addresses of bootstrap nodes. These are the *trusted peers* from
which to learn about other peers in the network. Only edit this list
if you understand the risks of adding or removing nodes from this list.
`
package main
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
config "github.com/jbenet/go-ipfs/config"
u "github.com/jbenet/go-ipfs/util"
)
var cmdIpfsConfig = &commander.Command{
UsageLine: "config",
Short: "Get/Set ipfs config values",
Long: `ipfs config [<key>] [<value>] - Get/Set ipfs config values.
ipfs config <key> - Get value of <key>
ipfs config <key> <value> - Set value of <key> to <value>
ipfs config --show - Show config file
ipfs config --edit - Edit config file in $EDITOR
Examples:
Get the value of the 'datastore.path' key:
ipfs config datastore.path
Set the value of the 'datastore.path' key:
ipfs config datastore.path ~/.go-ipfs/datastore
`,
Run: configCmd,
Flag: *flag.NewFlagSet("ipfs-config", flag.ExitOnError),
}
func init() {
cmdIpfsConfig.Flag.Bool("edit", false, "Edit config file in $EDITOR")
cmdIpfsConfig.Flag.Bool("show", false, "Show config file")
}
func configCmd(c *commander.Command, inp []string) error {
confdir, err := getConfigDir(c.Parent)
if err != nil {
return err
}
filename, err := config.Filename(confdir)
if err != nil {
return err
}
// if editing, open the editor
if c.Flag.Lookup("edit").Value.Get().(bool) {
return configEditor(filename)
}
// if showing, cat the file
if c.Flag.Lookup("show").Value.Get().(bool) {
return configCat(filename)
}
if len(inp) == 0 {
// "ipfs config" run without parameters
u.POut(c.Long)
return nil
}
// Getter (1 param)
if len(inp) == 1 {
value, err := config.ReadConfigKey(filename, inp[0])
if err != nil {
return fmt.Errorf("Failed to get config value: %s", err)
}
strval, ok := value.(string)
if ok {
u.POut("%s\n", strval)
return nil
}
if err := config.Encode(os.Stdout, value); err != nil {
return fmt.Errorf("Failed to encode config value: %s", err)
}
u.POut("\n")
return nil
}
// Setter (>1 params)
err = config.WriteConfigKey(filename, inp[0], inp[1])
if err != nil {
return fmt.Errorf("Failed to set config value: %s", err)
}
return nil
}
func configCat(filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
if _, err = io.Copy(os.Stdout, file); err != nil {
return err
}
u.POut("\n")
return nil
}
func configEditor(filename string) error {
editor := os.Getenv("EDITOR")
if editor == "" {
return errors.New("ENV variable $EDITOR not set")
}
cmd := exec.Command("sh", "-c", editor+" "+filename)
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
return cmd.Run()
}
func writeConfig(c *commander.Command, cfg *config.Config) error {
confdir, err := getConfigDir(c)
if err != nil {
return err
}
filename, err := config.Filename(confdir)
if err != nil {
return err
}
return config.WriteConfigFile(filename, cfg)
}
// +build linux darwin freebsd
package main
import (
"fmt"
config "github.com/jbenet/go-ipfs/config"
tour "github.com/jbenet/go-ipfs/tour"
commander "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
)
var cmdIpfsTour = &commander.Command{
UsageLine: "tour [<number>]",
Short: "Take the IPFS Tour.",
Long: `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
`,
Run: tourCmd,
Subcommands: []*commander.Command{
cmdIpfsTourNext,
cmdIpfsTourList,
cmdIpfsTourRestart,
},
}
var cmdIpfsTourNext = &commander.Command{
UsageLine: "next",
Short: "Show the next IPFS Tour topic.",
Run: tourNextCmd,
}
var cmdIpfsTourList = &commander.Command{
UsageLine: "list",
Short: "Show a list of IPFS Tour topics.",
Run: tourListCmd,
}
var cmdIpfsTourRestart = &commander.Command{
UsageLine: "restart",
Short: "Restart the IPFS Tour.",
Run: tourRestartCmd,
}
func tourCmd(c *commander.Command, inp []string) error {
cfg, err := getConfig(c)
if err != nil {
return err
}
topic := tour.TopicID(cfg.Tour.Last)
if len(inp) > 0 {
topic = tour.TopicID(inp[0])
}
return tourShow(topic)
}
func tourNextCmd(c *commander.Command, _ []string) error {
cfg, err := getConfig(c)
if err != nil {
return err
}
topic := tour.NextTopic(tour.TopicID(cfg.Tour.Last))
if err := tourShow(topic); err != nil {
return err
}
// if topic didn't change (last) done
if string(topic) == cfg.Tour.Last {
return nil
}
// topic changed, not last. write it out.
cfg.Tour.Last = string(topic)
return writeConfig(c, cfg)
}
func tourListCmd(c *commander.Command, _ []string) error {
cfg, err := getConfig(c)
if err != nil {
return err
}
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.Printf("- %c %-5.5s %s\n", c, id, t.Title)
}
return nil
}
func tourRestartCmd(c *commander.Command, _ []string) error {
cfg, err := getConfig(c)
if err != nil {
return err
}
cfg.Tour.Last = ""
return writeConfig(c, cfg)
}
func tourShow(id tour.ID) error {
t, found := tour.Topics[id]
if !found {
return fmt.Errorf("no topic with id: %s", id)
}
fmt.Printf("Tour %s - %s\n\n%s\n", t.ID, t.Title, t.Text)
return nil
}
func lastTour(cfg *config.Config) string {
return ""
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment