config.go 4.26 KB
Newer Older
1 2 3
package commands

import (
4
	"bytes"
5 6 7
	"errors"
	"fmt"
	"io"
8
	"io/ioutil"
9 10 11 12 13
	"os"
	"os/exec"

	cmds "github.com/jbenet/go-ipfs/commands"
	config "github.com/jbenet/go-ipfs/config"
14
	u "github.com/jbenet/go-ipfs/util"
15 16 17 18 19 20 21
)

type ConfigField struct {
	Key   string
	Value interface{}
}

22
var ConfigCmd = &cmds.Command{
23 24 25 26 27
	Helptext: cmds.HelpText{
		Tagline: "get and set IPFS config values",
		Synopsis: `
ipfs config <key>          - Get value of <key>
ipfs config <key> <value>  - Set value of <key> to <value>
28 29
ipfs config show           - Show config file
ipfs config edit           - Edit config file in $EDITOR
30 31 32 33 34 35 36 37 38 39 40
`,
		ShortDescription: `
ipfs config controls configuration variables. It works like 'git config'.
The configuration values are stored in a config file inside your IPFS
repository.`,
		LongDescription: `
ipfs config controls configuration variables. It works
much like 'git config'. The configuration values are stored in a config
file inside your IPFS repository.

EXAMPLES:
41

42
Get the value of the 'datastore.path' key:
43

44
  ipfs config datastore.path
45

46
Set the value of the 'datastore.path' key:
47

48
  ipfs config datastore.path ~/.go-ipfs/datastore
49
`,
50
	},
51 52

	Arguments: []cmds.Argument{
53 54
		cmds.StringArg("key", true, false, "The key of the config entry (e.g. \"Addresses.API\")"),
		cmds.StringArg("value", false, false, "The value to set the config entry to"),
55
	},
56
	Run: func(req cmds.Request) (interface{}, error) {
57
		args := req.Arguments()
58
		key := args[0]
59 60 61

		filename, err := config.Filename(req.Context().ConfigRoot)
		if err != nil {
62
			return nil, err
63 64 65 66
		}

		var value string
		if len(args) == 2 {
67
			value = args[1]
68
			return setConfig(filename, key, value)
69 70

		} else {
71
			return getConfig(filename, key)
72 73
		}
	},
74
	Marshalers: cmds.MarshalerMap{
75
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
76
			if len(res.Request().Arguments()) == 2 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
77
				return nil, nil // dont output anything
78 79
			}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80 81 82 83 84 85 86 87 88 89 90
			v := res.Output()
			if v == nil {
				k := res.Request().Arguments()[0]
				return nil, fmt.Errorf("config does not contain key: %s", k)
			}
			vf, ok := v.(*ConfigField)
			if !ok {
				return nil, u.ErrCast()
			}

			buf, err := config.HumanOutput(vf.Value)
91 92 93
			if err != nil {
				return nil, err
			}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
94
			buf = append(buf, byte('\n'))
95
			return bytes.NewReader(buf), nil
96 97
		},
	},
98
	Type: ConfigField{},
99 100 101 102 103 104 105
	Subcommands: map[string]*cmds.Command{
		"show": configShowCmd,
		"edit": configEditCmd,
	},
}

var configShowCmd = &cmds.Command{
106 107 108 109
	Helptext: cmds.HelpText{
		Tagline: "Outputs the content of the config file",
		ShortDescription: `
WARNING: Your private key is stored in the config file, and it will be
110 111
included in the output of this command.
`,
112
	},
113

114
	Run: func(req cmds.Request) (interface{}, error) {
115 116
		filename, err := config.Filename(req.Context().ConfigRoot)
		if err != nil {
117
			return nil, err
118 119
		}

120
		return showConfig(filename)
121 122 123 124
	},
}

var configEditCmd = &cmds.Command{
125 126 127 128
	Helptext: cmds.HelpText{
		Tagline: "Opens the config file for editing in $EDITOR",
		ShortDescription: `
To use 'ipfs config edit', you must have the $EDITOR environment
129 130
variable set to your preferred text editor.
`,
131
	},
132

133
	Run: func(req cmds.Request) (interface{}, error) {
134 135
		filename, err := config.Filename(req.Context().ConfigRoot)
		if err != nil {
136
			return nil, err
137 138
		}

139
		return nil, editConfig(filename)
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	},
}

func getConfig(filename string, key string) (*ConfigField, error) {
	value, err := config.ReadConfigKey(filename, key)
	if err != nil {
		return nil, fmt.Errorf("Failed to get config value: %s", err)
	}

	return &ConfigField{
		Key:   key,
		Value: value,
	}, nil
}

func setConfig(filename string, key, value string) (*ConfigField, error) {
	err := config.WriteConfigKey(filename, key, value)
	if err != nil {
		return nil, fmt.Errorf("Failed to set config value: %s", err)
	}

	return getConfig(filename, key)
}

func showConfig(filename string) (io.Reader, error) {
Brian Tiger Chow's avatar
todo  
Brian Tiger Chow committed
165
	// TODO maybe we should omit privkey so we don't accidentally leak it?
166

167
	data, err := ioutil.ReadFile(filename)
168 169 170 171
	if err != nil {
		return nil, err
	}

172
	return bytes.NewReader(data), nil
173 174 175 176 177 178 179 180 181 182 183 184
}

func editConfig(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()
}