Commit f7b6ee3c authored by chenminjian's avatar chenminjian

refact(cmd/repo): repo fsck and repo version uses new cmd lib

License: MIT
Signed-off-by: default avatarchenminjian <727180553@qq.com>
parent 3f7668bd
...@@ -42,8 +42,8 @@ var RepoCmd = &cmds.Command{ ...@@ -42,8 +42,8 @@ var RepoCmd = &cmds.Command{
Subcommands: map[string]*cmds.Command{ Subcommands: map[string]*cmds.Command{
"stat": repoStatCmd, "stat": repoStatCmd,
"gc": repoGcCmd, "gc": repoGcCmd,
"fsck": lgc.NewCommand(RepoFsckCmd), "fsck": RepoFsckCmd,
"version": lgc.NewCommand(repoVersionCmd), "version": repoVersionCmd,
"verify": lgc.NewCommand(repoVerifyCmd), "verify": lgc.NewCommand(repoVerifyCmd),
}, },
} }
...@@ -224,7 +224,7 @@ Version string The repo version. ...@@ -224,7 +224,7 @@ Version string The repo version.
}, },
} }
var RepoFsckCmd = &oldcmds.Command{ var RepoFsckCmd = &cmds.Command{
Helptext: cmdkit.HelpText{ Helptext: cmdkit.HelpText{
Tagline: "Remove repo lockfiles.", Tagline: "Remove repo lockfiles.",
ShortDescription: ` ShortDescription: `
...@@ -233,13 +233,15 @@ lockfiles, as well as the api file. This command can only run when no ipfs ...@@ -233,13 +233,15 @@ lockfiles, as well as the api file. This command can only run when no ipfs
daemons are running. daemons are running.
`, `,
}, },
Run: func(req oldcmds.Request, res oldcmds.Response) { Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
configRoot := req.InvocContext().ConfigRoot configRoot, err := cmdenv.GetConfigRoot(env)
if err != nil {
return err
}
dsPath, err := config.DataStorePath(configRoot) dsPath, err := config.DataStorePath(configRoot)
if err != nil { if err != nil {
res.SetError(err, cmdkit.ErrNormal) return err
return
} }
dsLockFile := filepath.Join(dsPath, "LOCK") // TODO: get this lockfile programmatically dsLockFile := filepath.Join(dsPath, "LOCK") // TODO: get this lockfile programmatically
...@@ -252,25 +254,25 @@ daemons are running. ...@@ -252,25 +254,25 @@ daemons are running.
err = os.Remove(repoLockFile) err = os.Remove(repoLockFile)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
res.SetError(err, cmdkit.ErrNormal) return err
return
} }
err = os.Remove(dsLockFile) err = os.Remove(dsLockFile)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
res.SetError(err, cmdkit.ErrNormal) return err
return
} }
err = os.Remove(apiFile) err = os.Remove(apiFile)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
res.SetError(err, cmdkit.ErrNormal) return err
return
} }
res.SetOutput(&MessageOutput{"Lockfiles have been removed.\n"}) return cmds.EmitOnce(res, &MessageOutput{"Lockfiles have been removed.\n"})
}, },
Type: MessageOutput{}, Type: MessageOutput{},
Marshalers: oldcmds.MarshalerMap{ Encoders: cmds.EncoderMap{
oldcmds.Text: MessageTextMarshaler, cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *MessageOutput) error {
fmt.Fprintf(w, out.Message)
return nil
}),
}, },
} }
...@@ -409,7 +411,7 @@ var repoVerifyCmd = &oldcmds.Command{ ...@@ -409,7 +411,7 @@ var repoVerifyCmd = &oldcmds.Command{
}, },
} }
var repoVersionCmd = &oldcmds.Command{ var repoVersionCmd = &cmds.Command{
Helptext: cmdkit.HelpText{ Helptext: cmdkit.HelpText{
Tagline: "Show the repo version.", Tagline: "Show the repo version.",
ShortDescription: ` ShortDescription: `
...@@ -420,36 +422,22 @@ var repoVersionCmd = &oldcmds.Command{ ...@@ -420,36 +422,22 @@ var repoVersionCmd = &oldcmds.Command{
Options: []cmdkit.Option{ Options: []cmdkit.Option{
cmdkit.BoolOption(repoQuietOptionName, "q", "Write minimal output."), cmdkit.BoolOption(repoQuietOptionName, "q", "Write minimal output."),
}, },
Run: func(req oldcmds.Request, res oldcmds.Response) { Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
res.SetOutput(&RepoVersion{ return cmds.EmitOnce(res, &RepoVersion{
Version: fmt.Sprint(fsrepo.RepoVersion), Version: fmt.Sprint(fsrepo.RepoVersion),
}) })
}, },
Type: RepoVersion{}, Type: RepoVersion{},
Marshalers: oldcmds.MarshalerMap{ Encoders: cmds.EncoderMap{
oldcmds.Text: func(res oldcmds.Response) (io.Reader, error) { cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *RepoVersion) error {
v, err := unwrapOutput(res.Output()) quiet, _ := req.Options[repoQuietOptionName].(bool)
if err != nil {
return nil, err
}
response, ok := v.(*RepoVersion)
if !ok {
return nil, e.TypeErr(response, v)
}
quiet, _, err := res.Request().Option("quiet").Bool()
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
if quiet { if quiet {
buf = bytes.NewBufferString(fmt.Sprintf("fs-repo@%s\n", response.Version)) fmt.Fprintf(w, fmt.Sprintf("fs-repo@%s\n", out.Version))
} else { } else {
buf = bytes.NewBufferString(fmt.Sprintf("ipfs repo version fs-repo@%s\n", response.Version)) fmt.Fprintf(w, fmt.Sprintf("ipfs repo version fs-repo@%s\n", out.Version))
} }
return buf, nil return nil
}),
},
}, },
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment