Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
bf7cb198
Commit
bf7cb198
authored
Jul 31, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
config: cleaned up ipfs config cmd
parent
20bce474
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
43 deletions
+85
-43
cmd/ipfs/config.go
cmd/ipfs/config.go
+74
-36
config/config.go
config/config.go
+9
-5
config/serialize.go
config/serialize.go
+2
-2
No files found.
cmd/ipfs/config.go
View file @
bf7cb198
package
main
import
(
"errors"
"github.com/gonuts/flag"
"github.com/jbenet/commander"
config
"github.com/jbenet/go-ipfs/config"
u
"github.com/jbenet/go-ipfs/util"
"io"
"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
,
},
Short
:
"Get/Set ipfs config values"
,
Long
:
`ipfs config [<key>] [<value>] - Get/Set ipfs config values.
ipfs config <key> - Get value of <key>
ipfs config <key> <value> - Set value of <key> to <value>
ipfs config --show - Show config file
ipfs config --edit - Edit config file in $EDITOR
Examples:
Get the value of the 'datastore.path' key:
ipfs config datastore.path
Set the value of the 'datastore.path' key:
ipfs config datastore.path ~/.go-ipfs/datastore
`
,
Run
:
configCmd
,
Flag
:
*
flag
.
NewFlagSet
(
"ipfs-config"
,
flag
.
ExitOnError
),
}
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
init
()
{
cmdIpfsConfig
.
Flag
.
Bool
(
"edit"
,
false
,
"Edit config file in $EDITOR"
)
cmdIpfsConfig
.
Flag
.
Bool
(
"show"
,
false
,
"Show config file"
)
}
func
configCmd
(
c
*
commander
.
Command
,
inp
[]
string
)
error
{
// todo: implement --config filename flag.
filename
,
err
:=
config
.
ConfigFilename
(
""
)
if
err
!=
nil
{
return
err
}
// if editing, open the editor
if
c
.
Flag
.
Lookup
(
"edit"
)
.
Value
.
Get
()
.
(
bool
)
{
return
configEditor
(
filename
)
}
// if showing, cat the file
if
c
.
Flag
.
Lookup
(
"show"
)
.
Value
.
Get
()
.
(
bool
)
{
return
configCat
(
filename
)
}
if
len
(
inp
)
==
0
{
// "ipfs config" run without parameters
u
.
POut
(
c
.
Long
+
"
\n
"
)
u
.
POut
(
c
.
Long
)
return
nil
}
// Getter (1 param)
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
errors
.
New
(
"Failed to get config value: "
+
err
.
Error
())
}
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
:
])
//
Setter (>1 params)
err
=
config
.
SetValueInConfigFile
(
inp
[
0
],
inp
[
1
:
])
if
err
!=
nil
{
u
.
POut
(
"Failed to set config value: "
+
err
.
Error
()
+
"
\n
"
)
return
errors
.
New
(
"Failed to set config value: "
+
err
.
Error
())
}
return
nil
}
func
config
EditCmd
(
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
()
func
config
Cat
(
filename
string
)
error
{
file
,
err
:=
os
.
Open
(
filename
)
if
err
!=
nil
{
return
err
}
return
nil
defer
file
.
Close
()
_
,
err
=
io
.
Copy
(
os
.
Stdout
,
file
)
return
err
}
func
configEditor
(
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
()
}
config/config.go
View file @
bf7cb198
...
...
@@ -22,7 +22,7 @@ type Config struct {
Datastore
*
Datastore
}
var
D
efaultConfigFilePath
=
"~/.go-ipfs/config"
var
d
efaultConfigFilePath
=
"~/.go-ipfs/config"
var
defaultConfigFile
=
`{
"identity": {},
"datastore": {
...
...
@@ -32,14 +32,18 @@ var defaultConfigFile = `{
}
`
// LoadConfig reads given file and returns the read config, or error.
func
LoadConfig
(
filename
string
)
(
*
Config
,
error
)
{
func
ConfigFilename
(
filename
string
)
(
string
,
error
)
{
if
len
(
filename
)
==
0
{
filename
=
D
efaultConfigFilePath
filename
=
d
efaultConfigFilePath
}
// tilde expansion on config file
filename
,
err
:=
u
.
TildeExpansion
(
filename
)
return
u
.
TildeExpansion
(
filename
)
}
// LoadConfig reads given file and returns the read config, or error.
func
LoadConfig
(
filename
string
)
(
*
Config
,
error
)
{
filename
,
err
:=
ConfigFilename
(
filename
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
config/serialize.go
View file @
bf7cb198
...
...
@@ -46,7 +46,7 @@ func GetValueInConfigFile(key string) (value string, err error) {
// reading config file
attrs
:=
strings
.
Split
(
key
,
"."
)
filename
,
_
:=
u
.
TildeExpansion
(
D
efaultConfigFilePath
)
filename
,
_
:=
u
.
TildeExpansion
(
d
efaultConfigFilePath
)
buf
,
err
:=
ioutil
.
ReadFile
(
filename
)
if
err
!=
nil
{
return
""
,
err
...
...
@@ -83,7 +83,7 @@ func SetValueInConfigFile(key string, values []string) error {
assignee
:=
strings
.
Join
(
values
,
" "
)
attrs
:=
strings
.
Split
(
key
,
"."
)
filename
,
_
:=
u
.
TildeExpansion
(
D
efaultConfigFilePath
)
filename
,
_
:=
u
.
TildeExpansion
(
d
efaultConfigFilePath
)
buf
,
err
:=
ioutil
.
ReadFile
(
filename
)
if
err
!=
nil
{
return
err
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment