Skip to content

Commit 95cbc78

Browse files
committed
config: introduce Import section
1 parent 2841ec0 commit 95cbc78

File tree

10 files changed

+225
-8
lines changed

10 files changed

+225
-8
lines changed

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Config struct {
3636
Experimental Experiments
3737
Plugins Plugins
3838
Pinning Pinning
39+
Import Import
3940

4041
Internal Internal // experimental/unstable options
4142
}

config/import.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package config
2+
3+
const (
4+
DefaultCidVersion = 0
5+
DefaultUnixFSRawLeaves = false
6+
DefaultUnixFSChunker = "size-262144"
7+
DefaultHashFunction = "sha2-256"
8+
)
9+
10+
// Import configures the default options for ingesting data. This affects commands
11+
// that ingest data, such as 'ipfs add', 'ipfs dag put, 'ipfs block put', 'ipfs files write'.
12+
type Import struct {
13+
CidVersion OptionalInteger
14+
UnixFSRawLeaves Flag
15+
UnixFSChunker OptionalString
16+
HashFunction OptionalString
17+
}

core/commands/add.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
gopath "path"
99
"strings"
1010

11+
"github.com/ipfs/kubo/config"
1112
"github.com/ipfs/kubo/core/commands/cmdenv"
1213

1314
"github.com/cheggaaa/pb"
@@ -155,12 +156,12 @@ See 'dag export' and 'dag import' for more information.
155156
cmds.BoolOption(trickleOptionName, "t", "Use trickle-dag format for dag generation."),
156157
cmds.BoolOption(onlyHashOptionName, "n", "Only chunk and hash - do not write to disk."),
157158
cmds.BoolOption(wrapOptionName, "w", "Wrap files with a directory object."),
158-
cmds.StringOption(chunkerOptionName, "s", "Chunking algorithm, size-[bytes], rabin-[min]-[avg]-[max] or buzhash").WithDefault("size-262144"),
159+
cmds.StringOption(chunkerOptionName, "s", "Chunking algorithm, size-[bytes], rabin-[min]-[avg]-[max] or buzhash"),
159160
cmds.BoolOption(rawLeavesOptionName, "Use raw blocks for leaf nodes."),
160161
cmds.BoolOption(noCopyOptionName, "Add the file using filestore. Implies raw-leaves. (experimental)"),
161162
cmds.BoolOption(fstoreCacheOptionName, "Check the filestore for pre-existing blocks. (experimental)"),
162163
cmds.IntOption(cidVersionOptionName, "CID version. Defaults to 0 unless an option that depends on CIDv1 is passed. Passing version 1 will cause the raw-leaves option to default to true."),
163-
cmds.StringOption(hashOptionName, "Hash function to use. Implies CIDv1 if not sha2-256. (experimental)").WithDefault("sha2-256"),
164+
cmds.StringOption(hashOptionName, "Hash function to use. Implies CIDv1 if not sha2-256. (experimental)"),
164165
cmds.BoolOption(inlineOptionName, "Inline small blocks into CIDs. (experimental)"),
165166
cmds.IntOption(inlineLimitOptionName, "Maximum block size to inline. (experimental)").WithDefault(32),
166167
cmds.BoolOption(pinOptionName, "Pin locally to protect added files from garbage collection.").WithDefault(true),
@@ -191,6 +192,16 @@ See 'dag export' and 'dag import' for more information.
191192
return err
192193
}
193194

195+
nd, err := cmdenv.GetNode(env)
196+
if err != nil {
197+
return err
198+
}
199+
200+
cfg, err := nd.Repo.Config()
201+
if err != nil {
202+
return err
203+
}
204+
194205
progress, _ := req.Options[progressOptionName].(bool)
195206
trickle, _ := req.Options[trickleOptionName].(bool)
196207
wrap, _ := req.Options[wrapOptionName].(bool)
@@ -207,6 +218,24 @@ See 'dag export' and 'dag import' for more information.
207218
inlineLimit, _ := req.Options[inlineLimitOptionName].(int)
208219
toFilesStr, toFilesSet := req.Options[toFilesOptionName].(string)
209220

221+
if chunker == "" {
222+
chunker = cfg.Import.UnixFSChunker.WithDefault(config.DefaultUnixFSChunker)
223+
}
224+
225+
if hashFunStr == "" {
226+
hashFunStr = cfg.Import.HashFunction.WithDefault(config.DefaultHashFunction)
227+
}
228+
229+
if !cidVerSet && !cfg.Import.CidVersion.IsDefault() {
230+
cidVerSet = true
231+
cidVer = int(cfg.Import.CidVersion.WithDefault(config.DefaultCidVersion))
232+
}
233+
234+
if !rbset && cfg.Import.UnixFSRawLeaves != config.Default {
235+
rbset = true
236+
rawblks = cfg.Import.UnixFSRawLeaves.WithDefault(config.DefaultUnixFSRawLeaves)
237+
}
238+
210239
if onlyHash && toFilesSet {
211240
return fmt.Errorf("%s and %s options are not compatible", onlyHashOptionName, toFilesOptionName)
212241
}

core/commands/block.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/ipfs/boxo/files"
1010

11+
"github.com/ipfs/kubo/config"
1112
cmdenv "github.com/ipfs/kubo/core/commands/cmdenv"
1213
"github.com/ipfs/kubo/core/commands/cmdutils"
1314

@@ -153,7 +154,7 @@ only for backward compatibility when a legacy CIDv0 is required (--format=v0).
153154
},
154155
Options: []cmds.Option{
155156
cmds.StringOption(blockCidCodecOptionName, "Multicodec to use in returned CID").WithDefault("raw"),
156-
cmds.StringOption(mhtypeOptionName, "Multihash hash function").WithDefault("sha2-256"),
157+
cmds.StringOption(mhtypeOptionName, "Multihash hash function"),
157158
cmds.IntOption(mhlenOptionName, "Multihash hash length").WithDefault(-1),
158159
cmds.BoolOption(pinOptionName, "Pin added blocks recursively").WithDefault(false),
159160
cmdutils.AllowBigBlockOption,
@@ -165,7 +166,21 @@ only for backward compatibility when a legacy CIDv0 is required (--format=v0).
165166
return err
166167
}
167168

169+
nd, err := cmdenv.GetNode(env)
170+
if err != nil {
171+
return err
172+
}
173+
174+
cfg, err := nd.Repo.Config()
175+
if err != nil {
176+
return err
177+
}
178+
168179
mhtype, _ := req.Options[mhtypeOptionName].(string)
180+
if mhtype == "" {
181+
mhtype = cfg.Import.HashFunction.WithDefault(config.DefaultHashFunction)
182+
}
183+
169184
mhtval, ok := mh.Names[mhtype]
170185
if !ok {
171186
return fmt.Errorf("unrecognized multihash function: %s", mhtype)

core/commands/dag/dag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ into an object of the specified format.
8787
cmds.StringOption("store-codec", "Codec that the stored object will be encoded with").WithDefault("dag-cbor"),
8888
cmds.StringOption("input-codec", "Codec that the input object is encoded in").WithDefault("dag-json"),
8989
cmds.BoolOption("pin", "Pin this object when adding."),
90-
cmds.StringOption("hash", "Hash function to use").WithDefault("sha2-256"),
90+
cmds.StringOption("hash", "Hash function to use"),
9191
cmdutils.AllowBigBlockOption,
9292
},
9393
Run: dagPut,

core/commands/dag/put.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
blocks "github.com/ipfs/go-block-format"
88
"github.com/ipfs/go-cid"
99
ipldlegacy "github.com/ipfs/go-ipld-legacy"
10+
"github.com/ipfs/kubo/config"
1011
"github.com/ipfs/kubo/core/commands/cmdenv"
1112
"github.com/ipfs/kubo/core/commands/cmdutils"
1213
"github.com/ipld/go-ipld-prime/multicodec"
@@ -32,11 +33,25 @@ func dagPut(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) e
3233
return err
3334
}
3435

36+
nd, err := cmdenv.GetNode(env)
37+
if err != nil {
38+
return err
39+
}
40+
41+
cfg, err := nd.Repo.Config()
42+
if err != nil {
43+
return err
44+
}
45+
3546
inputCodec, _ := req.Options["input-codec"].(string)
3647
storeCodec, _ := req.Options["store-codec"].(string)
3748
hash, _ := req.Options["hash"].(string)
3849
dopin, _ := req.Options["pin"].(bool)
3950

51+
if hash == "" {
52+
hash = cfg.Import.HashFunction.WithDefault(config.DefaultHashFunction)
53+
}
54+
4055
var icodec mc.Code
4156
if err := icodec.Set(inputCodec); err != nil {
4257
return err

core/commands/files.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212

1313
humanize "github.com/dustin/go-humanize"
14+
"github.com/ipfs/kubo/config"
1415
"github.com/ipfs/kubo/core"
1516
"github.com/ipfs/kubo/core/commands/cmdenv"
1617

@@ -802,18 +803,28 @@ See '--to-files' in 'ipfs add --help' for more information.
802803
return err
803804
}
804805

806+
nd, err := cmdenv.GetNode(env)
807+
if err != nil {
808+
return err
809+
}
810+
811+
cfg, err := nd.Repo.Config()
812+
if err != nil {
813+
return err
814+
}
815+
805816
create, _ := req.Options[filesCreateOptionName].(bool)
806817
mkParents, _ := req.Options[filesParentsOptionName].(bool)
807818
trunc, _ := req.Options[filesTruncateOptionName].(bool)
808819
flush, _ := req.Options[filesFlushOptionName].(bool)
809820
rawLeaves, rawLeavesDef := req.Options[filesRawLeavesOptionName].(bool)
810821

811-
prefix, err := getPrefixNew(req)
812-
if err != nil {
813-
return err
822+
if !rawLeavesDef && cfg.Import.UnixFSRawLeaves != config.Default {
823+
rawLeavesDef = true
824+
rawLeaves = cfg.Import.UnixFSRawLeaves.WithDefault(config.DefaultUnixFSRawLeaves)
814825
}
815826

816-
nd, err := cmdenv.GetNode(env)
827+
prefix, err := getPrefixNew(req)
817828
if err != nil {
818829
return err
819830
}

docs/changelogs/v0.29.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
### 🔦 Highlights
1515

16+
#### Global configuration for data ingestion
17+
18+
A new configuration section, `Import`, was introduced. This section allows to override the default values of options used across several commands that do data ingestion. Read more in the [config documentation](../config.md).
19+
1620
### 📝 Changelog
1721

1822
### 👨‍👩‍👧‍👦 Contributors

docs/config.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ config file at runtime.
175175
- [`DNS`](#dns)
176176
- [`DNS.Resolvers`](#dnsresolvers)
177177
- [`DNS.MaxCacheTTL`](#dnsmaxcachettl)
178+
- [`Import`](#import)
179+
- [`Import.CidVersion`](#importcidversion)
180+
- [`Import.UnixFSRawLeaves`](#importunixfsrawleaves)
181+
- [`Import.UnixFSChunker`](#importunixfschunker)
182+
- [`Import.HashFunction`](#importhashfunction)
178183

179184
## Profiles
180185

@@ -2377,3 +2382,42 @@ Note: this does NOT work with Go's default DNS resolver. To make this a global s
23772382
Default: Respect DNS Response TTL
23782383

23792384
Type: `optionalDuration`
2385+
2386+
## `Import`
2387+
2388+
Options to configure the default options used for ingesting data, in commands such as `ipfs add` or `ipfs block put`. All affected commands are detailed per option.
2389+
2390+
Note that using flags will override the options defined here.
2391+
2392+
### `Import.CidVersion`
2393+
2394+
The default CID version. Commands affected: `ipfs add`.
2395+
2396+
Default: `0`
2397+
2398+
Type: `optionalInteger`
2399+
2400+
### `Import.UnixFSRawLeaves`
2401+
2402+
The default UnixFS raw leaves option. Commands affected: `ipfs add`, `ipfs files write`.
2403+
2404+
Default: `false`
2405+
2406+
Type: `flag`
2407+
2408+
### `Import.UnixFSChunker`
2409+
2410+
The default UnixFS chunker. Commands affected: `ipfs add`.
2411+
2412+
Default: `size-262144`
2413+
2414+
Type: `optionalString`
2415+
2416+
2417+
### `Import.HashFunction`
2418+
2419+
The default hash function. Commands affected: `ipfs add`, `ipfs block put`, `ipfs dag put`.
2420+
2421+
Default: `sha2-256`
2422+
2423+
Type: `optionalString`

test/cli/add_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package cli
2+
3+
import (
4+
"testing"
5+
6+
"github.com/ipfs/kubo/config"
7+
"github.com/ipfs/kubo/test/cli/harness"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestAdd(t *testing.T) {
12+
t.Parallel()
13+
14+
var (
15+
shortString = "hello world"
16+
shortStringCidV0 = "Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD"
17+
shortStringCidV1 = "bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e"
18+
shortStringCidV1Sha512 = "bafkrgqbqt3gerhas23vuzrapkdeqf4vu2dwxp3srdj6hvg6nhsug2tgyn6mj3u23yx7utftq3i2ckw2fwdh5qmhid5qf3t35yvkc5e5ottlw6"
19+
)
20+
21+
t.Run("output cid version: default (CIDv0)", func(t *testing.T) {
22+
t.Parallel()
23+
node := harness.NewT(t).NewNode().Init().StartDaemon()
24+
defer node.StopDaemon()
25+
26+
cidStr := node.IPFSAddStr(shortString)
27+
require.Equal(t, shortStringCidV0, cidStr)
28+
})
29+
30+
t.Run("output cid version: follows configuration (CIDv0)", func(t *testing.T) {
31+
t.Parallel()
32+
node := harness.NewT(t).NewNode().Init()
33+
node.UpdateConfig(func(cfg *config.Config) {
34+
cfg.Import.CidVersion = *config.NewOptionalInteger(0)
35+
})
36+
node.StartDaemon()
37+
defer node.StopDaemon()
38+
39+
cidStr := node.IPFSAddStr(shortString)
40+
require.Equal(t, shortStringCidV0, cidStr)
41+
})
42+
43+
t.Run("output cid version: follows configuration (hash function)", func(t *testing.T) {
44+
t.Parallel()
45+
node := harness.NewT(t).NewNode().Init()
46+
node.UpdateConfig(func(cfg *config.Config) {
47+
cfg.Import.HashFunction = *config.NewOptionalString("sha2-512")
48+
})
49+
node.StartDaemon()
50+
defer node.StopDaemon()
51+
52+
cidStr := node.IPFSAddStr(shortString)
53+
require.Equal(t, shortStringCidV1Sha512, cidStr)
54+
})
55+
56+
t.Run("output cid version: follows configuration (CIDv1)", func(t *testing.T) {
57+
t.Parallel()
58+
node := harness.NewT(t).NewNode().Init()
59+
node.UpdateConfig(func(cfg *config.Config) {
60+
cfg.Import.CidVersion = *config.NewOptionalInteger(1)
61+
})
62+
node.StartDaemon()
63+
defer node.StopDaemon()
64+
65+
cidStr := node.IPFSAddStr(shortString)
66+
require.Equal(t, shortStringCidV1, cidStr)
67+
})
68+
69+
t.Run("output cid version: flag overrides configuration", func(t *testing.T) {
70+
t.Parallel()
71+
node := harness.NewT(t).NewNode().Init()
72+
node.UpdateConfig(func(cfg *config.Config) {
73+
cfg.Import.CidVersion = *config.NewOptionalInteger(1)
74+
})
75+
node.StartDaemon()
76+
defer node.StopDaemon()
77+
78+
cidStr := node.IPFSAddStr(shortString, "--cid-version", "0")
79+
require.Equal(t, shortStringCidV0, cidStr)
80+
})
81+
}

0 commit comments

Comments
 (0)