Exemplo n.º 1
0
// LoadConfig reads given file and returns the read config, or error.
func LoadConfig(filename string) (*Config, error) {
	if len(filename) == 0 {
		filename = defaultConfigFilePath
	}

	// tilde expansion on config file
	filename, err := u.TildeExpansion(filename)
	if err != nil {
		return nil, err
	}

	// if nothing is there, write first config file.
	if _, err := os.Stat(filename); os.IsNotExist(err) {
		WriteFile(filename, []byte(defaultConfigFile))
	}

	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
}
Exemplo n.º 2
0
func initCmd(c *commander.Command, inp []string) error {
	filename, err := config.Filename(config.DefaultConfigFilePath)
	if err != nil {
		return errors.New("Couldn't get home directory path")
	}
	fi, err := os.Lstat(filename)
	force := c.Flag.Lookup("f").Value.Get().(bool)
	if fi != nil || (err != nil && !os.IsNotExist(err)) && !force {
		return errors.New("ipfs configuration file already exists!\nReinitializing would overwrite your keys.\n(use -f to force overwrite)")
	}
	cfg := new(config.Config)

	cfg.Datastore = config.Datastore{}
	dspath, err := u.TildeExpansion("~/.go-ipfs/datastore")
	if err != nil {
		return err
	}
	cfg.Datastore.Path = dspath
	cfg.Datastore.Type = "leveldb"

	cfg.Identity = new(config.Identity)
	// This needs thought
	// cfg.Identity.Address = ""

	nbits := c.Flag.Lookup("b").Value.Get().(int)
	if nbits < 1024 {
		return errors.New("Bitsize less than 1024 is considered unsafe.")
	}

	sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
	if err != nil {
		return err
	}

	// pretend to encrypt key, then store it unencrypted
	skbytes, err := sk.Bytes()
	if err != nil {
		return err
	}
	cfg.Identity.PrivKey = base64.StdEncoding.EncodeToString(skbytes)

	id, err := identify.IDFromPubKey(pk)
	if err != nil {
		return err
	}
	cfg.Identity.PeerID = id.Pretty()

	path, err := u.TildeExpansion(config.DefaultConfigFilePath)
	if err != nil {
		return err
	}
	err = config.WriteConfigFile(path, cfg)
	if err != nil {
		return err
	}
	return nil
}
Exemplo n.º 3
0
// Load reads given file and returns the read config, or error.
func Load(filename string) (*Config, error) {
	filename, err := Filename(filename)
	if err != nil {
		return nil, err
	}

	// if nothing is there, fail. User must run 'ipfs init'
	if _, err := os.Stat(filename); os.IsNotExist(err) {
		return nil, errors.New("ipfs not initialized, please run 'ipfs init'")
	}

	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
}
Exemplo n.º 4
0
// Filename returns the proper tilde expanded config filename.
func Filename(filename string) (string, error) {
	if len(filename) == 0 {
		filename = defaultConfigFilePath
	}

	// tilde expansion on config file
	return u.TildeExpansion(filename)
}