Implement ipfs init --profile
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Jun 21, 2017 at 17:24 UTC
49645f00cfa8812fa64a5586d4dceb2d622d4e2c
1 file changed
+57
-3
cmd/ipfs/init.go
+57
-3
@@ -7,6 +7,7 @@ import (
7
"io"
8
"os"
9
"path"
10
+ "strings"
11
12
context "context"
13
assets "github.com/ipfs/go-ipfs/assets"
@@ -21,6 +22,36 @@ const (
22
nBitsForKeypairDefault = 2048
23
)
24
25
+// TODO: move this out(where?)
26
+// ConfigProfiles is a map holding configuration transformers
27
+var ConfigProfiles = map[string]func(*config.Config) error{
28
+ "server": func(c *config.Config) error {
29
+
30
+ // defaultServerFilters has a list of non-routable IPv4 prefixes
31
+ // according to http://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
32
+ defaultServerFilters := []string{
33
+ "/ip4/10.0.0.0/ipcidr/8",
34
+ "/ip4/100.64.0.0/ipcidr/10",
35
+ "/ip4/169.254.0.0/ipcidr/16",
36
+ "/ip4/172.16.0.0/ipcidr/12",
37
+ "/ip4/192.0.0.0/ipcidr/24",
38
+ "/ip4/192.0.0.0/ipcidr/29",
39
+ "/ip4/192.0.0.8/ipcidr/32",
40
+ "/ip4/192.0.0.170/ipcidr/32",
41
+ "/ip4/192.0.0.171/ipcidr/32",
42
+ "/ip4/192.0.2.0/ipcidr/24",
43
+ "/ip4/192.168.0.0/ipcidr/16",
44
+ "/ip4/198.18.0.0/ipcidr/15",
45
+ "/ip4/198.51.100.0/ipcidr/24",
46
+ "/ip4/203.0.113.0/ipcidr/24",
47
+ "/ip4/240.0.0.0/ipcidr/4",
48
+ }
49
+
50
+ c.Swarm.AddrFilters = append(c.Swarm.AddrFilters, defaultServerFilters...)
51
+ return nil
52
+ },
53
+}
54
+
55
var initCmd = &cmds.Command{
56
Helptext: cmds.HelpText{
57
Tagline: "Initializes ipfs config file.",
@@ -40,6 +71,7 @@ environment variable:
71
Options: []cmds.Option{
72
cmds.IntOption("bits", "b", "Number of bits to use in the generated RSA private key.").Default(nBitsForKeypairDefault),
73
cmds.BoolOption("empty-repo", "e", "Don't add and pin help files to the local storage.").Default(false),
74
+ cmds.StringOption("profile", "p", "Apply profile settings to config"),
75
76
// TODO need to decide whether to expose the override as a file or a
77
// directory. That is: should we allow the user to also specify the
@@ -96,7 +128,18 @@ environment variable:
128
}
129
}
130
99
- if err := doInit(os.Stdout, req.InvocContext().ConfigRoot, empty, nBitsForKeypair, conf); err != nil {
131
+ profile, _, err := req.Option("profile").String()
132
+ if err != nil {
133
+ res.SetError(err, cmds.ErrNormal)
134
+ return
135
+ }
136
+
137
+ var profiles []string
138
+ if profile != "" {
139
+ profiles = strings.Split(profile, ",")
140
+ }
141
+
142
+ if err := doInit(os.Stdout, req.InvocContext().ConfigRoot, empty, nBitsForKeypair, profiles, conf); err != nil {
143
res.SetError(err, cmds.ErrNormal)
144
return
145
}
@@ -108,10 +151,10 @@ Reinitializing would overwrite your keys.
151
`)
152
153
func initWithDefaults(out io.Writer, repoRoot string) error {
111
- return doInit(out, repoRoot, false, nBitsForKeypairDefault, nil)
154
+ return doInit(out, repoRoot, false, nBitsForKeypairDefault, nil, nil)
155
}
156
114
-func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, conf *config.Config) error {
157
+func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, confProfiles []string, conf *config.Config) error {
158
if _, err := fmt.Fprintf(out, "initializing IPFS node at %s\n", repoRoot); err != nil {
159
return err
160
}
@@ -132,6 +175,17 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, con
175
}
176
}
177
178
+ for _, profile := range confProfiles {
179
+ transformer := ConfigProfiles[profile]
180
+ if transformer == nil {
181
+ return fmt.Errorf("invalid configuration profile: %s", profile)
182
+ }
183
+
184
+ if err := transformer(conf); err != nil {
185
+ return err
186
+ }
187
+ }
188
+
189
if err := fsrepo.Init(repoRoot, conf); err != nil {
190
return err
191
}