-
Notifications
You must be signed in to change notification settings - Fork 57
/
man.go
92 lines (80 loc) · 2.57 KB
/
man.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"io/ioutil"
"strings"
"time"
"github.com/rackspace/rack/commandoptions"
"github.com/rackspace/rack/internal/github.com/codegangsta/cli"
"github.com/rackspace/rack/util"
)
func man() {
content := fmt.Sprintln(`.\" Manpage for rack`)
content += fmt.Sprintln(`.\" Contact [email protected] to correct errors or typos`)
content += fmt.Sprintf(`.TH man 1 "%s" "%s" "rack man page"`+"\n", time.Now().Format("06 May 2010"), util.Version)
content += fmt.Sprintln(`.SH NAME`)
content += fmt.Sprintf(`rack \- %s`+"\n", Usage())
content += fmt.Sprintln(`.SH DESCRIPTION`)
content += fmt.Sprintf("%s\n\n\n", Desc())
content += fmt.Sprintln(`.SH SYNOPSIS`)
content += fmt.Sprintln("rack <command> <subcommand> <action> [OPTIONS]")
content += fmt.Sprintln("The following global options are available:")
for _, flag := range commandoptions.GlobalFlags() {
content += fmt.Sprintln(".TP")
name, usage := parseFlag(flag)
if name != "" && usage != "" {
content += fmt.Sprintf(`\fB\-\-%s\fR`+"\n", strings.Replace(name, "-", `\-`, -1))
content += fmt.Sprintln(usage)
} else {
content += fmt.Sprintln(flag.String())
}
}
content += fmt.Sprintln(`.SH TOP-LEVEL COMMANDS`)
for _, cmd := range Cmds() {
if len(cmd.Subcommands) > 0 {
continue
}
content += fmt.Sprintln(".TP")
content += fmt.Sprintf(`\fB%s\fR`+"\n", cmd.Name)
content += fmt.Sprintln(cmd.Usage)
}
for _, cmd := range Cmds() {
if len(cmd.Subcommands) == 0 {
continue
}
content += fmt.Sprintf(`.SH %s COMMANDS`+"\n", strings.ToUpper(cmd.Name))
content += fmt.Sprintln(cmd.Usage)
for _, serviceCmd := range cmd.Subcommands {
name := strings.ToUpper(serviceCmd.Name)
content += fmt.Sprintf(`.SS "\s-1%s COMMANDS\s0"`+"\n", name)
content += fmt.Sprintf(`.IX Subsection "%s"`+"\n", name)
for _, resourceCmd := range serviceCmd.Subcommands {
content += fmt.Sprintf(`.IP "\fB%s\fR"`+"\n", resourceCmd.Usage)
content += fmt.Sprintf(`.IX Item "%s"`+"\n", resourceCmd.Usage)
content += fmt.Sprintln(resourceCmd.Description)
}
}
}
content += fmt.Sprintln(".SH BUGS")
content += fmt.Sprintln("See https://github.com/rackspace/rack/issues")
ioutil.WriteFile("rack.1", []byte(content), 0755)
}
func parseFlag(f cli.Flag) (string, string) {
name, usage := "", ""
if f, ok := f.(cli.StringFlag); ok {
name = f.Name
usage = f.Usage
}
if f, ok := f.(cli.BoolFlag); ok {
name = f.Name
usage = f.Usage
}
if f, ok := f.(cli.IntFlag); ok {
name = f.Name
usage = f.Usage
}
return name, usage
}
func flagUsage(f cli.Flag) string {
return ""
}