@cryptotaxi247 / kubo / commits / 995e674cf

feat: add ability to use existing config during init

Dominic Della Valle committed Jul 8, 2019 at 14:35 UTC 995e674cfbc3b4c1afed4c149771529f40db0aff
2 files changed +43 -40
cmd/ipfs/daemon.go
+16 -9
@@ -13,6 +13,8 @@ import (
13 "sync"
14
15 version "github.com/ipfs/go-ipfs"
16 + config "github.com/ipfs/go-ipfs-config"
17 + cserial "github.com/ipfs/go-ipfs-config/serialize"
18 utilmain "github.com/ipfs/go-ipfs/cmd/ipfs/util"
19 oldcmds "github.com/ipfs/go-ipfs/commands"
20 "github.com/ipfs/go-ipfs/core"
@@ -26,11 +28,11 @@ import (
28 migrate "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
29
30 "github.com/hashicorp/go-multierror"
29 - "github.com/ipfs/go-ipfs-cmds"
31 + cmds "github.com/ipfs/go-ipfs-cmds"
32 mprome "github.com/ipfs/go-metrics-prometheus"
33 goprocess "github.com/jbenet/goprocess"
34 ma "github.com/multiformats/go-multiaddr"
33 - "github.com/multiformats/go-multiaddr-net"
35 + manet "github.com/multiformats/go-multiaddr-net"
36 "github.com/prometheus/client_golang/prometheus"
37 )
38
@@ -38,6 +40,7 @@ const (
40 adjustFDLimitKwd = "manage-fdlimit"
41 enableGCKwd = "enable-gc"
42 initOptionKwd = "init"
43 + initConfigOptionKwd = "init-config"
44 initProfileOptionKwd = "init-profile"
45 ipfsMountKwd = "mount-ipfs"
46 ipnsMountKwd = "mount-ipns"
@@ -154,6 +157,7 @@ Headers.
157
158 Options: []cmds.Option{
159 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"),
161 cmds.StringOption(initProfileOptionKwd, "Configuration profiles to apply for --init. See ipfs init --help for more"),
162 cmds.StringOption(routingOptionKwd, "Overrides the routing option").WithDefault(routingOptionDefaultKwd),
163 cmds.BoolOption(mountKwd, "Mounts IPFS to the filesystem"),
@@ -229,17 +233,20 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
233 // first, whether user has provided the initialization flag. we may be
234 // running in an uninitialized state.
235 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
240
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 {
243 return err
244 }
245 }
246 +
247 + if err = doInit(os.Stdout, cctx.ConfigRoot, false, nBitsForKeypairDefault, profiles, conf); err != nil {
248 + return err
249 + }
250 }
251
252 // acquire the repo lock _before_ constructing a node. we need to make
cmd/ipfs/init.go
+27 -31
@@ -7,7 +7,7 @@ import (
7 "fmt"
8 "io"
9 "os"
10 - "path"
10 + "path/filepath"
11 "strings"
12
13 assets "github.com/ipfs/go-ipfs/assets"
@@ -16,9 +16,9 @@ import (
16 namesys "github.com/ipfs/go-ipfs/namesys"
17 fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
18
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"
22 )
23
24 const (
@@ -28,6 +28,10 @@ const (
28 profileOptionName = "profile"
29 )
30
31 +var errRepoExists = errors.New(`ipfs configuration file already exists!
32 +Reinitializing would overwrite your keys.
33 +`)
34 +
35 var initCmd = &cmds.Command{
36 Helptext: cmds.HelpText{
37 Tagline: "Initializes ipfs config file.",
@@ -102,31 +106,30 @@ environment variable:
106 }
107 }
108
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)
110 return doInit(os.Stdout, cctx.ConfigRoot, empty, nBitsForKeypair, profiles, conf)
111 },
112 }
113
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
117 }
118
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
130 }
131
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 {
133 if _, err := fmt.Fprintf(out, "initializing IPFS node at %s\n", repoRoot); err != nil {
134 return err
135 }
@@ -147,15 +150,8 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con
150 }
151 }
152
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
155 }
156
157 if err := fsrepo.Init(repoRoot, conf); err != nil {
@@ -175,7 +171,7 @@ func checkWritable(dir string) error {
171 _, err := os.Stat(dir)
172 if err == nil {
173 // dir exists, make sure we can write to it
178 - testfile := path.Join(dir, "test")
174 + testfile := filepath.Join(dir, "test")
175 fi, err := os.Create(testfile)
176 if err != nil {
177 if os.IsPermission(err) {