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

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  	"os/exec"
     9  
    10  	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
    11  	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
    12  	config "github.com/jbenet/go-ipfs/config"
    13  	u "github.com/jbenet/go-ipfs/util"
    14  )
    15  
    16  var cmdIpfsConfig = &commander.Command{
    17  	UsageLine: "config",
    18  	Short:     "Get/Set ipfs config values",
    19  	Long: `ipfs config [<key>] [<value>] - Get/Set ipfs config values.
    20  
    21      ipfs config <key>          - Get value of <key>
    22      ipfs config <key> <value>  - Set value of <key> to <value>
    23      ipfs config --show         - Show config file
    24      ipfs config --edit         - Edit config file in $EDITOR
    25  
    26  Examples:
    27  
    28    Get the value of the 'datastore.path' key:
    29  
    30        ipfs config datastore.path
    31  
    32    Set the value of the 'datastore.path' key:
    33  
    34        ipfs config datastore.path ~/.go-ipfs/datastore
    35  
    36  `,
    37  	Run:  configCmd,
    38  	Flag: *flag.NewFlagSet("ipfs-config", flag.ExitOnError),
    39  }
    40  
    41  func init() {
    42  	cmdIpfsConfig.Flag.Bool("edit", false, "Edit config file in $EDITOR")
    43  	cmdIpfsConfig.Flag.Bool("show", false, "Show config file")
    44  }
    45  
    46  func configCmd(c *commander.Command, inp []string) error {
    47  
    48  	confdir, err := getConfigDir(c.Parent)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	filename, err := config.Filename(confdir)
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	// if editing, open the editor
    59  	if c.Flag.Lookup("edit").Value.Get().(bool) {
    60  		return configEditor(filename)
    61  	}
    62  
    63  	// if showing, cat the file
    64  	if c.Flag.Lookup("show").Value.Get().(bool) {
    65  		return configCat(filename)
    66  	}
    67  
    68  	if len(inp) == 0 {
    69  		// "ipfs config" run without parameters
    70  		u.POut(c.Long)
    71  		return nil
    72  	}
    73  
    74  	// Getter (1 param)
    75  	if len(inp) == 1 {
    76  		value, err := config.ReadConfigKey(filename, inp[0])
    77  		if err != nil {
    78  			return fmt.Errorf("Failed to get config value: %s", err)
    79  		}
    80  
    81  		strval, ok := value.(string)
    82  		if ok {
    83  			u.POut("%s\n", strval)
    84  			return nil
    85  		}
    86  
    87  		if err := config.Encode(os.Stdout, value); err != nil {
    88  			return fmt.Errorf("Failed to encode config value: %s", err)
    89  		}
    90  		u.POut("\n")
    91  		return nil
    92  	}
    93  
    94  	// Setter (>1 params)
    95  	err = config.WriteConfigKey(filename, inp[0], inp[1])
    96  	if err != nil {
    97  		return fmt.Errorf("Failed to set config value: %s", err)
    98  	}
    99  
   100  	return nil
   101  }
   102  
   103  func configCat(filename string) error {
   104  
   105  	file, err := os.Open(filename)
   106  	if err != nil {
   107  		return err
   108  	}
   109  	defer file.Close()
   110  
   111  	if _, err = io.Copy(os.Stdout, file); err != nil {
   112  		return err
   113  	}
   114  	u.POut("\n")
   115  	return nil
   116  }
   117  
   118  func configEditor(filename string) error {
   119  
   120  	editor := os.Getenv("EDITOR")
   121  	if editor == "" {
   122  		return errors.New("ENV variable $EDITOR not set")
   123  	}
   124  
   125  	cmd := exec.Command("sh", "-c", editor+" "+filename)
   126  	cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
   127  	return cmd.Run()
   128  }
   129  
   130  func writeConfig(c *commander.Command, cfg *config.Config) error {
   131  
   132  	confdir, err := getConfigDir(c)
   133  	if err != nil {
   134  		return err
   135  	}
   136  
   137  	filename, err := config.Filename(confdir)
   138  	if err != nil {
   139  		return err
   140  	}
   141  
   142  	return config.WriteConfigFile(filename, cfg)
   143  }