Skip to content

Commit

Permalink
use profile transforms on init config-file
Browse files Browse the repository at this point in the history
  • Loading branch information
djdv committed Jul 10, 2019
1 parent f1f726c commit 73615a8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 36 deletions.
8 changes: 2 additions & 6 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,23 +237,19 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
profiles, _ := req.Options[initProfileOptionKwd].(string)

if cfgLocation != "" {
if profiles != "" {
return errInitConfigArgs
}

conf, err := cserial.Load(cfgLocation)
if err != nil {
return err
}

if err = doInit(os.Stdout, cctx.ConfigRoot, false, nBitsForKeypairDefault, nil, conf); err != nil {
if err = doInit(os.Stdout, cctx.ConfigRoot, false, nBitsForKeypairDefault, profiles, conf); err != nil {
return err
}
} else {
cfgLocation = cctx.ConfigRoot

if !fsrepo.IsInitialized(cfgLocation) {
err := initWithDefaults(os.Stdout, cfgLocation, profiles)
err := doInit(os.Stdout, cfgLocation, false, nBitsForKeypairDefault, profiles, nil)
if err != nil {
return err
}
Expand Down
50 changes: 20 additions & 30 deletions cmd/ipfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ const (
configOptionName = "config-file"
)

var (
errRepoExists = errors.New(`ipfs configuration file already exists!
var errRepoExists = errors.New(`ipfs configuration file already exists!
Reinitializing would overwrite your keys.
`)
errInitConfigArgs = errors.New("Config file <-> Profile merging not implemented")
)

var initCmd = &cmds.Command{
Helptext: cmds.HelpText{
Expand Down Expand Up @@ -113,40 +110,40 @@ environment variable:
}

cfgLocation, _ := req.Options[configOptionName].(string)
profile, _ := req.Options[profileOptionName].(string)
profiles, _ := req.Options[profileOptionName].(string)

if cfgLocation != "" {
if profile != "" {
return errInitConfigArgs
}

conf, err := cserial.Load(cfgLocation)
if err != nil {
return err
}

return doInit(os.Stdout, cctx.ConfigRoot, false, nBitsForKeypairDefault, nil, conf)
}

var profiles []string
if profile != "" {
profiles = strings.Split(profile, ",")
return doInit(os.Stdout, cctx.ConfigRoot, false, nBitsForKeypairDefault, profiles, conf)
}

return doInit(os.Stdout, cctx.ConfigRoot, empty, nBitsForKeypair, profiles, conf)
},
}

func initWithDefaults(out io.Writer, repoRoot string, profile string) error {
var profiles []string
if profile != "" {
profiles = strings.Split(profile, ",")
func applyProfiles(conf *config.Config, profiles string) error {
if profiles == "" {
return nil
}

return doInit(out, repoRoot, false, nBitsForKeypairDefault, profiles, nil)
for _, profile := range strings.Split(profiles, ",") {
transformer, ok := config.Profiles[profile]
if !ok {
return fmt.Errorf("invalid configuration profile: %s", profile)
}

if err := transformer.Transform(conf); err != nil {
return err
}
}
return nil
}

func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, confProfiles []string, conf *config.Config) error {
func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, confProfiles string, conf *config.Config) error {
if _, err := fmt.Fprintf(out, "initializing IPFS node at %s\n", repoRoot); err != nil {
return err
}
Expand All @@ -167,15 +164,8 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con
}
}

for _, profile := range confProfiles {
transformer, ok := config.Profiles[profile]
if !ok {
return fmt.Errorf("invalid configuration profile: %s", profile)
}

if err := transformer.Transform(conf); err != nil {
return err
}
if err := applyProfiles(conf, confProfiles); err != nil {
return err
}

if err := fsrepo.Init(repoRoot, conf); err != nil {
Expand Down

0 comments on commit 73615a8

Please sign in to comment.