mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-29 01:12:24 +08:00
Implements repository initialization with default config
Modified init command to receive default configuration from stdin. The changes enable us to use existing key-pair, datastore configuration while initializing new ipfs node. License: MIT Signed-off-by: Sivachandran <sivachandran.p@gmail.com>
This commit is contained in:

committed by
Sivachandran

parent
d597f4b89d
commit
6989686b8b
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -30,7 +31,9 @@ at ~/.ipfs. To change the repo location, set the $IPFS_PATH environment variable
|
|||||||
export IPFS_PATH=/path/to/ipfsrepo
|
export IPFS_PATH=/path/to/ipfsrepo
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
|
Arguments: []cmds.Argument{
|
||||||
|
cmds.FileArg("default-config", false, false, "Initialize with the given configuration.").EnableStdin(),
|
||||||
|
},
|
||||||
Options: []cmds.Option{
|
Options: []cmds.Option{
|
||||||
cmds.IntOption("bits", "b", fmt.Sprintf("Number of bits to use in the generated RSA private key (defaults to %d)", nBitsForKeypairDefault)),
|
cmds.IntOption("bits", "b", fmt.Sprintf("Number of bits to use in the generated RSA private key (defaults to %d)", nBitsForKeypairDefault)),
|
||||||
cmds.BoolOption("empty-repo", "e", "Don't add and pin help files to the local storage."),
|
cmds.BoolOption("empty-repo", "e", "Don't add and pin help files to the local storage."),
|
||||||
@ -77,7 +80,24 @@ at ~/.ipfs. To change the repo location, set the $IPFS_PATH environment variable
|
|||||||
nBitsForKeypair = nBitsForKeypairDefault
|
nBitsForKeypair = nBitsForKeypairDefault
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := doInit(os.Stdout, req.InvocContext().ConfigRoot, empty, nBitsForKeypair); err != nil {
|
var conf *config.Config
|
||||||
|
|
||||||
|
f := req.Files()
|
||||||
|
if f != nil {
|
||||||
|
confFile, err := f.NextFile()
|
||||||
|
if err != nil {
|
||||||
|
res.SetError(err, cmds.ErrNormal)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
conf = &config.Config{}
|
||||||
|
if err := json.NewDecoder(confFile).Decode(conf); err != nil {
|
||||||
|
res.SetError(err, cmds.ErrNormal)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := doInit(os.Stdout, req.InvocContext().ConfigRoot, empty, nBitsForKeypair, conf); err != nil {
|
||||||
res.SetError(err, cmds.ErrNormal)
|
res.SetError(err, cmds.ErrNormal)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -89,10 +109,10 @@ Reinitializing would overwrite your keys.
|
|||||||
`)
|
`)
|
||||||
|
|
||||||
func initWithDefaults(out io.Writer, repoRoot string) error {
|
func initWithDefaults(out io.Writer, repoRoot string) error {
|
||||||
return doInit(out, repoRoot, false, nBitsForKeypairDefault)
|
return doInit(out, repoRoot, false, nBitsForKeypairDefault, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int) error {
|
func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int, conf *config.Config) error {
|
||||||
if _, err := fmt.Fprintf(out, "initializing ipfs node at %s\n", repoRoot); err != nil {
|
if _, err := fmt.Fprintf(out, "initializing ipfs node at %s\n", repoRoot); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -105,9 +125,12 @@ func doInit(out io.Writer, repoRoot string, empty bool, nBitsForKeypair int) err
|
|||||||
return errRepoExists
|
return errRepoExists
|
||||||
}
|
}
|
||||||
|
|
||||||
conf, err := config.Init(out, nBitsForKeypair)
|
if conf == nil {
|
||||||
if err != nil {
|
var err error
|
||||||
return err
|
conf, err = config.Init(out, nBitsForKeypair)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := fsrepo.Init(repoRoot, conf); err != nil {
|
if err := fsrepo.Init(repoRoot, conf); err != nil {
|
||||||
|
55
test/sharness/t0022-init-default.sh
Executable file
55
test/sharness/t0022-init-default.sh
Executable file
@ -0,0 +1,55 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
#
|
||||||
|
# Copyright (c) 2014 Christian Couder
|
||||||
|
# MIT Licensed; see the LICENSE file in this repository.
|
||||||
|
#
|
||||||
|
|
||||||
|
test_description="Test init command with default config"
|
||||||
|
|
||||||
|
. lib/test-lib.sh
|
||||||
|
|
||||||
|
cfg_key="Addresses.API"
|
||||||
|
cfg_val="/ip4/0.0.0.0/tcp/5001"
|
||||||
|
|
||||||
|
# test that init succeeds
|
||||||
|
test_expect_success "ipfs init succeeds" '
|
||||||
|
export IPFS_PATH="$(pwd)/.ipfs" &&
|
||||||
|
echo "IPFS_PATH: \"$IPFS_PATH\"" &&
|
||||||
|
BITS="2048" &&
|
||||||
|
ipfs init --bits="$BITS" >actual_init ||
|
||||||
|
test_fsh cat actual_init
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success ".ipfs/config has been created" '
|
||||||
|
test -f "$IPFS_PATH"/config ||
|
||||||
|
test_fsh ls -al .ipfs
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success "ipfs config succeeds" '
|
||||||
|
ipfs config $cfg_flags "$cfg_key" "$cfg_val"
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success "ipfs read config succeeds" '
|
||||||
|
IPFS_DEFAULT_CONFIG=$(cat "$IPFS_PATH"/config)
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success "clean up ipfs dir" '
|
||||||
|
rm -rf "$IPFS_PATH"
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success "ipfs init default config succeeds" '
|
||||||
|
echo $IPFS_DEFAULT_CONFIG | ipfs init >actual_init ||
|
||||||
|
test_fsh cat actual_init
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success "ipfs config output looks good" '
|
||||||
|
echo "$cfg_val" >expected &&
|
||||||
|
ipfs config "$cfg_key" >actual &&
|
||||||
|
test_cmp expected actual
|
||||||
|
'
|
||||||
|
|
||||||
|
test_launch_ipfs_daemon
|
||||||
|
|
||||||
|
test_kill_ipfs_daemon
|
||||||
|
|
||||||
|
test_done
|
Reference in New Issue
Block a user