@cryptotaxi247 / kubo / commits / bf7cb198e

config: cleaned up ipfs config cmd

Juan Batiz-Benet committed Jul 31, 2014 at 01:08 UTC bf7cb198ea9e2dfdb9ce5b8dc9fc907c878225f7
3 files changed +85 -43
cmd/ipfs/config.go
+74 -36
@@ -1,72 +1,110 @@
1 package main
2
3 import (
4 + "errors"
5 + "github.com/gonuts/flag"
6 "github.com/jbenet/commander"
7 config "github.com/jbenet/go-ipfs/config"
8 u "github.com/jbenet/go-ipfs/util"
9 + "io"
10 "os"
11 "os/exec"
12 )
13
14 var cmdIpfsConfig = &commander.Command{
15 UsageLine: "config",
13 - Short: "See and Edit ipfs options",
14 - Long: `ipfs config - See or Edit ipfs configuration.
15 -
16 - See specific config's values with:
17 - ipfs config datastore.path
18 - Assign a new value with:
19 - ipfs config datastore.path ~/.go-ipfs/datastore
20 -
21 - Open the config file in your editor(from $EDITOR):
22 - ipfs config edit
23 - `,
24 - Run: configCmd,
25 - Subcommands: []*commander.Command{
26 - cmdIpfsConfigEdit,
27 - },
16 + Short: "Get/Set ipfs config values",
17 + Long: `ipfs config [<key>] [<value>] - Get/Set ipfs config values.
18 +
19 + ipfs config <key> - Get value of <key>
20 + ipfs config <key> <value> - Set value of <key> to <value>
21 + ipfs config --show - Show config file
22 + ipfs config --edit - Edit config file in $EDITOR
23 +
24 +Examples:
25 +
26 + Get the value of the 'datastore.path' key:
27 +
28 + ipfs config datastore.path
29 +
30 + Set the value of the 'datastore.path' key:
31 +
32 + ipfs config datastore.path ~/.go-ipfs/datastore
33 +
34 +`,
35 + Run: configCmd,
36 + Flag: *flag.NewFlagSet("ipfs-config", flag.ExitOnError),
37 }
38
30 -var cmdIpfsConfigEdit = &commander.Command{
31 - UsageLine: "edit",
32 - Short: "Opens the configuration file in the editor.",
33 - Long: `Looks up environment variable $EDITOR and
34 - attempts to open the config file with it.
35 - `,
36 - Run: configEditCmd,
39 +func init() {
40 + cmdIpfsConfig.Flag.Bool("edit", false, "Edit config file in $EDITOR")
41 + cmdIpfsConfig.Flag.Bool("show", false, "Show config file")
42 }
43
44 func configCmd(c *commander.Command, inp []string) error {
45 +
46 + // todo: implement --config filename flag.
47 + filename, err := config.ConfigFilename("")
48 + if err != nil {
49 + return err
50 + }
51 +
52 + // if editing, open the editor
53 + if c.Flag.Lookup("edit").Value.Get().(bool) {
54 + return configEditor(filename)
55 + }
56 +
57 + // if showing, cat the file
58 + if c.Flag.Lookup("show").Value.Get().(bool) {
59 + return configCat(filename)
60 + }
61 +
62 if len(inp) == 0 {
63 // "ipfs config" run without parameters
42 - u.POut(c.Long + "\n")
64 + u.POut(c.Long)
65 return nil
66 }
67
68 + // Getter (1 param)
69 if len(inp) == 1 {
47 - // "ipfs config" run without one parameter, so this is a value getter
70 value, err := config.GetValueInConfigFile(inp[0])
71 if err != nil {
50 - u.POut("Failed to get config value: " + err.Error() + "\n")
51 - } else {
52 - u.POut(value + "\n")
72 + return errors.New("Failed to get config value: " + err.Error())
73 }
74 +
75 + u.POut(value + "\n")
76 return nil
77 }
78
57 - // "ipfs config" run without two parameter, so this is a value setter
58 - err := config.SetValueInConfigFile(inp[0], inp[1:])
79 + // Setter (>1 params)
80 + err = config.SetValueInConfigFile(inp[0], inp[1:])
81 if err != nil {
60 - u.POut("Failed to set config value: " + err.Error() + "\n")
82 + return errors.New("Failed to set config value: " + err.Error())
83 }
84 +
85 return nil
86 }
87
65 -func configEditCmd(c *commander.Command, _ []string) error {
66 - if editor := os.Getenv("EDITOR"); editor == "" {
67 - u.POut("ENVIRON variable $EDITOR is not assigned \n")
68 - } else {
69 - exec.Command("sh", "-c", editor+" "+config.DefaultConfigFilePath).Start()
88 +func configCat(filename string) error {
89 +
90 + file, err := os.Open(filename)
91 + if err != nil {
92 + return err
93 }
71 - return nil
94 + defer file.Close()
95 +
96 + _, err = io.Copy(os.Stdout, file)
97 + return err
98 +}
99 +
100 +func configEditor(filename string) error {
101 +
102 + editor := os.Getenv("EDITOR")
103 + if editor == "" {
104 + return errors.New("ENV variable $EDITOR not set")
105 + }
106 +
107 + cmd := exec.Command("sh", "-c", editor+" "+filename)
108 + cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
109 + return cmd.Run()
110 }
config/config.go
+9 -5
@@ -22,7 +22,7 @@ type Config struct {
22 Datastore *Datastore
23 }
24
25 -var DefaultConfigFilePath = "~/.go-ipfs/config"
25 +var defaultConfigFilePath = "~/.go-ipfs/config"
26 var defaultConfigFile = `{
27 "identity": {},
28 "datastore": {
@@ -32,14 +32,18 @@ var defaultConfigFile = `{
32 }
33 `
34
35 -// LoadConfig reads given file and returns the read config, or error.
36 -func LoadConfig(filename string) (*Config, error) {
35 +func ConfigFilename(filename string) (string, error) {
36 if len(filename) == 0 {
38 - filename = DefaultConfigFilePath
37 + filename = defaultConfigFilePath
38 }
39
40 // tilde expansion on config file
42 - filename, err := u.TildeExpansion(filename)
41 + return u.TildeExpansion(filename)
42 +}
43 +
44 +// LoadConfig reads given file and returns the read config, or error.
45 +func LoadConfig(filename string) (*Config, error) {
46 + filename, err := ConfigFilename(filename)
47 if err != nil {
48 return nil, err
49 }
config/serialize.go
+2 -2
@@ -46,7 +46,7 @@ func GetValueInConfigFile(key string) (value string, err error) {
46 // reading config file
47 attrs := strings.Split(key, ".")
48
49 - filename, _ := u.TildeExpansion(DefaultConfigFilePath)
49 + filename, _ := u.TildeExpansion(defaultConfigFilePath)
50 buf, err := ioutil.ReadFile(filename)
51 if err != nil {
52 return "", err
@@ -83,7 +83,7 @@ func SetValueInConfigFile(key string, values []string) error {
83 assignee := strings.Join(values, " ")
84 attrs := strings.Split(key, ".")
85
86 - filename, _ := u.TildeExpansion(DefaultConfigFilePath)
86 + filename, _ := u.TildeExpansion(defaultConfigFilePath)
87 buf, err := ioutil.ReadFile(filename)
88 if err != nil {
89 return err