This repository has been archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
113 lines (92 loc) · 2.63 KB
/
utils.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
/******************************************************************************
Cloud Resource Counter
File: utils.go
Summary: Various utility functions
******************************************************************************/
package main
import (
"os"
"reflect"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/ec2"
)
// OpenFileForWriting does stuff...
func OpenFileForWriting(fileName string, typeOfFile string, am ActivityMonitor, append bool) *os.File {
// What is our flag for the file?
var flag int
if append {
flag = os.O_WRONLY | os.O_APPEND
} else {
flag = os.O_WRONLY | os.O_CREATE | os.O_TRUNC
}
// Can we open it for writing?
file, err := os.OpenFile(fileName, flag, 0666)
// Check for error
am.CheckError(err)
return file
}
// GetEC2Regions determines the set of regions associated with the account.
func GetEC2Regions(ec2is *EC2InstanceService, am ActivityMonitor) []string {
// Construct the input
input := &ec2.DescribeRegionsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("opt-in-status"),
Values: []*string{
aws.String("opt-in-not-required"),
aws.String("opted-in"),
},
},
},
}
// Execute the command
result, err := ec2is.GetRegions(input)
// Do we have an error?
if am.CheckError(err) {
return nil
}
// Transform the array of results into an array of region names...
var regionNames []string
for _, regionInfo := range result.Regions {
regionNames = append(regionNames, *regionInfo.RegionName)
}
return regionNames
}
// IsValidRegionName returns whether the supplied region name is valid or not.
func IsValidRegionName(regionName string) bool {
// Get the AWS Partition
awsPartition := endpoints.AwsPartition()
// Loop through the region names...
for id := range awsPartition.Regions() {
// Does it match?
if id == regionName {
return true
}
}
return false
}
// Map applies a function to each element of a string array
// Borrowed from: https://gobyexample.com/collection-functions
func Map(vs []string, f func(string) string) []string {
vsm := make([]string, len(vs))
for i, v := range vs {
vsm[i] = f(v)
}
return vsm
}
// NilInterface checks whether the supplied interface is nil or not
func NilInterface(intf interface{}) bool {
return intf == nil || reflect.ValueOf(intf).IsNil()
}
// FileExists checks if a file exists and is not a directory before we
// try using it to prevent further errors.
func FileExists(fileName string) bool {
// Stat the file
info, err := os.Stat(fileName)
// Check for non-existent file
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}