Add minio cli changes and add CustomAppHelpTemplate

This commit is contained in:
Harshavardhana 2015-04-28 21:16:10 -07:00
parent a3d03184ca
commit 76701187e3
5 changed files with 60 additions and 58 deletions

4
Godeps/Godeps.json generated
View File

@ -27,8 +27,8 @@
},
{
"ImportPath": "github.com/minio-io/cli",
"Comment": "1.2.0-106-g74f4efd",
"Rev": "74f4efdae47555906336b1dcd30c4b40d4d0d6fa"
"Comment": "1.2.0-108-g4ad376c",
"Rev": "4ad376c97a51a452e36aaa4c19e42560e64be836"
},
{
"ImportPath": "github.com/stretchr/objx",

View File

@ -6,7 +6,6 @@ import (
"os"
"os/exec"
"strings"
"time"
"io/ioutil"
"text/tabwriter"
@ -45,7 +44,7 @@ type App struct {
// Execute this function if the proper command cannot be found
CommandNotFound func(context *Context, command string)
// Compilation date
Compiled time.Time
Compiled string
// ExtraInfo pass additional info as a key value map
ExtraInfo map[string]string
// List of all authors who contributed
@ -56,20 +55,24 @@ type App struct {
Email string
// Writer writer to write output to
Writer io.Writer
// CustomAppHelpTemplate the text template for app help topic.
// cli.go uses text/template to render templates. You can
// render custom help text by setting this variable.
CustomAppHelpTemplate string
}
// mustCompileTime - determines the modification time of the current binary
func mustCompileTime() time.Time {
func mustCompileTime() string {
path, err := exec.LookPath(os.Args[0])
if err != nil {
return time.Time{}
return ""
}
info, err := os.Stat(path)
if err != nil {
return time.Time{}
return ""
}
return info.ModTime()
return info.ModTime().String()
}
// NewApp - Creates a new cli Application with some reasonable defaults for Name, Usage, Version and Action.

View File

@ -98,41 +98,6 @@ func ExampleAppHelp() {
// This is how we describe describeit the function
}
func ExampleAppBashComplete() {
// set args for examples sake
os.Args = []string{"greet", "--generate-bash-completion"}
app := cli.NewApp()
app.Name = "greet"
app.EnableBashCompletion = true
app.Commands = []cli.Command{
{
Name: "describeit",
Aliases: []string{"d"},
Usage: "use it to see a description",
Description: "This is how we describe describeit the function",
Action: func(c *cli.Context) {
fmt.Printf("i like to describe things")
},
}, {
Name: "next",
Usage: "next example",
Description: "more stuff to see when generating bash completion",
Action: func(c *cli.Context) {
fmt.Printf("the next example")
},
},
}
app.Run(os.Args)
// Output:
// describeit
// d
// next
// help
// h
}
func TestApp_Run(t *testing.T) {
s := ""

View File

@ -8,27 +8,25 @@ import (
// The text template for the Default help topic.
// cli.go uses text/template to render templates. You can
// render custom help text by setting this variable.
var AppHelpTemplate = `NAME:
var DefaultAppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.Name}} {{if .Flags}}[global flags] {{end}}command{{if .Flags}} [command flags]{{end}} [arguments...]
VERSION:
{{.Version}}
BUILD:
{{.Compiled}}
{{range $key, $value := .ExtraInfo}}
{{ $key }}:
{{ $value }}
{{ end }}
COMMANDS:
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
{{end}}{{if .Flags}}
GLOBAL FLAGS:
{{range .Flags}}{{.}}
{{end}}{{end}}
VERSION:
{{.Version}}
{{if .Compiled}}
BUILD:
{{.Compiled}}{{end}}
{{range $key, $value := .ExtraInfo}}
{{$value}}{{end}}
`
// The text template for the command help topic.
@ -120,7 +118,11 @@ func ShowAppHelp(c *Context) {
app.Commands = append(app.Commands, command)
}
}
HelpPrinter(AppHelpTemplate, app)
if app.CustomAppHelpTemplate != "" {
HelpPrinter(app.CustomAppHelpTemplate, app)
} else {
HelpPrinter(DefaultAppHelpTemplate, app)
}
}
// DefaultAppComplete - Prints the list of subcommands as the default app completion method

40
main.go
View File

@ -213,6 +213,18 @@ func getWebServerConfigFunc(c *cli.Context) server.StartServerFunc {
return webDrivers.GetStartServerFunc()
}
// Build date
var BuildDate string
// getBuildDate -
func getBuildDate() string {
if BuildDate == "" {
return ""
}
t, _ := time.Parse(time.RFC3339Nano, BuildDate)
return t.String()
}
// Tries to get os/arch/platform specific information
// Returns a map of current os/arch/platform/memstats
func getSystemData() map[string]string {
@ -242,9 +254,6 @@ func getSystemData() map[string]string {
// Version is based on MD5SUM of its binary
var Version = mustHashBinarySelf()
// BuildDate - build time
var BuildDate string
func main() {
// set up iodine
iodine.SetGlobalState("minio.version", Version)
@ -254,7 +263,7 @@ func main() {
app := cli.NewApp()
app.Name = "minio"
app.Version = Version
app.Compiled, _ = time.Parse(time.RFC3339Nano, BuildDate)
app.Compiled = getBuildDate()
app.Author = "Minio.io"
app.Usage = "Minimalist Object Storage"
app.Flags = flags
@ -265,5 +274,28 @@ func main() {
}
return nil
}
app.CustomAppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.Name}} {{if .Flags}}[global flags] {{end}}command{{if .Flags}} [command flags]{{end}} [arguments...]
COMMANDS:
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
{{end}}{{if .Flags}}
GLOBAL FLAGS:
{{range .Flags}}{{.}}
{{end}}{{end}}
VERSION:
{{.Version}}
{{if .Compiled}}
BUILD:
{{.Compiled}}{{end}}
{{range $key, $value := .ExtraInfo}}
{{$key}}:
{{$value}}
{{end}}
`
app.RunAndExitOnError()
}