2016-06-03 09:09:14 +08:00
|
|
|
package command
|
2012-08-07 07:54:53 +08:00
|
|
|
|
|
|
|
import (
|
2013-01-17 16:56:56 +08:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2012-08-07 07:54:53 +08:00
|
|
|
)
|
|
|
|
|
2016-06-03 09:09:14 +08:00
|
|
|
var Commands = []*Command{
|
|
|
|
cmdBenchmark,
|
|
|
|
cmdBackup,
|
|
|
|
cmdCompact,
|
|
|
|
cmdFix,
|
|
|
|
cmdServer,
|
|
|
|
cmdMaster,
|
|
|
|
cmdFiler,
|
|
|
|
cmdUpload,
|
|
|
|
cmdDownload,
|
|
|
|
cmdShell,
|
|
|
|
cmdVersion,
|
|
|
|
cmdVolume,
|
|
|
|
cmdExport,
|
|
|
|
cmdMount,
|
|
|
|
}
|
|
|
|
|
2012-08-07 07:54:53 +08:00
|
|
|
type Command struct {
|
2013-01-17 16:56:56 +08:00
|
|
|
// Run runs the command.
|
|
|
|
// The args are the arguments after the command name.
|
|
|
|
Run func(cmd *Command, args []string) bool
|
2012-08-07 07:54:53 +08:00
|
|
|
|
2013-01-17 16:56:56 +08:00
|
|
|
// UsageLine is the one-line usage message.
|
|
|
|
// The first word in the line is taken to be the command name.
|
|
|
|
UsageLine string
|
2012-08-07 07:54:53 +08:00
|
|
|
|
2013-01-17 16:56:56 +08:00
|
|
|
// Short is the short description shown in the 'go help' output.
|
|
|
|
Short string
|
2012-08-07 07:54:53 +08:00
|
|
|
|
2013-01-17 16:56:56 +08:00
|
|
|
// Long is the long message shown in the 'go help <this-command>' output.
|
|
|
|
Long string
|
2012-08-07 07:54:53 +08:00
|
|
|
|
2013-01-17 16:56:56 +08:00
|
|
|
// Flag is a set of flags specific to this command.
|
|
|
|
Flag flag.FlagSet
|
2013-01-20 11:49:57 +08:00
|
|
|
|
|
|
|
IsDebug *bool
|
2012-08-07 07:54:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the command's name: the first word in the usage line.
|
|
|
|
func (c *Command) Name() string {
|
2013-01-17 16:56:56 +08:00
|
|
|
name := c.UsageLine
|
|
|
|
i := strings.Index(name, " ")
|
|
|
|
if i >= 0 {
|
|
|
|
name = name[:i]
|
|
|
|
}
|
|
|
|
return name
|
2012-08-07 07:54:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) Usage() {
|
2013-01-17 16:56:56 +08:00
|
|
|
fmt.Fprintf(os.Stderr, "Example: weed %s\n", c.UsageLine)
|
|
|
|
fmt.Fprintf(os.Stderr, "Default Usage:\n")
|
|
|
|
c.Flag.PrintDefaults()
|
|
|
|
fmt.Fprintf(os.Stderr, "Description:\n")
|
|
|
|
fmt.Fprintf(os.Stderr, " %s\n", strings.TrimSpace(c.Long))
|
|
|
|
os.Exit(2)
|
2012-08-07 07:54:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Runnable reports whether the command can be run; otherwise
|
|
|
|
// it is a documentation pseudo-command such as importpath.
|
|
|
|
func (c *Command) Runnable() bool {
|
2013-01-17 16:56:56 +08:00
|
|
|
return c.Run != nil
|
2012-08-07 07:54:53 +08:00
|
|
|
}
|