go-ipfs-config: make it easier to detect an uninitialized repo

This commit is contained in:
Steven Allen 2019-08-29 11:48:53 -07:00
parent 356880aacf
commit 40bc2372f9

View File

@ -11,13 +11,19 @@ import (
"github.com/ipfs/go-ipfs-config"
"github.com/facebookgo/atomicfile"
"github.com/ipfs/go-ipfs-util"
)
// ErrNotInitialized is returned when we fail to read the config because the
// repo doesn't exist.
var ErrNotInitialized = errors.New("ipfs not initialized, please run 'ipfs init'")
// ReadConfigFile reads the config from `filename` into `cfg`.
func ReadConfigFile(filename string, cfg interface{}) error {
f, err := os.Open(filename)
if err != nil {
if os.IsNotExist(err) {
err = ErrNotInitialized
}
return err
}
defer f.Close()
@ -56,11 +62,6 @@ func encode(w io.Writer, value interface{}) error {
// 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, errors.New("ipfs not initialized, please run 'ipfs init'")
}
var cfg config.Config
err := ReadConfigFile(filename, &cfg)
if err != nil {