Use man content for `help <cmd>' and '<cmd> --help'
Call 'go generate ./commands' to create string entries from .ronns which the cobra command can then use (added to the build scripts)
This commit is contained in:
parent
ccbc6f6069
commit
e970264f00
1
.gitignore
vendored
1
.gitignore
vendored
@ -23,3 +23,4 @@ repos
|
||||
docker/*.key
|
||||
|
||||
src
|
||||
commands/mancontent_gen.go
|
||||
|
@ -16,6 +16,9 @@ import (
|
||||
"github.com/github/git-lfs/vendor/_nuts/github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// Populate man pages
|
||||
//go:generate go run ../docs/man/mangen.go
|
||||
|
||||
var (
|
||||
Debugging = false
|
||||
ErrorBuffer = &bytes.Buffer{}
|
||||
@ -29,6 +32,7 @@ var (
|
||||
cmd.Usage()
|
||||
},
|
||||
}
|
||||
ManPages = make(map[string]string, 20)
|
||||
)
|
||||
|
||||
// Error prints a formatted message to Stderr. It also gets printed to the
|
||||
@ -218,6 +222,28 @@ func determineIncludeExcludePaths(includeArg, excludeArg string) (include, exclu
|
||||
return includePaths, excludePaths
|
||||
}
|
||||
|
||||
// help is used for 'git-lfs help <command>'
|
||||
func help(cmd *cobra.Command, args []string) {
|
||||
if txt, ok := ManPages[cmd.Use]; ok {
|
||||
fmt.Fprintf(os.Stderr, txt)
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Sorry, no help text found\n")
|
||||
}
|
||||
}
|
||||
|
||||
// usage is used for 'git-lfs <command> --help' or wen invoked manually
|
||||
func usage(cmd *cobra.Command) error {
|
||||
if txt, ok := ManPages[cmd.Use]; ok {
|
||||
fmt.Fprintf(os.Stderr, txt)
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "Sorry, no usage text found\n")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
log.SetOutput(ErrorWriter)
|
||||
// Set up help/usage funcs based on manpage text
|
||||
RootCmd.SetHelpFunc(help)
|
||||
RootCmd.SetUsageFunc(usage)
|
||||
}
|
||||
|
67
docs/man/mangen.go
Normal file
67
docs/man/mangen.go
Normal file
@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Reads all .ronn files & and converts them to string literals
|
||||
// triggered by "go generate" comment
|
||||
// Literals are inserted into a map using an init function, this means
|
||||
// that there are no compilation errors if 'go generate' hasn't been run, just
|
||||
// blank man files.
|
||||
func main() {
|
||||
fmt.Fprintf(os.Stderr, "Converting man pages into code...\n")
|
||||
mandir := "../docs/man"
|
||||
fs, err := ioutil.ReadDir(mandir)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to open man dir: %v\n", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
out, err := os.Create("../commands/mancontent_gen.go")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to create go file: %v\n", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
out.WriteString("package commands\n\nfunc init() {\n")
|
||||
out.WriteString("// THIS FILE IS GENERATED, DO NOT EDIT\n")
|
||||
out.WriteString("// Use 'go generate ./commands' to update\n")
|
||||
r := regexp.MustCompile(`git-lfs(?:-([A-Za-z\-]+))?.\d.ronn`)
|
||||
count := 0
|
||||
for _, f := range fs {
|
||||
if match := r.FindStringSubmatch(f.Name()); match != nil {
|
||||
fmt.Fprintf(os.Stderr, "%v\n", f.Name())
|
||||
cmd := match[1]
|
||||
if len(cmd) == 0 {
|
||||
// This is git-lfs.1.ronn
|
||||
cmd = "git-lfs"
|
||||
}
|
||||
out.WriteString("ManPages[\"" + cmd + "\"] = `")
|
||||
contentf, err := os.Open(filepath.Join(mandir, f.Name()))
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to open %v: %v\n", f.Name(), err)
|
||||
os.Exit(2)
|
||||
}
|
||||
// Process the ronn to make it nicer as help text
|
||||
scanner := bufio.NewScanner(contentf)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
// Remove backticks since it won't format & that's delimiting the string
|
||||
line = strings.Replace(line, "`", "", -1)
|
||||
// Maybe more e.g. ## ?
|
||||
out.WriteString(line + "\n")
|
||||
}
|
||||
out.WriteString("`\n")
|
||||
contentf.Close()
|
||||
count++
|
||||
}
|
||||
}
|
||||
out.WriteString("}\n")
|
||||
fmt.Fprintf(os.Stderr, "Successfully processed %d man pages.\n", count)
|
||||
|
||||
}
|
@ -38,6 +38,11 @@ func mainBuild() {
|
||||
|
||||
fmt.Printf("Using %s\n", runtime.Version())
|
||||
|
||||
genOut, err := exec.Command("go", "generate", "./commands").CombinedOutput()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "go generate failed:\n%v", genOut)
|
||||
os.Exit(1)
|
||||
}
|
||||
cmd, _ := exec.Command("git", "rev-parse", "--short", "HEAD").Output()
|
||||
|
||||
if len(cmd) > 0 {
|
||||
|
Loading…
Reference in New Issue
Block a user