-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathhelp.go
101 lines (85 loc) · 1.86 KB
/
help.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
93
94
95
96
97
98
99
100
101
package main
//go:generate sh -c "for md in doc/vaulted*.md; do md2man-roff ${DOLLAR}md > doc/man/${DOLLAR}(basename ${DOLLAR}md .md); done"
//go:generate go-bindata -nometadata -prefix doc/man/ -o man.go doc/man/
//go:generate gofmt -w man.go
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"github.com/miquella/vaulted/lib"
)
var (
ErrHelp = errors.New("help requested")
HelpAliases = map[string]string{
"add": "add",
"create": "add",
"new": "add",
"cp": "cp",
"copy": "cp",
"dump": "dump",
"edit": "edit",
"env": "env",
"exec": "exec",
"ls": "ls",
"list": "ls",
"load": "load",
"passwd": "passwd",
"password": "passwd",
"rm": "rm",
"delete": "rm",
"remove": "rm",
"shell": "shell",
"upgrade": "upgrade",
}
)
type Help struct {
Subcommand string
}
func (h *Help) Run(store vaulted.Store) error {
return displayHelp(h.Subcommand)
}
func displayHelp(subcommand string) error {
if subcommand != "" {
if HelpAliases[subcommand] == "" {
return fmt.Errorf("Unknown subcommand: %s", subcommand)
}
subcommand = HelpAliases[subcommand]
}
manpage := "vaulted.1"
if subcommand != "" {
manpage = fmt.Sprintf("vaulted-%s.1", subcommand)
}
content, err := Asset(manpage)
if err != nil {
return err
}
manpath, err := exec.LookPath("man")
if err != nil {
return err
}
dir, err := ioutil.TempDir("", "vaulted")
if err != nil {
return err
}
defer os.RemoveAll(dir)
man := path.Join(dir, manpage)
err = ioutil.WriteFile(man, content, 0644)
if err != nil {
return err
}
var attr os.ProcAttr
attr.Files = []*os.File{os.Stdin, os.Stdout, os.Stderr}
proc, err := os.StartProcess(manpath, []string{"man", man}, &attr)
if err != nil {
return err
}
state, _ := proc.Wait()
if !state.Success() {
os.Exit(255)
}
os.Exit(1)
return nil
}