/
main.go
203 lines (162 loc) · 4.29 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/s3"
"gopkg.in/alecthomas/kingpin.v2"
"log"
"os"
"os/user"
"path/filepath"
"regexp"
"sort"
"strings"
)
type Manifests []string
func (m Manifests) Len() int {
return len(m)
}
func (m Manifests) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
func (m Manifests) Less(i, j int) bool {
return m[i] < m[j]
}
func Exists(name string) bool {
_, err := os.Stat(name)
return !os.IsNotExist(err)
}
func NotExists(name string) bool {
return !Exists(name)
}
func AbsPath(fname string) (f string, err error) {
var fpath string
matched, _ := regexp.Match("^~/", []byte(fname))
if matched {
usr, _ := user.Current()
fpath = strings.Replace(fname, "~", usr.HomeDir, 1)
} else {
fpath, err = filepath.Abs(fname)
}
return fpath, err
}
func GetAccessKey() (accessKey string) {
accessKey = os.Getenv("AWS_ACCESS_KEY")
if accessKey == "" {
accessKey = os.Getenv("AWS_ACCESS_KEY_ID")
}
return accessKey
}
func GetSecretKey() (secretKey string) {
secretKey = os.Getenv("AWS_SECRET_KEY")
if secretKey == "" {
secretKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
}
return secretKey
}
func CredentialFilePath(file string) (credentialFile string, err error) {
if file == "" {
credentialFile, err = AbsPath(credentials1)
} else {
credentialFile, err = AbsPath(file)
}
if file == "" && NotExists(credentialFile) {
credentialFile, err = AbsPath(credentials2)
}
return credentialFile, err
}
func AWSConfig(creds *credentials.Credentials, region string) (config aws.Config) {
config = aws.Config{
Credentials: creds,
}
if region != "" {
config.Region = aws.String(region)
}
return config
}
func S3ListObjectOutput(cli *s3.S3, bucket string) (listObjectOutput *s3.ListObjectsOutput, err error) {
listObjectOutput, err = cli.ListObjects(&s3.ListObjectsInput{
Bucket: aws.String(bucket),
})
return listObjectOutput, err
}
func GetManifests(listObjectOutput *s3.ListObjectsOutput) (manifests Manifests) {
re := regexp.MustCompile(".*.ya?ml$")
for _, c := range listObjectOutput.Contents {
if re.Match([]byte(*c.Key)) {
manifests = append(manifests, *c.Key)
}
}
return manifests
}
func OutputAll(manifests Manifests) {
for _, manifest := range manifests {
fmt.Println(manifest)
}
os.Exit(0)
}
func GetManifest(manifests Manifests, num int) (manifest string) {
if num > manifests.Len() || num < 0 {
log.Fatalf("Out of range (%d items)", manifests.Len())
}
return manifests[num]
}
func Nth(manifests Manifests, num int) {
fmt.Println(GetManifest(manifests, num-1))
os.Exit(0)
}
func Latest(manifests Manifests) {
fmt.Println(GetManifest(manifests, 0))
os.Exit(0)
}
func Oldest(manifests Manifests) {
fmt.Println(GetManifest(manifests, manifests.Len()-1))
os.Exit(0)
}
var (
all = kingpin.Flag("all", "All manifests").Short('a').Bool()
num = kingpin.Flag("num", "N-th Manifest").Short('n').Default("1").Int()
bucket = kingpin.Flag("bucket", "Bucket").Short('b').Required().String()
region = kingpin.Flag("region", "Region").Default("ap-northeast-1").String()
file = kingpin.Flag("file", "Credentials file(Default ~/.aws/credentials, ~/.aws/config)").Short('f').String()
profile = kingpin.Flag("profile", "Profile").Default("default").String()
oldest = kingpin.Flag("oldest", "The oldest manifest").Bool()
credentials1 = "~/.aws/credentials"
credentials2 = "~/.aws/config"
)
func main() {
kingpin.Version("0.0.1")
kingpin.Parse()
var manifests Manifests
var creds *credentials.Credentials
var config aws.Config
var credentialFile string
var err error
credentialFile, err = CredentialFilePath(*file)
if err != nil {
log.Fatal(err)
}
if Exists(credentialFile) {
creds = credentials.NewSharedCredentials(credentialFile, *profile)
} else {
accessKey := GetAccessKey()
secretKey := GetSecretKey()
creds = credentials.NewStaticCredentials(accessKey, secretKey, "")
}
config = AWSConfig(creds, *region)
cli := s3.New(&config)
listObjectOutput, err := S3ListObjectOutput(cli, *bucket)
if err != nil {
log.Fatal(err)
}
manifests = GetManifests(listObjectOutput)
sort.Sort(sort.Reverse(manifests))
if *all {
OutputAll(manifests)
}
if *oldest {
Oldest(manifests)
}
Nth(manifests, *num)
}