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
80323eba
Commit
80323eba
authored
Jul 31, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
config: redo to use other serialization functions
parent
bf7cb198
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
94 additions
and
84 deletions
+94
-84
cmd/ipfs/config.go
cmd/ipfs/config.go
+15
-5
cmd/ipfs/ipfs.go
cmd/ipfs/ipfs.go
+1
-1
config/config.go
config/config.go
+8
-2
config/config_test.go
config/config_test.go
+1
-1
config/serialize.go
config/serialize.go
+69
-75
No files found.
cmd/ipfs/config.go
View file @
80323eba
...
...
@@ -2,6 +2,7 @@ package main
import
(
"errors"
"fmt"
"github.com/gonuts/flag"
"github.com/jbenet/commander"
config
"github.com/jbenet/go-ipfs/config"
...
...
@@ -67,19 +68,28 @@ func configCmd(c *commander.Command, inp []string) error {
// Getter (1 param)
if
len
(
inp
)
==
1
{
value
,
err
:=
config
.
GetValueInConfigFile
(
inp
[
0
])
value
,
err
:=
config
.
ReadConfigKey
(
filename
,
inp
[
0
])
if
err
!=
nil
{
return
errors
.
New
(
"Failed to get config value:
"
+
err
.
Error
()
)
return
fmt
.
Errorf
(
"Failed to get config value:
%s"
,
err
)
}
u
.
POut
(
value
+
"
\n
"
)
strval
,
ok
:=
value
.
(
string
)
if
ok
{
u
.
POut
(
"%s
\n
"
,
strval
)
return
nil
}
if
err
:=
config
.
Encode
(
os
.
Stdout
,
value
);
err
!=
nil
{
return
fmt
.
Errorf
(
"Failed to encode config value: %s"
,
err
)
}
u
.
POut
(
"
\n
"
)
return
nil
}
// Setter (>1 params)
err
=
config
.
SetValueInConfigFile
(
inp
[
0
],
inp
[
1
:
])
err
=
config
.
WriteConfigKey
(
filename
,
inp
[
0
],
inp
[
1
])
if
err
!=
nil
{
return
errors
.
New
(
"Failed to set config value:
"
+
err
.
Error
()
)
return
fmt
.
Errorf
(
"Failed to set config value:
%s"
,
err
)
}
return
nil
...
...
cmd/ipfs/ipfs.go
View file @
80323eba
...
...
@@ -67,7 +67,7 @@ func main() {
func
localNode
()
(
*
core
.
IpfsNode
,
error
)
{
//todo implement config file flag
cfg
,
err
:=
config
.
Load
Config
(
""
)
cfg
,
err
:=
config
.
Config
Load
(
""
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
config/config.go
View file @
80323eba
...
...
@@ -32,6 +32,7 @@ var defaultConfigFile = `{
}
`
// ConfigFilename returns the proper tilde expanded config filename.
func
ConfigFilename
(
filename
string
)
(
string
,
error
)
{
if
len
(
filename
)
==
0
{
filename
=
defaultConfigFilePath
...
...
@@ -41,8 +42,8 @@ func ConfigFilename(filename string) (string, error) {
return
u
.
TildeExpansion
(
filename
)
}
//
Load
Config reads given file and returns the read config, or error.
func
Load
Config
(
filename
string
)
(
*
Config
,
error
)
{
// Config
Load
reads given file and returns the read config, or error.
func
Config
Load
(
filename
string
)
(
*
Config
,
error
)
{
filename
,
err
:=
ConfigFilename
(
filename
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -67,3 +68,8 @@ func LoadConfig(filename string) (*Config, error) {
return
&
cfg
,
err
}
// Set sets the value of a particular config key
func
Set
(
filename
,
key
,
value
string
)
error
{
return
nil
}
config/config_test.go
View file @
80323eba
...
...
@@ -7,7 +7,7 @@ import (
func
TestConfig
(
t
*
testing
.
T
)
{
cfg
,
err
:=
Load
Config
(
""
)
cfg
,
err
:=
Config
Load
(
""
)
if
err
!=
nil
{
t
.
Error
(
err
)
return
...
...
config/serialize.go
View file @
80323eba
...
...
@@ -2,120 +2,114 @@ package config
import
(
"encoding/json"
"errors"
"fmt"
u
"github.com/jbenet/go-ipfs/util"
"io/ioutil"
"io"
"os"
"path"
"strings"
)
//
WriteFile writes the given buffer `buf` into
file
name
d `filename
`.
func
Write
File
(
filename
string
,
buf
[]
byte
)
error
{
err
:=
os
.
MkdirAll
(
path
.
Dir
(
filename
)
,
0777
)
//
ReadConfigFile reads the config from `
filename
` into `cfg
`.
func
ReadConfig
File
(
filename
string
,
cfg
interface
{}
)
error
{
f
,
err
:=
os
.
Open
(
filename
)
if
err
!=
nil
{
return
err
}
defer
f
.
Close
()
return
ioutil
.
WriteFile
(
filename
,
buf
,
0666
)
return
Decode
(
f
,
cfg
)
}
//
Read
ConfigFile
read
s the config from `
filename` into `cfg
`.
func
Read
ConfigFile
(
filename
string
,
cfg
*
Config
)
error
{
bu
f
,
err
:=
ioutil
.
ReadFil
e
(
filename
)
//
Write
ConfigFile
write
s the config from `
cfg` into `filename
`.
func
Write
ConfigFile
(
filename
string
,
cfg
interface
{}
)
error
{
f
,
err
:=
os
.
Creat
e
(
filename
)
if
err
!=
nil
{
return
err
}
defer
f
.
Close
()
return
json
.
Unmarshal
(
bu
f
,
cfg
)
return
Encode
(
f
,
cfg
)
}
// Write
Config
File writes the
config from `cfg` into `
filename
`.
func
Write
Config
File
(
filename
string
,
cfg
*
Config
)
error
{
bu
f
,
err
:=
json
.
MarshalIndent
(
cfg
,
""
,
" "
)
// WriteFile writes the
buffer at
filename
func
WriteFile
(
filename
string
,
buf
[]
byte
)
error
{
f
,
err
:=
os
.
Create
(
filename
)
if
err
!=
nil
{
return
err
}
defer
f
.
Close
()
return
WriteFile
(
filename
,
buf
)
_
,
err
=
f
.
Write
(
buf
)
return
err
}
// WriteConfigFile writes the config from `cfg` into `filename`.
func
GetValueInConfigFile
(
key
string
)
(
value
string
,
err
error
)
{
// reading config file
attrs
:=
strings
.
Split
(
key
,
"."
)
filename
,
_
:=
u
.
TildeExpansion
(
defaultConfigFilePath
)
buf
,
err
:=
ioutil
.
ReadFile
(
filename
)
// Encode configuration with JSON
func
Encode
(
w
io
.
Writer
,
value
interface
{})
error
{
// need to prettyprint, hence MarshalIndent, instead of Encoder
buf
,
err
:=
json
.
MarshalIndent
(
value
,
""
,
" "
)
if
err
!=
nil
{
return
""
,
err
return
err
}
// deserializing json
var
cfg
interface
{}
var
exists
bool
_
,
err
=
w
.
Write
(
buf
)
return
err
}
err
=
json
.
Unmarshal
(
buf
,
&
cfg
)
if
err
!=
nil
{
return
""
,
err
// Decode configuration with JSON
func
Decode
(
r
io
.
Reader
,
value
interface
{})
error
{
return
json
.
NewDecoder
(
r
)
.
Decode
(
value
)
}
// ReadConfigKey retrieves only the value of a particular key
func
ReadConfigKey
(
filename
,
key
string
)
(
interface
{},
error
)
{
var
cfg
interface
{}
if
err
:=
ReadConfigFile
(
filename
,
&
cfg
);
err
!=
nil
{
return
nil
,
err
}
for
i
:=
range
attrs
{
cfgMap
,
isMap
:=
cfg
.
(
map
[
string
]
interface
{})
if
!
isMap
{
return
""
,
errors
.
New
(
fmt
.
Sprintf
(
"%s has no attributes"
,
strings
.
Join
(
attrs
[
:
i
],
"."
)))
}
cfg
,
exists
=
cfgMap
[
attrs
[
i
]]
if
!
exists
{
return
""
,
errors
.
New
(
fmt
.
Sprintf
(
"Configuration option key
\"
%s
\"
not recognized"
,
strings
.
Join
(
attrs
[
:
i
+
1
],
"."
)))
}
val
,
is_string
:=
cfg
.
(
string
)
if
is_string
{
return
val
,
nil
var
ok
bool
cursor
:=
cfg
parts
:=
strings
.
Split
(
key
,
"."
)
for
i
,
part
:=
range
parts
{
cursor
,
ok
=
cursor
.
(
map
[
string
]
interface
{})[
part
]
if
!
ok
{
sofar
:=
strings
.
Join
(
parts
[
:
i
],
"."
)
return
nil
,
fmt
.
Errorf
(
"%s key has no attributes"
,
sofar
)
}
}
return
""
,
errors
.
New
(
fmt
.
Sprintf
(
"%s is not a string"
,
key
))
return
cursor
,
nil
}
// WriteConfigFile writes the config from `cfg` into `filename`.
func
SetValueInConfigFile
(
key
string
,
values
[]
string
)
error
{
assignee
:=
strings
.
Join
(
values
,
" "
)
attrs
:=
strings
.
Split
(
key
,
"."
)
filename
,
_
:=
u
.
TildeExpansion
(
defaultConfigFilePath
)
buf
,
err
:=
ioutil
.
ReadFile
(
filename
)
if
err
!=
nil
{
// WriteConfigKey writes the value of a particular key
func
WriteConfigKey
(
filename
,
key
string
,
value
interface
{})
error
{
var
cfg
interface
{}
if
err
:=
ReadConfigFile
(
filename
,
&
cfg
);
err
!=
nil
{
return
err
}
// deserializing json
var
cfg
,
orig
interface
{}
var
exists
,
isMap
bool
cfgMap
:=
make
(
map
[
string
]
interface
{})
var
ok
bool
var
mcursor
map
[
string
]
interface
{}
cursor
:=
cfg
err
=
json
.
Unmarshal
(
buf
,
&
orig
)
cfg
=
orig
if
err
!=
nil
{
return
err
}
parts
:=
strings
.
Split
(
key
,
"."
)
for
i
,
part
:=
range
parts
{
mcursor
,
ok
=
cursor
.
(
map
[
string
]
interface
{})
if
!
ok
{
sofar
:=
strings
.
Join
(
parts
[
:
i
],
"."
)
return
fmt
.
Errorf
(
"%s key is not a map"
,
sofar
)
}
for
i
:=
0
;
i
<
len
(
attrs
);
i
++
{
cfgMap
,
isMap
=
cfg
.
(
map
[
string
]
interface
{})
// curs = append(curs, cfgMap)
if
!
isMap
{
return
errors
.
New
(
fmt
.
Sprintf
(
"%s has no attributes"
,
strings
.
Join
(
attrs
[
:
i
],
"."
)))
// last part? set here
if
i
==
(
len
(
parts
)
-
1
)
{
mcursor
[
part
]
=
value
break
}
cfg
,
exists
=
cfgMap
[
attrs
[
i
]]
if
!
exists
{
return
errors
.
New
(
fmt
.
Sprintf
(
"Configuration option key
\"
%s
\"
not recognized"
,
strings
.
Join
(
attrs
[
:
i
+
1
],
"."
)))
cursor
,
ok
=
mcursor
[
part
]
if
!
ok
{
// create map if this is empty
mcursor
[
part
]
=
map
[
string
]
interface
{}{}
cursor
=
mcursor
[
part
]
}
}
cfgMap
[
attrs
[
len
(
attrs
)
-
1
]]
=
assignee
buf
,
err
=
json
.
MarshalIndent
(
orig
,
""
,
" "
)
if
err
!=
nil
{
return
err
}
WriteFile
(
filename
,
buf
)
return
nil
return
WriteConfigFile
(
filename
,
cfg
)
}
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