Skip to content

Commit 8c9fab9

Browse files
committed
feat: add ability to use existing config during init
1 parent c014d1e commit 8c9fab9

File tree

2 files changed

+43
-40
lines changed

2 files changed

+43
-40
lines changed

cmd/ipfs/daemon.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"sync"
1414

1515
version "github.com/ipfs/go-ipfs"
16+
config "github.com/ipfs/go-ipfs-config"
17+
cserial "github.com/ipfs/go-ipfs-config/serialize"
1618
utilmain "github.com/ipfs/go-ipfs/cmd/ipfs/util"
1719
oldcmds "github.com/ipfs/go-ipfs/commands"
1820
"github.com/ipfs/go-ipfs/core"
@@ -26,18 +28,19 @@ import (
2628
migrate "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
2729

2830
"github.com/hashicorp/go-multierror"
29-
"github.com/ipfs/go-ipfs-cmds"
31+
cmds "github.com/ipfs/go-ipfs-cmds"
3032
mprome "github.com/ipfs/go-metrics-prometheus"
3133
goprocess "github.com/jbenet/goprocess"
3234
ma "github.com/multiformats/go-multiaddr"
33-
"github.com/multiformats/go-multiaddr-net"
35+
manet "github.com/multiformats/go-multiaddr-net"
3436
"github.com/prometheus/client_golang/prometheus"
3537
)
3638

3739
const (
3840
adjustFDLimitKwd = "manage-fdlimit"
3941
enableGCKwd = "enable-gc"
4042
initOptionKwd = "init"
43+
initConfigOptionKwd = "init-config"
4144
initProfileOptionKwd = "init-profile"
4245
ipfsMountKwd = "mount-ipfs"
4346
ipnsMountKwd = "mount-ipns"
@@ -154,6 +157,7 @@ Headers.
154157

155158
Options: []cmds.Option{
156159
cmds.BoolOption(initOptionKwd, "Initialize ipfs with default settings if not already initialized"),
160+
cmds.StringOption(initConfigOptionKwd, "Path to existing configuration file to be loaded during --init"),
157161
cmds.StringOption(initProfileOptionKwd, "Configuration profiles to apply for --init. See ipfs init --help for more"),
158162
cmds.StringOption(routingOptionKwd, "Overrides the routing option").WithDefault(routingOptionDefaultKwd),
159163
cmds.BoolOption(mountKwd, "Mounts IPFS to the filesystem"),
@@ -229,17 +233,20 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
229233
// first, whether user has provided the initialization flag. we may be
230234
// running in an uninitialized state.
231235
initialize, _ := req.Options[initOptionKwd].(bool)
232-
if initialize {
236+
if initialize && !fsrepo.IsInitialized(cctx.ConfigRoot) {
237+
cfgLocation, _ := req.Options[initConfigOptionKwd].(string)
238+
profiles, _ := req.Options[initProfileOptionKwd].(string)
239+
var conf *config.Config
233240

234-
cfg := cctx.ConfigRoot
235-
if !fsrepo.IsInitialized(cfg) {
236-
profiles, _ := req.Options[initProfileOptionKwd].(string)
237-
238-
err := initWithDefaults(os.Stdout, cfg, profiles)
239-
if err != nil {
241+
if cfgLocation != "" {
242+
if conf, err = cserial.Load(cfgLocation); err != nil {
240243
return err
241244
}
242245
}
246+
247+
if err = doInit(os.Stdout, cctx.ConfigRoot, false, nBitsForKeypairDefault, profiles, conf); err != nil {
248+
return err
249+
}
243250
}
244251

245252
// acquire the repo lock _before_ constructing a node. we need to make

cmd/ipfs/init.go

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"fmt"
88
"io"
99
"os"
10-
"path"
10+
"path/filepath"
1111
"strings"
1212

1313
assets "github.com/ipfs/go-ipfs/assets"
@@ -16,9 +16,9 @@ import (
1616
namesys "github.com/ipfs/go-ipfs/namesys"
1717
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
1818

19-
"github.com/ipfs/go-ipfs-cmds"
20-
"github.com/ipfs/go-ipfs-config"
21-
"github.com/ipfs/go-ipfs-files"
19+
cmds "github.com/ipfs/go-ipfs-cmds"
20+
config "github.com/ipfs/go-ipfs-config"
21+
files "github.com/ipfs/go-ipfs-files"
2222
)
2323

2424
const (
@@ -28,6 +28,10 @@ const (
2828
profileOptionName = "profile"
2929
)
3030

31+
var errRepoExists = errors.New(`ipfs configuration file already exists!
32+
Reinitializing would overwrite your keys.
33+
`)
34+
3135
var initCmd = &cmds.Command{
3236
Helptext: cmds.HelpText{
3337
Tagline: "Initializes ipfs config file.",
@@ -102,31 +106,30 @@ environment variable:
102106
}
103107
}
104108

105-
profile, _ := req.Options[profileOptionName].(string)
106-
107-
var profiles []string
108-
if profile != "" {
109-
profiles = strings.Split(profile, ",")
110-
}
111-
109+
profiles, _ := req.Options[profileOptionName].(string)
112110
return doInit(os.Stdout, cctx.ConfigRoot, empty, nBitsForKeypair, profiles, conf)
113111
},
114112
}
115113

116-
var errRepoExists = errors.New(`ipfs configuration file already exists!
117-
Reinitializing would overwrite your keys.
118-
`)
119-
120-
func initWithDefaults(out io.Writer, repoRoot string, profile string) error {
121-
var profiles []string
122-
if profile != "" {
123-
profiles = strings.Split(profile, ",")
114+
func applyProfiles(conf *config.Config, profiles string) error {
115+
if profiles == "" {
116+
return nil
124117
}
125118

126-
return doInit(out, repoRoot, false, nBitsForKeypairDefault, profiles, nil)
119+
for _, profile := range strings.Split(profiles, ",") {
120+
transformer, ok := config.Profiles[profile]
121+
if !ok {
122+
return fmt.Errorf("invalid configuration profile: %s", profile)
123+
}
124+
125+
if err := transformer.Transform(conf); err != nil {
126+
return err
127+
}
128+
}
129+
return nil
127130
}
128131

129-
func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, confProfiles []string, conf *config.Config) error {
132+
func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, confProfiles string, conf *config.Config) error {
130133
if _, err := fmt.Fprintf(out, "initializing IPFS node at %s\n", repoRoot); err != nil {
131134
return err
132135
}
@@ -147,15 +150,8 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con
147150
}
148151
}
149152

150-
for _, profile := range confProfiles {
151-
transformer, ok := config.Profiles[profile]
152-
if !ok {
153-
return fmt.Errorf("invalid configuration profile: %s", profile)
154-
}
155-
156-
if err := transformer.Transform(conf); err != nil {
157-
return err
158-
}
153+
if err := applyProfiles(conf, confProfiles); err != nil {
154+
return err
159155
}
160156

161157
if err := fsrepo.Init(repoRoot, conf); err != nil {
@@ -175,7 +171,7 @@ func checkWritable(dir string) error {
175171
_, err := os.Stat(dir)
176172
if err == nil {
177173
// dir exists, make sure we can write to it
178-
testfile := path.Join(dir, "test")
174+
testfile := filepath.Join(dir, "test")
179175
fi, err := os.Create(testfile)
180176
if err != nil {
181177
if os.IsPermission(err) {

0 commit comments

Comments
 (0)