forked from imjerrybao/apex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.go
73 lines (58 loc) · 1.28 KB
/
docs.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
// Package docs outputs Wiki documentation.
package docs
import (
"io"
"os"
"os/exec"
"strings"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
"github.com/apex/apex/cmd/apex/root"
doc "github.com/apex/apex/docs"
)
// topic name.
var topic string
// example output.
const example = ` Output documentation topics
$ apex docs
Output documentation for a topic
$ apex docs project.json`
// Command config.
var Command = &cobra.Command{
Use: "docs [<topic>]",
Short: "Output documentation",
Example: example,
PersistentPreRun: root.PreRunNoop,
PreRun: preRun,
RunE: run,
}
// Initialize.
func init() {
root.Register(Command)
}
// PreRun joins args to form the topic.
func preRun(c *cobra.Command, args []string) {
if len(args) >= 1 {
topic = strings.Join(args, " ")
}
}
// Run command.
func run(c *cobra.Command, args []string) (err error) {
var w io.WriteCloser = os.Stdout
if isatty.IsTerminal(os.Stdout.Fd()) {
cmd := exec.Command("less", "-R")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
defer cmd.Wait()
w, err = cmd.StdinPipe()
if err != nil {
return err
}
defer w.Close()
if err := cmd.Start(); err != nil {
return err
}
}
_, err = io.Copy(w, doc.Reader())
return err
}