config.go 1.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
package main

import (
	"github.com/jbenet/commander"
	config "github.com/jbenet/go-ipfs/config"
	u "github.com/jbenet/go-ipfs/util"
	"os"
	"os/exec"
)

var cmdIpfsConfig = &commander.Command{
	UsageLine: "config",
	Short:     "See and Edit ipfs options",
	Long: `ipfs config - See or Edit ipfs configuration.

    See specific config's values with:
	ipfs config datastore.path
    Assign a new value with:
	ipfs config datastore.path ~/.go-ipfs/datastore

    Open the config file in your editor(from $EDITOR):
	ipfs config edit
  `,
	Run: configCmd,
	Subcommands: []*commander.Command{
		cmdIpfsConfigEdit,
	},
}

var cmdIpfsConfigEdit = &commander.Command{
	UsageLine: "edit",
	Short:     "Opens the configuration file in the editor.",
	Long: `Looks up environment variable $EDITOR and
	attempts to open the config file with it.
  `,
	Run: configEditCmd,
}

func configCmd(c *commander.Command, inp []string) error {
	if len(inp) == 0 {
		// "ipfs config" run without parameters
		u.POut(c.Long + "\n")
		return nil
	}

	if len(inp) == 1 {
		// "ipfs config" run without one parameter, so this is a value getter
		value, err := config.GetValueInConfigFile(inp[0])
		if err != nil {
			u.POut("Failed to get config value: " + err.Error() + "\n")
		} else {
			u.POut(value + "\n")
		}
		return nil
	}

	// "ipfs config" run without two parameter, so this is a value setter
	err := config.SetValueInConfigFile(inp[0], inp[1:])
	if err != nil {
		u.POut("Failed to set config value: " + err.Error() + "\n")
	}
	return nil
}

func configEditCmd(c *commander.Command, _ []string) error {
	if editor := os.Getenv("EDITOR"); editor == "" {
		u.POut("ENVIRON variable $EDITOR is not assigned \n")
	} else {
		exec.Command("sh", "-c", editor+" "+config.DefaultConfigFilePath).Start()
	}
	return nil
}