提交 70819671 作者: Henry

cleaned up 'ipfs init'

上级 d88ba193
...@@ -28,7 +28,6 @@ var initCmd = &cmds.Command{ ...@@ -28,7 +28,6 @@ var initCmd = &cmds.Command{
Options: []cmds.Option{ Options: []cmds.Option{
cmds.IntOption("bits", "b", "Number of bits to use in the generated RSA private key (defaults to 4096)"), cmds.IntOption("bits", "b", "Number of bits to use in the generated RSA private key (defaults to 4096)"),
cmds.StringOption("passphrase", "p", "Passphrase for encrypting the private key"),
cmds.BoolOption("force", "f", "Overwrite existing config (if it exists)"), cmds.BoolOption("force", "f", "Overwrite existing config (if it exists)"),
// TODO need to decide whether to expose the override as a file or a // TODO need to decide whether to expose the override as a file or a
...@@ -49,6 +48,7 @@ var initCmd = &cmds.Command{ ...@@ -49,6 +48,7 @@ var initCmd = &cmds.Command{
res.SetError(err, cmds.ErrNormal) res.SetError(err, cmds.ErrNormal)
return return
} }
if !bitsOptFound { if !bitsOptFound {
nBitsForKeypair = nBitsForKeypairDefault nBitsForKeypair = nBitsForKeypairDefault
} }
...@@ -75,28 +75,26 @@ func initWithDefaults(out io.Writer, repoRoot string) error { ...@@ -75,28 +75,26 @@ func initWithDefaults(out io.Writer, repoRoot string) error {
return debugerror.Wrap(err) return debugerror.Wrap(err)
} }
func writef(out io.Writer, format string, ifs ...interface{}) error {
_, err := out.Write([]byte(fmt.Sprintf(format, ifs...)))
return err
}
func doInit(out io.Writer, repoRoot string, force bool, nBitsForKeypair int) error { func doInit(out io.Writer, repoRoot string, force bool, nBitsForKeypair int) error {
if err := writef(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
} }
if fsrepo.IsInitialized(repoRoot) && !force { if fsrepo.IsInitialized(repoRoot) && !force {
return errRepoExists return errRepoExists
} }
conf, err := config.Init(out, nBitsForKeypair) conf, err := config.Init(out, nBitsForKeypair)
if err != nil { if err != nil {
return err return err
} }
if fsrepo.IsInitialized(repoRoot) { if fsrepo.IsInitialized(repoRoot) {
if err := fsrepo.Remove(repoRoot); err != nil { if err := fsrepo.Remove(repoRoot); err != nil {
return err return err
} }
} }
if err := fsrepo.Init(repoRoot, conf); err != nil { if err := fsrepo.Init(repoRoot, conf); err != nil {
return err return err
} }
...@@ -111,10 +109,12 @@ func doInit(out io.Writer, repoRoot string, force bool, nBitsForKeypair int) err ...@@ -111,10 +109,12 @@ func doInit(out io.Writer, repoRoot string, force bool, nBitsForKeypair int) err
func addDefaultAssets(out io.Writer, repoRoot string) error { func addDefaultAssets(out io.Writer, repoRoot string) error {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
r := fsrepo.At(repoRoot) r := fsrepo.At(repoRoot)
if err := r.Open(); err != nil { // NB: repo is owned by the node if err := r.Open(); err != nil { // NB: repo is owned by the node
return err return err
} }
nd, err := core.NewIPFSNode(ctx, core.Offline(r)) nd, err := core.NewIPFSNode(ctx, core.Offline(r))
if err != nil { if err != nil {
return err return err
...@@ -130,6 +130,7 @@ func addDefaultAssets(out io.Writer, repoRoot string) error { ...@@ -130,6 +130,7 @@ func addDefaultAssets(out io.Writer, repoRoot string) error {
if err != nil { if err != nil {
return err return err
} }
k := u.B58KeyDecode(s) k := u.B58KeyDecode(s)
if err := dirb.AddChild(fname, k); err != nil { if err := dirb.AddChild(fname, k); err != nil {
return err return err
...@@ -141,15 +142,21 @@ func addDefaultAssets(out io.Writer, repoRoot string) error { ...@@ -141,15 +142,21 @@ func addDefaultAssets(out io.Writer, repoRoot string) error {
if err != nil { if err != nil {
return err return err
} }
if err := nd.Pinning.Pin(dir, true); err != nil { if err := nd.Pinning.Pin(dir, true); err != nil {
return err return err
} }
if err := nd.Pinning.Flush(); err != nil { if err := nd.Pinning.Flush(); err != nil {
return err return err
} }
writef(out, "to get started, enter:\n") if _, err = fmt.Fprintf(out, "to get started, enter:\n"); err != nil {
return writef(out, "\n\tipfs cat /ipfs/%s/readme\n\n", dkey) return err
}
_, err = fmt.Fprintf(out, "\n\tipfs cat /ipfs/%s/readme\n\n", dkey)
return err
} }
func initializeIpnsKeyspace(repoRoot string) error { func initializeIpnsKeyspace(repoRoot string) error {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论