-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetOpts.go
62 lines (57 loc) · 1.73 KB
/
getOpts.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
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"runtime/debug"
flags "github.com/jessevdk/go-flags"
)
// ref. https://pkg.go.dev/github.com/jessevdk/go-flags
type Options struct {
Help bool `short:"h" long:"help" description:"print help and exit."`
Version bool `short:"v" long:"version" description:"print version and exit."`
VersionAll bool `short:"V" long:"version-all" hidden:"yes"`
PublicKey string `short:"u" long:"public" value-name:"<target>" description:"github account, url or file."`
PrivateKey string `short:"k" long:"private" value-name:"<id_file>" description:"ssh private key file."`
InputPath string `short:"i" long:"input" value-name:"<input_file>" description:"the path of the file to read. default: stdin"`
OutputPath string `short:"o" long:"output" value-name:"<output_file>" description:"the path of the file to write. default: stdout"`
Decrypt bool `short:"d" long:"decrypt" description:"decryption mode."`
}
func getOpts() Options {
var opts Options
parser := flags.NewParser(&opts, flags.PassDoubleDash)
parser.Usage = "[options]"
_, err := parser.Parse()
if opts.Version {
// e.g "git-caesar 1.0.0 Linux/amd64 (go1.20.4)"
fmt.Printf("%s %s %s/%s (%s)\n",
filepath.Base(os.Args[0]),
getVersion(),
runtime.GOOS,
runtime.GOARCH,
runtime.Version())
os.Exit(0)
}
if opts.VersionAll {
info, ok := debug.ReadBuildInfo()
if !ok {
fmt.Println("build info not found.")
os.Exit(1)
}
fmt.Printf("%s %s\n", info.Main.Path, info.Main.Version)
for _, m := range info.Deps {
fmt.Printf("%s %s\n", m.Path, m.Version)
}
os.Exit(0)
}
if opts.Help {
parser.WriteHelp(os.Stderr)
os.Exit(0)
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return opts
}