diff --git a/cmd/ipfs/main.go b/cmd/ipfs/main.go index a19b60c80..d06f03dff 100644 --- a/cmd/ipfs/main.go +++ b/cmd/ipfs/main.go @@ -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 diff --git a/cmd/ipfs/tour.go b/cmd/ipfs/tour.go index aed7ea918..18c8fce4b 100644 --- a/cmd/ipfs/tour.go +++ b/cmd/ipfs/tour.go @@ -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) } diff --git a/core/commands/bootstrap.go b/core/commands/bootstrap.go index 194e320af..7d0a2788c 100644 --- a/core/commands/bootstrap.go +++ b/core/commands/bootstrap.go @@ -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 } diff --git a/core/commands/config.go b/core/commands/config.go index 952df98df..8a4061520 100644 --- a/core/commands/config.go +++ b/core/commands/config.go @@ -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) } diff --git a/repo/config/config.go b/repo/config/config.go index d33e4d36d..939c4564c 100644 --- a/repo/config/config.go +++ b/repo/config/config.go @@ -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, "", " ") } diff --git a/repo/config/version.go b/repo/config/version.go index 62cacfa49..1ba3662c8 100644 --- a/repo/config/version.go +++ b/repo/config/version.go @@ -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{ diff --git a/repo/config/version_test.go b/repo/config/version_test.go index 37160c478..c4b8aeb5a 100644 --- a/repo/config/version_test.go +++ b/repo/config/version_test.go @@ -1,6 +1,7 @@ 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) } } - } diff --git a/repo/fsrepo/fsrepo.go b/repo/fsrepo/fsrepo.go index ca54be076..fb666d869 100644 --- a/repo/fsrepo/fsrepo.go +++ b/repo/fsrepo/fsrepo.go @@ -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 diff --git a/repo/config/serialize.go b/repo/fsrepo/serialize.go similarity index 64% rename from repo/config/serialize.go rename to repo/fsrepo/serialize.go index b71d945b0..5986d0f88 100644 --- a/repo/config/serialize.go +++ b/repo/fsrepo/serialize.go @@ -1,4 +1,4 @@ -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) +} diff --git a/repo/config/config_test.go b/repo/fsrepo/serialize_test.go similarity index 80% rename from repo/config/config_test.go rename to repo/fsrepo/serialize_test.go index c891d6c51..5de9674af 100644 --- a/repo/config/config_test.go +++ b/repo/fsrepo/serialize_test.go @@ -1,13 +1,15 @@ -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 { diff --git a/updates/updates.go b/updates/updates.go index fef7ed15f..19fd123b3 100644 --- a/updates/updates.go +++ b/updates/updates.go @@ -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 }