-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
61 lines (54 loc) · 1.27 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
package main
import (
"os"
"github.com/aws/aws-sdk-go/service/ec2"
"gopkg.in/urfave/cli.v1"
)
// BaseFlags base on commands
var BaseFlags = []cli.Flag{
cli.StringFlag{
Name: "region, r",
Usage: "The region to use. Overrides config/env settings.",
},
cli.StringFlag{
Name: "profile, p",
Usage: "Use a specific profile from your credential file.",
},
}
// Commands lists
var Commands = []cli.Command{
commandInit,
}
var flagsInit = append(BaseFlags, cli.StringFlag{
Name: "filters, f",
Usage: "Filtering ec2 tag",
})
var commandInit = cli.Command{
Name: "list",
Usage: "List EC2 instances.",
Action: func(c *cli.Context) error {
sess, err := GenerateSession(c.String("region"), c.String("profile"))
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
ec2Client := NewEC2Client(ec2.New(sess))
instances, err := ec2Client.ListInstances(c.String("filters"))
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
PrintInstances(instances)
return nil
},
Flags: flagsInit,
}
func main() {
app := cli.NewApp()
app.Name = "ec2-toys"
app.Usage = "Useful cli to operation Amazon EC2."
app.Author = "Shuichi Ohsawa"
app.Email = "ohsawa0515@gmail.com"
app.Version = Version
app.Flags = BaseFlags
app.Commands = Commands
app.Run(os.Args)
}