-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
83 lines (68 loc) · 1.86 KB
/
main.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
// (C) 2017 Christoph Berger <[email protected]>. Some rights reserved.
// Distributed under a 3-clause BSD license; see LICENSE.txt.
// goman - the missing man pages for Go binaries
//
// goman replaces missing man pages for Go binaries by locating the README file of the corresponding source code project and rendering this file as plain text with ANSI colors, to be viewed in a terminal, optionally through less -R.
//
// Usage:
//
// goman <go binary file>
//
// or
//
// goman <go binary file> | less -R
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"runtime/debug"
"syscall"
)
func usage() {
fmt.Print(`Usage:
goman <name of Go binary>
goman is man for Go binaries. It attempts to fetch the README file of a Go binary's project and displays it in the terminal, if found.
`)
flag.Usage()
b, _ := debug.ReadBuildInfo()
fmt.Printf("\nModule info: %s %s\n", b.Main.Path, b.Main.Version)
}
var (
remoteOnly *bool
verbose *bool
)
func main() {
log.SetFlags(0)
verbose = flag.Bool("v", false, "Verbose error output")
remoteOnly = flag.Bool("r", false, "Skip local search (as the local file may be outdated)")
flag.Parse()
if len(flag.Args()) != 1 {
usage()
return
}
switch flag.Args()[0] {
// easier than defining four flags and checking for the string "help" also:
case "-h", "-help", "--help", "-?", "help":
readme, _, err := findReadme("github.com/appliedgocode/goman", "")
if err != nil {
usage()
return
}
fmt.Println(string(mdToAnsi(readme)))
return
}
exec := flag.Args()[0]
go exitOnSignal()
run(exec)
}
// In case goman gets stuck somewhere. This should not happen under normal circumstances.
// There are no resources to be cleaned up, so we just exit.
func exitOnSignal() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
sig := <-c
log.Fatalf("received signal %s", sig)
}