-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
113 lines (98 loc) · 1.69 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"io"
"log"
"os"
"runtime/debug"
"github.com/urfave/cli/v2"
)
var (
all bool
verbose bool
version = ""
revision = ""
)
func main() {
app := &cli.App{
Name: "ec2id",
Usage: "get instance id",
Flags: []cli.Flag{
// &cli.BoolFlag{
// Name: "help",
// Destination: &help,
// },
&cli.BoolFlag{
Name: "all",
Destination: &all,
},
&cli.BoolFlag{
Name: "verbose",
Destination: &verbose,
},
},
Action: func(ctx *cli.Context) error {
name := ctx.Args().Get(0)
client, err := NewAwsClient()
if err != nil {
fmt.Fprintf(os.Stderr, "initialized failed:%v\n", err)
os.Exit(1)
}
ids, err := Ec2id(name, client)
if err == nil && ids != nil {
printIds(os.Stdout, ids, all)
}
return err
},
HideHelpCommand: true,
Version: versionFormatter(getVersion(), getRevision()),
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
os.Exit(0)
}
func versionFormatter(version string, revision string) string {
if version == "" {
version = "devel"
}
if revision == "" {
return version
}
return fmt.Sprintf("%s (rev: %s)", version, revision)
}
func getVersion() string {
if version != "" {
return version
}
i, ok := debug.ReadBuildInfo()
if !ok {
return ""
}
return i.Main.Version
}
func getRevision() string {
if revision != "" {
return revision
}
i, ok := debug.ReadBuildInfo()
if !ok {
return ""
}
for _, s := range i.Settings {
if s.Key == "vcs.revision" {
return s.Value
}
}
return ""
}
func printIds(out io.Writer, ids []string, all bool) {
for _, id := range ids {
fmt.Fprintln(out, id)
if !all {
break
}
}
}