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

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

14 15 16 17
	cmds "github.com/ipfs/go-ipfs/commands"
	repo "github.com/ipfs/go-ipfs/repo"
	config "github.com/ipfs/go-ipfs/repo/config"
	fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
18
	u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
19 20 21 22 23 24 25
)

type ConfigField struct {
	Key   string
	Value interface{}
}

26
var ConfigCmd = &cmds.Command{
27
	Helptext: cmds.HelpText{
28
		Tagline: "Get and set IPFS config values.",
29
		ShortDescription: `
Richard Littauer's avatar
Richard Littauer committed
30
'ipfs config' controls configuration variables. It works like 'git config'.
31 32 33
The configuration values are stored in a config file inside your IPFS
repository.`,
		LongDescription: `
Richard Littauer's avatar
Richard Littauer committed
34
'ipfs config' controls configuration variables. It works
35 36 37
much like 'git config'. The configuration values are stored in a config
file inside your IPFS repository.

38
Examples:
39

40
Get the value of the 'datastore.path' key:
41

42
  $ ipfs config datastore.path
43

44
Set the value of the 'datastore.path' key:
45

46
  $ ipfs config datastore.path ~/.ipfs/datastore
47
`,
48
	},
49 50

	Arguments: []cmds.Argument{
51 52
		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."),
53
	},
54
	Options: []cmds.Option{
55 56
		cmds.BoolOption("bool", "Set a boolean value.").Default(false),
		cmds.BoolOption("json", "Parse stringified JSON.").Default(false),
57
	},
58
	Run: func(req cmds.Request, res cmds.Response) {
59
		args := req.Arguments()
60
		key := args[0]
61

62
		// This is a temporary fix until we move the private key out of the config file
63 64
		switch strings.ToLower(key) {
		case "identity", "identity.privkey":
65 66 67 68 69
			res.SetError(fmt.Errorf("cannot show or change private key through API"), cmds.ErrNormal)
			return
		default:
		}

Jeromy's avatar
Jeromy committed
70
		r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
71
		if err != nil {
72 73
			res.SetError(err, cmds.ErrNormal)
			return
74
		}
75
		defer r.Close()
76

77
		var output *ConfigField
78
		if len(args) == 2 {
79
			value := args[1]
80 81 82 83 84 85 86 87 88 89 90

			if parseJson, _, _ := req.Option("json").Bool(); parseJson {
				var jsonVal interface{}
				if err := json.Unmarshal([]byte(value), &jsonVal); err != nil {
					err = fmt.Errorf("failed to unmarshal json. %s", err)
					res.SetError(err, cmds.ErrNormal)
					return
				}

				output, err = setConfig(r, key, jsonVal)
			} else if isbool, _, _ := req.Option("bool").Bool(); isbool {
91 92 93 94
				output, err = setConfig(r, key, value == "true")
			} else {
				output, err = setConfig(r, key, value)
			}
95
		} else {
96 97 98 99 100
			output, err = getConfig(r, key)
		}
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
101
		}
102
		res.SetOutput(output)
103
	},
104
	Marshalers: cmds.MarshalerMap{
105
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
106
			if len(res.Request().Arguments()) == 2 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
107
				return nil, nil // dont output anything
108 109
			}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110 111 112 113 114 115 116 117 118 119 120
			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)
121 122 123
			if err != nil {
				return nil, err
			}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
124
			buf = append(buf, byte('\n'))
125
			return bytes.NewReader(buf), nil
126 127
		},
	},
128
	Type: ConfigField{},
129
	Subcommands: map[string]*cmds.Command{
130 131 132
		"show":    configShowCmd,
		"edit":    configEditCmd,
		"replace": configReplaceCmd,
133 134 135 136
	},
}

var configShowCmd = &cmds.Command{
137
	Helptext: cmds.HelpText{
rht's avatar
rht committed
138
		Tagline: "Outputs the content of the config file.",
139 140
		ShortDescription: `
WARNING: Your private key is stored in the config file, and it will be
141 142
included in the output of this command.
`,
143
	},
144

145
	Run: func(req cmds.Request, res cmds.Response) {
146
		fname, err := config.Filename(req.InvocContext().ConfigRoot)
147
		if err != nil {
148 149
			res.SetError(err, cmds.ErrNormal)
			return
150 151
		}

152
		data, err := ioutil.ReadFile(fname)
153 154 155 156
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
157 158 159 160 161 162 163 164

		var cfg map[string]interface{}
		err = json.Unmarshal(data, &cfg)
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

Jeromy's avatar
Jeromy committed
165 166 167 168 169 170 171
		idmap, ok := cfg["Identity"].(map[string]interface{})
		if !ok {
			res.SetError(fmt.Errorf("config has no identity"), cmds.ErrNormal)
			return
		}

		delete(idmap, "PrivKey")
172 173 174 175 176 177 178 179

		output, err := config.HumanOutput(cfg)
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		res.SetOutput(bytes.NewReader(output))
180 181 182 183
	},
}

var configEditCmd = &cmds.Command{
184
	Helptext: cmds.HelpText{
rht's avatar
rht committed
185
		Tagline: "Opens the config file for editing in $EDITOR.",
186 187
		ShortDescription: `
To use 'ipfs config edit', you must have the $EDITOR environment
188 189
variable set to your preferred text editor.
`,
190
	},
191

192
	Run: func(req cmds.Request, res cmds.Response) {
Jeromy's avatar
Jeromy committed
193
		filename, err := config.Filename(req.InvocContext().ConfigRoot)
194
		if err != nil {
195 196
			res.SetError(err, cmds.ErrNormal)
			return
197 198
		}

199 200 201 202
		err = editConfig(filename)
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
		}
203 204 205
	},
}

206 207
var configReplaceCmd = &cmds.Command{
	Helptext: cmds.HelpText{
rht's avatar
rht committed
208
		Tagline: "Replaces the config with <file>.",
209
		ShortDescription: `
210
Make sure to back up the config file first if neccessary, as this operation
211 212 213 214 215
can't be undone.
`,
	},

	Arguments: []cmds.Argument{
216
		cmds.FileArg("file", true, false, "The file to use as the new config."),
217
	},
218
	Run: func(req cmds.Request, res cmds.Response) {
Jeromy's avatar
Jeromy committed
219
		r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
220
		if err != nil {
221 222
			res.SetError(err, cmds.ErrNormal)
			return
223 224 225 226 227
		}
		defer r.Close()

		file, err := req.Files().NextFile()
		if err != nil {
228 229
			res.SetError(err, cmds.ErrNormal)
			return
230 231 232
		}
		defer file.Close()

233 234 235 236 237
		err = replaceConfig(r, file)
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
238 239 240
	},
}

241
func getConfig(r repo.Repo, key string) (*ConfigField, error) {
242
	value, err := r.GetConfigKey(key)
243
	if err != nil {
Richard Littauer's avatar
Richard Littauer committed
244
		return nil, fmt.Errorf("Failed to get config value: %q", err)
245 246 247 248 249 250 251
	}
	return &ConfigField{
		Key:   key,
		Value: value,
	}, nil
}

252
func setConfig(r repo.Repo, key string, value interface{}) (*ConfigField, error) {
253 254
	keyF, err := getConfig(r, "Identity.PrivKey")
	if err != nil {
255
		return nil, errors.New("failed to get PrivKey")
256 257 258
	}
	privkey := keyF.Value
	err = r.SetConfigKey(key, value)
259
	if err != nil {
260
		return nil, fmt.Errorf("failed to set config value: %s (maybe use --json?)", err)
261
	}
262
	err = r.SetConfigKey("Identity.PrivKey", privkey)
263
	if err != nil {
264
		return nil, errors.New("failed to set PrivKey")
265
	}
266
	return getConfig(r, key)
267 268 269 270 271 272 273 274 275 276 277 278
}

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()
}
279 280 281 282

func replaceConfig(r repo.Repo, file io.Reader) error {
	var cfg config.Config
	if err := json.NewDecoder(file).Decode(&cfg); err != nil {
283 284 285 286
		return errors.New("failed to decode file as config")
	}
	if len(cfg.Identity.PrivKey) != 0 {
		return errors.New("setting private key with API is not supported")
287 288
	}

289 290 291 292 293 294
	keyF, err := getConfig(r, "Identity.PrivKey")
	if err != nil {
		return fmt.Errorf("Failed to get PrivKey")
	}
	cfg.Identity.PrivKey = keyF.Value.(string)

295 296
	return r.SetConfig(&cfg)
}