-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (85 loc) · 2.19 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
package main
import (
"context"
"log"
"os"
"sort"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/carmeloriolo/ec2ti/internal/client"
"github.com/carmeloriolo/ec2ti/internal/components"
"github.com/carmeloriolo/ec2ti/internal/ui"
"github.com/urfave/cli/v2"
)
const (
appName = "Ec2Ti"
appDescription = "The terminal user interface to connect to your AWS EC2 instances easily"
flagAwsRegion = "AWS_DEFAULT_REGION"
flagAwsEndpoint = "AWS_EC2_CUSTOM_ENDPOINT"
)
var (
AppVersion = ""
appFlags = []cli.Flag{
&cli.StringFlag{
Name: flagAwsEndpoint,
EnvVars: []string{"AWS_EC2_CUSTOM_ENDPOINT"},
},
&cli.StringFlag{
Name: flagAwsRegion,
EnvVars: []string{"AWS_DEFAULT_REGION"},
Required: true,
},
}
)
func main() {
app := &cli.App{
Name: appName,
Usage: appDescription,
Flags: appFlags,
Version: AppVersion,
Action: func(c *cli.Context) error {
cfg, err := config.LoadDefaultConfig(
context.Background(),
config.WithRegion(c.String(flagAwsRegion)),
config.WithEndpointResolver(
aws.EndpointResolverFunc(
func(service, region string) (aws.Endpoint, error) {
if c.String(flagAwsEndpoint) != "" {
return aws.Endpoint{
PartitionID: "aws",
URL: c.String(flagAwsEndpoint),
SigningRegion: c.String(flagAwsRegion),
}, nil
}
return aws.Endpoint{}, &aws.EndpointNotFoundError{} // default fallback
},
),
),
)
if err != nil {
log.Fatal(err)
}
instances, err := client.NewEc2Client(cfg).GetInstances()
if err != nil {
log.Fatal(err)
}
sort.Slice(instances, func(i, j int) bool {
return instances[i].Name < instances[j].Name
})
userIdentity, err := client.NewSts(cfg).GetCallerIdentity()
if err != nil {
log.Fatal(err)
}
u := ui.NewUi().SetTitle(appName).SetHeader(&components.InfoHeader{
UserIdentity: *userIdentity,
Region: c.String(flagAwsRegion),
})
u = u.SetTable(components.NewInstanceTable(instances, len(instances)))
return u.SetHandlers(ui.DefaultHandlers).Run()
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}