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
405afd2a
Commit
405afd2a
authored
10 years ago
by
Brian Tiger Chow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(config, repo): all writes go through FSRepo. next: privatize these
parent
c2877b20
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
81 additions
and
75 deletions
+81
-75
cmd/ipfs/main.go
cmd/ipfs/main.go
+2
-1
cmd/ipfs/tour.go
cmd/ipfs/tour.go
+2
-1
core/commands/bootstrap.go
core/commands/bootstrap.go
+4
-3
core/commands/config.go
core/commands/config.go
+3
-2
repo/config/config.go
repo/config/config.go
+11
-23
repo/config/version.go
repo/config/version.go
+0
-13
repo/config/version_test.go
repo/config/version_test.go
+2
-3
repo/fsrepo/fsrepo.go
repo/fsrepo/fsrepo.go
+1
-1
repo/fsrepo/serialize.go
repo/fsrepo/serialize.go
+50
-25
repo/fsrepo/serialize_test.go
repo/fsrepo/serialize_test.go
+4
-2
updates/updates.go
updates/updates.go
+2
-1
No files found.
cmd/ipfs/main.go
View file @
405afd2a
...
...
@@ -23,6 +23,7 @@ import (
daemon
"github.com/jbenet/go-ipfs/core/daemon"
repo
"github.com/jbenet/go-ipfs/repo"
config
"github.com/jbenet/go-ipfs/repo/config"
fsrepo
"github.com/jbenet/go-ipfs/repo/fsrepo"
updates
"github.com/jbenet/go-ipfs/updates"
u
"github.com/jbenet/go-ipfs/util"
"github.com/jbenet/go-ipfs/util/debugerror"
...
...
@@ -449,7 +450,7 @@ func loadConfig(path string) (*config.Config, error) {
return
nil
,
err
}
return
config
.
Load
(
configFile
)
return
fsrepo
.
Load
(
configFile
)
}
// startProfiling begins CPU profiling and returns a `stop` function to be
...
...
This diff is collapsed.
Click to expand it.
cmd/ipfs/tour.go
View file @
405afd2a
...
...
@@ -10,6 +10,7 @@ import (
cmds
"github.com/jbenet/go-ipfs/commands"
config
"github.com/jbenet/go-ipfs/repo/config"
tour
"github.com/jbenet/go-ipfs/tour"
fsrepo
"github.com/jbenet/go-ipfs/repo/fsrepo"
)
var
tourCmd
=
&
cmds
.
Command
{
...
...
@@ -191,5 +192,5 @@ func writeConfig(path string, cfg *config.Config) error {
if
err
!=
nil
{
return
err
}
return
config
.
WriteConfigFile
(
filename
,
cfg
)
return
fsrepo
.
WriteConfigFile
(
filename
,
cfg
)
}
This diff is collapsed.
Click to expand it.
core/commands/bootstrap.go
View file @
405afd2a
...
...
@@ -6,6 +6,7 @@ import (
cmds
"github.com/jbenet/go-ipfs/commands"
config
"github.com/jbenet/go-ipfs/repo/config"
"github.com/jbenet/go-ipfs/repo/fsrepo"
u
"github.com/jbenet/go-ipfs/util"
errors
"github.com/jbenet/go-ipfs/util/debugerror"
)
...
...
@@ -250,7 +251,7 @@ func bootstrapAdd(filename string, cfg *config.Config, peers []config.BootstrapP
}
}
err
:=
config
.
WriteConfigFile
(
filename
,
cfg
)
err
:=
fsrepo
.
WriteConfigFile
(
filename
,
cfg
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -278,7 +279,7 @@ func bootstrapRemove(filename string, cfg *config.Config, toRemove []config.Boot
}
cfg
.
Bootstrap
=
keep
err
:=
config
.
WriteConfigFile
(
filename
,
cfg
)
err
:=
fsrepo
.
WriteConfigFile
(
filename
,
cfg
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -291,7 +292,7 @@ func bootstrapRemoveAll(filename string, cfg *config.Config) ([]config.Bootstrap
copy
(
removed
,
cfg
.
Bootstrap
)
cfg
.
Bootstrap
=
nil
err
:=
config
.
WriteConfigFile
(
filename
,
cfg
)
err
:=
fsrepo
.
WriteConfigFile
(
filename
,
cfg
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
This diff is collapsed.
Click to expand it.
core/commands/config.go
View file @
405afd2a
...
...
@@ -11,6 +11,7 @@ import (
cmds
"github.com/jbenet/go-ipfs/commands"
config
"github.com/jbenet/go-ipfs/repo/config"
fsrepo
"github.com/jbenet/go-ipfs/repo/fsrepo"
u
"github.com/jbenet/go-ipfs/util"
)
...
...
@@ -141,7 +142,7 @@ variable set to your preferred text editor.
}
func
getConfig
(
filename
string
,
key
string
)
(
*
ConfigField
,
error
)
{
value
,
err
:=
config
.
ReadConfigKey
(
filename
,
key
)
value
,
err
:=
fsrepo
.
ReadConfigKey
(
filename
,
key
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Failed to get config value: %s"
,
err
)
}
...
...
@@ -153,7 +154,7 @@ func getConfig(filename string, key string) (*ConfigField, error) {
}
func
setConfig
(
filename
string
,
key
,
value
string
)
(
*
ConfigField
,
error
)
{
err
:=
config
.
WriteConfigKey
(
filename
,
key
,
value
)
err
:=
fsrepo
.
WriteConfigKey
(
filename
,
key
,
value
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Failed to set config value: %s"
,
err
)
}
...
...
This diff is collapsed.
Click to expand it.
repo/config/config.go
View file @
405afd2a
...
...
@@ -3,6 +3,7 @@ package config
import
(
"encoding/base64"
"encoding/json"
"errors"
"os"
"path/filepath"
...
...
@@ -13,7 +14,6 @@ import (
ic
"github.com/jbenet/go-ipfs/p2p/crypto"
u
"github.com/jbenet/go-ipfs/util"
"github.com/jbenet/go-ipfs/util/debugerror"
)
var
log
=
u
.
Logger
(
"config"
)
...
...
@@ -191,29 +191,17 @@ func (i *Identity) DecodePrivateKey(passphrase string) (ic.PrivKey, error) {
return
ic
.
UnmarshalPrivateKey
(
pkb
)
}
//
Load reads given file and returns the read config, or error.
func
Load
(
filename
string
)
(
*
Config
,
error
)
{
// if nothing is there, fail. User must run 'ipfs init'
if
!
u
.
FileExists
(
filename
)
{
return
nil
,
debugerror
.
New
(
"ipfs not initialized, please run 'ipfs init'"
)
//
HumanOutput gets a config value ready for printing
func
HumanOutput
(
value
interface
{})
([]
byte
,
error
)
{
s
,
ok
:=
value
.
(
string
)
if
ok
{
return
[]
byte
(
strings
.
Trim
(
s
,
"
\n
"
)),
nil
}
var
cfg
Config
err
:=
ReadConfigFile
(
filename
,
&
cfg
)
if
err
!=
nil
{
return
nil
,
err
}
// tilde expansion on datastore path
cfg
.
Datastore
.
Path
,
err
=
u
.
TildeExpansion
(
cfg
.
Datastore
.
Path
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
cfg
,
err
return
Marshal
(
value
)
}
// Set sets the value of a particular config key
func
Set
(
filename
,
key
,
value
string
)
error
{
return
WriteConfigKey
(
filename
,
key
,
value
)
// Marshal configuration with JSON
func
Marshal
(
value
interface
{})
([]
byte
,
error
)
{
// need to prettyprint, hence MarshalIndent, instead of Encoder
return
json
.
MarshalIndent
(
value
,
""
,
" "
)
}
This diff is collapsed.
Click to expand it.
repo/config/version.go
View file @
405afd2a
...
...
@@ -120,19 +120,6 @@ func (v *Version) ShouldCheckForUpdate() bool {
return
true
}
// RecordUpdateCheck is called to record that an update check was performed,
// showing that the running version is the most recent one.
func
RecordUpdateCheck
(
cfg
*
Config
,
filename
string
)
{
cfg
.
Version
.
CheckDate
=
time
.
Now
()
if
cfg
.
Version
.
CheckPeriod
==
""
{
// CheckPeriod was not initialized for some reason (e.g. config file broken)
log
.
Error
(
"config.Version.CheckPeriod not set. config broken?"
)
}
WriteConfigFile
(
filename
,
cfg
)
}
// VersionDefaultValue returns the default version config value (for init).
func
VersionDefaultValue
()
Version
{
return
Version
{
...
...
This diff is collapsed.
Click to expand it.
repo/config/version_test.go
View file @
405afd2a
package
config
import
(
"encoding/json"
"strings"
"testing"
)
...
...
@@ -23,8 +24,7 @@ func TestAutoUpdateValues(t *testing.T) {
}
for
i
,
tc
:=
range
tests
{
err
:=
Decode
(
strings
.
NewReader
(
tc
.
input
),
&
tval
)
if
err
!=
tc
.
err
{
if
err
:=
json
.
NewDecoder
(
strings
.
NewReader
(
tc
.
input
))
.
Decode
(
&
tval
);
err
!=
tc
.
err
{
t
.
Fatalf
(
"%d failed - got err %q wanted %v"
,
i
,
err
,
tc
.
err
)
}
...
...
@@ -32,5 +32,4 @@ func TestAutoUpdateValues(t *testing.T) {
t
.
Fatalf
(
"%d failed - got val %q where we wanted %q"
,
i
,
tval
.
AutoUpdate
,
tc
.
val
)
}
}
}
This diff is collapsed.
Click to expand it.
repo/fsrepo/fsrepo.go
View file @
405afd2a
...
...
@@ -54,7 +54,7 @@ func (r *FSRepo) SetConfig(conf *config.Config) error {
if
err
!=
nil
{
return
err
}
if
err
:=
config
.
WriteConfigFile
(
configFilename
,
conf
);
err
!=
nil
{
if
err
:=
WriteConfigFile
(
configFilename
,
conf
);
err
!=
nil
{
return
err
}
r
.
config
=
*
conf
// copy so caller cannot modify the private config
...
...
This diff is collapsed.
Click to expand it.
repo/
config
/serialize.go
→
repo/
fsrepo
/serialize.go
View file @
405afd2a
package
config
package
fsrepo
import
(
"encoding/json"
...
...
@@ -7,8 +7,15 @@ import (
"os"
"path/filepath"
"strings"
"time"
"github.com/jbenet/go-ipfs/repo/config"
"github.com/jbenet/go-ipfs/util"
"github.com/jbenet/go-ipfs/util/debugerror"
)
var
log
=
util
.
Logger
(
"fsrepo"
)
// ReadConfigFile reads the config from `filename` into `cfg`.
func
ReadConfigFile
(
filename
string
,
cfg
interface
{})
error
{
f
,
err
:=
os
.
Open
(
filename
)
...
...
@@ -16,8 +23,7 @@ func ReadConfigFile(filename string, cfg interface{}) error {
return
err
}
defer
f
.
Close
()
if
err
:=
Decode
(
f
,
cfg
);
err
!=
nil
{
if
err
:=
json
.
NewDecoder
(
f
)
.
Decode
(
cfg
);
err
!=
nil
{
return
fmt
.
Errorf
(
"Failure to decode config: %s"
,
err
)
}
return
nil
...
...
@@ -56,38 +62,17 @@ func WriteFile(filename string, buf []byte) error {
return
err
}
// HumanOutput gets a config value ready for printing
func
HumanOutput
(
value
interface
{})
([]
byte
,
error
)
{
s
,
ok
:=
value
.
(
string
)
if
ok
{
return
[]
byte
(
strings
.
Trim
(
s
,
"
\n
"
)),
nil
}
return
Marshal
(
value
)
}
// Marshal configuration with JSON
func
Marshal
(
value
interface
{})
([]
byte
,
error
)
{
// need to prettyprint, hence MarshalIndent, instead of Encoder
return
json
.
MarshalIndent
(
value
,
""
,
" "
)
}
// Encode configuration with JSON
func
Encode
(
w
io
.
Writer
,
value
interface
{})
error
{
// need to prettyprint, hence MarshalIndent, instead of Encoder
buf
,
err
:=
Marshal
(
value
)
buf
,
err
:=
config
.
Marshal
(
value
)
if
err
!=
nil
{
return
err
}
_
,
err
=
w
.
Write
(
buf
)
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
{}
...
...
@@ -142,3 +127,43 @@ func WriteConfigKey(filename, key string, value interface{}) error {
return
WriteConfigFile
(
filename
,
cfg
)
}
// Load reads given file and returns the read config, or error.
func
Load
(
filename
string
)
(
*
config
.
Config
,
error
)
{
// if nothing is there, fail. User must run 'ipfs init'
if
!
util
.
FileExists
(
filename
)
{
return
nil
,
debugerror
.
New
(
"ipfs not initialized, please run 'ipfs init'"
)
}
var
cfg
config
.
Config
err
:=
ReadConfigFile
(
filename
,
&
cfg
)
if
err
!=
nil
{
return
nil
,
err
}
// tilde expansion on datastore path
cfg
.
Datastore
.
Path
,
err
=
util
.
TildeExpansion
(
cfg
.
Datastore
.
Path
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
cfg
,
err
}
// Set sets the value of a particular config key
func
Set
(
filename
,
key
,
value
string
)
error
{
return
WriteConfigKey
(
filename
,
key
,
value
)
}
// RecordUpdateCheck is called to record that an update check was performed,
// showing that the running version is the most recent one.
func
RecordUpdateCheck
(
cfg
*
config
.
Config
,
filename
string
)
{
cfg
.
Version
.
CheckDate
=
time
.
Now
()
if
cfg
.
Version
.
CheckPeriod
==
""
{
// CheckPeriod was not initialized for some reason (e.g. config file broken)
log
.
Error
(
"config.Version.CheckPeriod not set. config broken?"
)
}
WriteConfigFile
(
filename
,
cfg
)
}
This diff is collapsed.
Click to expand it.
repo/
config/config
_test.go
→
repo/
fsrepo/serialize
_test.go
View file @
405afd2a
package
config
package
fsrepo
import
(
"testing"
config
"github.com/jbenet/go-ipfs/repo/config"
)
func
TestConfig
(
t
*
testing
.
T
)
{
const
filename
=
".ipfsconfig"
const
dsPath
=
"/path/to/datastore"
cfgWritten
:=
new
(
Config
)
cfgWritten
:=
new
(
config
.
Config
)
cfgWritten
.
Datastore
.
Path
=
dsPath
err
:=
WriteConfigFile
(
filename
,
cfgWritten
)
if
err
!=
nil
{
...
...
This diff is collapsed.
Click to expand it.
updates/updates.go
View file @
405afd2a
...
...
@@ -6,6 +6,7 @@ import (
"time"
config
"github.com/jbenet/go-ipfs/repo/config"
fsrepo
"github.com/jbenet/go-ipfs/repo/fsrepo"
u
"github.com/jbenet/go-ipfs/util"
semver
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
...
...
@@ -209,7 +210,7 @@ func CliCheckForUpdates(cfg *config.Config, confFile string) error {
// if there is no update available, record it, and exit.
if
err
==
ErrNoUpdateAvailable
{
log
.
Noticef
(
"No update available, checked on %s"
,
time
.
Now
())
config
.
RecordUpdateCheck
(
cfg
,
confFile
)
// only record if we checked successfully.
fsrepo
.
RecordUpdateCheck
(
cfg
,
confFile
)
// only record if we checked successfully.
return
nil
}
...
...
This diff is collapsed.
Click to expand it.
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