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

     1  package commands
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"io"
     8  	"os"
     9  	"os/exec"
    10  
    11  	cmds "github.com/jbenet/go-ipfs/commands"
    12  	config "github.com/jbenet/go-ipfs/config"
    13  )
    14  
    15  type ConfigField struct {
    16  	Key   string
    17  	Value interface{}
    18  }
    19  
    20  var configCmd = &cmds.Command{
    21  	Description: "Get/set IPFS config values",
    22  	Help: `Examples:
    23  
    24    Get the value of the 'datastore.path' key:
    25  
    26        ipfs config datastore.path
    27  
    28    Set the value of the 'datastore.path' key:
    29  
    30        ipfs config datastore.path ~/.go-ipfs/datastore
    31  `,
    32  
    33  	Arguments: []cmds.Argument{
    34  		cmds.StringArg("key", true, false, "The key of the config entry (e.g. \"Addresses.API\")"),
    35  		cmds.StringArg("value", false, false, "The value to set the config entry to"),
    36  	},
    37  	Run: func(req cmds.Request) (interface{}, error) {
    38  		args := req.Arguments()
    39  
    40  		key, ok := args[0].(string)
    41  		if !ok {
    42  			return nil, errors.New("cast error")
    43  		}
    44  
    45  		filename, err := config.Filename(req.Context().ConfigRoot)
    46  		if err != nil {
    47  			return nil, err
    48  		}
    49  
    50  		var value string
    51  		if len(args) == 2 {
    52  			var ok bool
    53  			value, ok = args[1].(string)
    54  			if !ok {
    55  				return nil, errors.New("cast error")
    56  			}
    57  
    58  			return setConfig(filename, key, value)
    59  
    60  		} else {
    61  			return getConfig(filename, key)
    62  		}
    63  	},
    64  	Marshallers: map[cmds.EncodingType]cmds.Marshaller{
    65  		cmds.Text: func(res cmds.Response) ([]byte, error) {
    66  			v := res.Output().(*ConfigField)
    67  
    68  			s := ""
    69  			if len(res.Request().Arguments()) == 2 {
    70  				s += fmt.Sprintf("'%s' set to: ", v.Key)
    71  			}
    72  
    73  			marshalled, err := json.Marshal(v.Value)
    74  			if err != nil {
    75  				return nil, err
    76  			}
    77  			s += fmt.Sprintf("%s\n", marshalled)
    78  
    79  			return []byte(s), nil
    80  		},
    81  	},
    82  	Type: &ConfigField{},
    83  	Subcommands: map[string]*cmds.Command{
    84  		"show": configShowCmd,
    85  		"edit": configEditCmd,
    86  	},
    87  }
    88  
    89  var configShowCmd = &cmds.Command{
    90  	Description: "Outputs the content of the config file",
    91  	Help: `WARNING: Your private key is stored in the config file, and it will be
    92  included in the output of this command.
    93  `,
    94  
    95  	Run: func(req cmds.Request) (interface{}, error) {
    96  		filename, err := config.Filename(req.Context().ConfigRoot)
    97  		if err != nil {
    98  			return nil, err
    99  		}
   100  
   101  		return showConfig(filename)
   102  	},
   103  }
   104  
   105  var configEditCmd = &cmds.Command{
   106  	Description: "Opens the config file for editing in $EDITOR",
   107  	Help: `To use 'ipfs config edit', you must have the $EDITOR environment
   108  variable set to your preferred text editor.
   109  `,
   110  
   111  	Run: func(req cmds.Request) (interface{}, error) {
   112  		filename, err := config.Filename(req.Context().ConfigRoot)
   113  		if err != nil {
   114  			return nil, err
   115  		}
   116  
   117  		return nil, editConfig(filename)
   118  	},
   119  }
   120  
   121  func getConfig(filename string, key string) (*ConfigField, error) {
   122  	value, err := config.ReadConfigKey(filename, key)
   123  	if err != nil {
   124  		return nil, fmt.Errorf("Failed to get config value: %s", err)
   125  	}
   126  
   127  	return &ConfigField{
   128  		Key:   key,
   129  		Value: value,
   130  	}, nil
   131  }
   132  
   133  func setConfig(filename string, key, value string) (*ConfigField, error) {
   134  	err := config.WriteConfigKey(filename, key, value)
   135  	if err != nil {
   136  		return nil, fmt.Errorf("Failed to set config value: %s", err)
   137  	}
   138  
   139  	return getConfig(filename, key)
   140  }
   141  
   142  func showConfig(filename string) (io.Reader, error) {
   143  	// MAYBE_TODO: maybe we should omit privkey so we don't accidentally leak it?
   144  
   145  	file, err := os.Open(filename)
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  	//defer file.Close()
   150  
   151  	return file, nil
   152  }
   153  
   154  func editConfig(filename string) error {
   155  	editor := os.Getenv("EDITOR")
   156  	if editor == "" {
   157  		return errors.New("ENV variable $EDITOR not set")
   158  	}
   159  
   160  	cmd := exec.Command("sh", "-c", editor+" "+filename)
   161  	cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
   162  	return cmd.Run()
   163  }