mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-02 20:32:58 +08:00
refactor(init) return an error
This commit is contained in:

committed by
Juan Batiz-Benet

parent
faf6454df6
commit
d894924152
@ -27,38 +27,39 @@ var initCmd = &cmds.Command{
|
|||||||
new keypair.
|
new keypair.
|
||||||
`,
|
`,
|
||||||
Run: func(res cmds.Response, req cmds.Request) {
|
Run: func(res cmds.Response, req cmds.Request) {
|
||||||
foo(res, req)
|
err := foo(res, req)
|
||||||
|
if err != nil {
|
||||||
|
res.SetError(err, cmds.ErrNormal)
|
||||||
|
return
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func foo(res cmds.Response, req cmds.Request) {
|
func foo(res cmds.Response, req cmds.Request) error {
|
||||||
ctx := req.Context()
|
ctx := req.Context()
|
||||||
|
|
||||||
u.POut("initializing ipfs node at %s\n", ctx.ConfigRoot)
|
u.POut("initializing ipfs node at %s\n", ctx.ConfigRoot)
|
||||||
filename, err := config.Filename(ctx.ConfigRoot)
|
filename, err := config.Filename(ctx.ConfigRoot)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(errors.New("Couldn't get home directory path"), cmds.ErrNormal)
|
return errors.New("Couldn't get home directory path")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
arg, found := req.Option("d")
|
arg, found := req.Option("d")
|
||||||
dspath, ok := arg.(string)
|
dspath, ok := arg.(string)
|
||||||
if found && !ok {
|
if found && !ok {
|
||||||
res.SetError(errors.New("failed to parse datastore flag"), cmds.ErrNormal)
|
return errors.New("failed to parse datastore flag")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fi, err := os.Lstat(filename)
|
fi, err := os.Lstat(filename)
|
||||||
arg, found = req.Option("f")
|
arg, found = req.Option("f")
|
||||||
force, ok := arg.(bool)
|
force, ok := arg.(bool)
|
||||||
if found && !ok {
|
if found && !ok {
|
||||||
res.SetError(errors.New("failed to parse force flag"), cmds.ErrNormal)
|
return errors.New("failed to parse force flag")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if fi != nil || (err != nil && !os.IsNotExist(err)) {
|
if fi != nil || (err != nil && !os.IsNotExist(err)) {
|
||||||
if !force {
|
if !force {
|
||||||
res.SetError(errors.New("ipfs configuration file already exists!\nReinitializing would overwrite your keys.\n(use -f to force overwrite)"), cmds.ErrNormal)
|
// TODO multi-line string
|
||||||
return
|
return errors.New("ipfs configuration file already exists!\nReinitializing would overwrite your keys.\n(use -f to force overwrite)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cfg := new(config.Config)
|
cfg := new(config.Config)
|
||||||
@ -67,8 +68,7 @@ func foo(res cmds.Response, req cmds.Request) {
|
|||||||
if len(dspath) == 0 {
|
if len(dspath) == 0 {
|
||||||
dspath, err = config.DataStorePath("")
|
dspath, err = config.DataStorePath("")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmds.ErrNormal)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cfg.Datastore.Path = dspath
|
cfg.Datastore.Path = dspath
|
||||||
@ -76,16 +76,14 @@ func foo(res cmds.Response, req cmds.Request) {
|
|||||||
|
|
||||||
// Construct the data store if missing
|
// Construct the data store if missing
|
||||||
if err := os.MkdirAll(dspath, os.ModePerm); err != nil {
|
if err := os.MkdirAll(dspath, os.ModePerm); err != nil {
|
||||||
res.SetError(err, cmds.ErrNormal)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the directory is writeable
|
// Check the directory is writeable
|
||||||
if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil {
|
if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil {
|
||||||
os.Remove(f.Name())
|
os.Remove(f.Name())
|
||||||
} else {
|
} else {
|
||||||
res.SetError(errors.New("Datastore '"+dspath+"' is not writeable"), cmds.ErrNormal)
|
return errors.New("Datastore '" + dspath + "' is not writeable")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg.Identity = config.Identity{}
|
cfg.Identity = config.Identity{}
|
||||||
@ -105,36 +103,31 @@ func foo(res cmds.Response, req cmds.Request) {
|
|||||||
arg, found = req.Option("b")
|
arg, found = req.Option("b")
|
||||||
nbits, ok := arg.(int)
|
nbits, ok := arg.(int)
|
||||||
if found && !ok {
|
if found && !ok {
|
||||||
res.SetError(errors.New("failed to get bits flag"), cmds.ErrNormal)
|
return errors.New("failed to get bits flag")
|
||||||
return
|
|
||||||
} else if !found {
|
} else if !found {
|
||||||
nbits = 4096
|
nbits = 4096
|
||||||
}
|
}
|
||||||
if nbits < 1024 {
|
if nbits < 1024 {
|
||||||
res.SetError(errors.New("Bitsize less than 1024 is considered unsafe."), cmds.ErrNormal)
|
return errors.New("Bitsize less than 1024 is considered unsafe.")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
u.POut("generating key pair\n")
|
u.POut("generating key pair\n")
|
||||||
sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
|
sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmds.ErrNormal)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// currently storing key unencrypted. in the future we need to encrypt it.
|
// currently storing key unencrypted. in the future we need to encrypt it.
|
||||||
// TODO(security)
|
// TODO(security)
|
||||||
skbytes, err := sk.Bytes()
|
skbytes, err := sk.Bytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmds.ErrNormal)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
cfg.Identity.PrivKey = base64.StdEncoding.EncodeToString(skbytes)
|
cfg.Identity.PrivKey = base64.StdEncoding.EncodeToString(skbytes)
|
||||||
|
|
||||||
id, err := peer.IDFromPubKey(pk)
|
id, err := peer.IDFromPubKey(pk)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmds.ErrNormal)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
cfg.Identity.PeerID = id.Pretty()
|
cfg.Identity.PeerID = id.Pretty()
|
||||||
|
|
||||||
@ -155,7 +148,7 @@ func foo(res cmds.Response, req cmds.Request) {
|
|||||||
|
|
||||||
err = config.WriteConfigFile(filename, cfg)
|
err = config.WriteConfigFile(filename, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmds.ErrNormal)
|
return err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user