-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflag.go
82 lines (67 loc) · 1.67 KB
/
flag.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
package main
import (
"flag"
"os"
"strconv"
)
type Flags struct {
ImagesFile string
AWSAccount string
AWSRegion string
DryRun bool
Version bool
Args []string
}
func ParseFlags() (Flags, error) {
f := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
imagesFile := f.String("images-file", getStringEnv("ECR_SYNC_IMAGES_FILE", "images-list"),
"file containing list of images to sync")
awsAccount := f.String("aws-account", getStringEnv("ECR_SYNC_AWS_ACCOUNT", ""),
"AWS account where to sync images to")
awsRegion := f.String("aws-region", getStringEnv("ECR_SYNC_AWS_REGION", ""),
"AWS region where to sync images to")
dryRun := f.Bool("dry-run", getBoolEnv("ECR_SYNC_DRY_RUN", false),
"whether to do sync - tag and push image")
version := f.Bool("version", getBoolEnv("ECR_SYNC_VERSION", false),
"ecr-image-sync version")
if err := f.Parse(os.Args[1:]); err != nil {
return Flags{}, err
}
return Flags{
ImagesFile: stringValue(imagesFile),
AWSAccount: stringValue(awsAccount),
AWSRegion: stringValue(awsRegion),
DryRun: boolValue(dryRun),
Version: boolValue(version),
Args: f.Args(),
}, nil
}
func getStringEnv(envName string, defaultValue string) string {
env, ok := os.LookupEnv(envName)
if !ok {
return defaultValue
}
return env
}
func stringValue(v *string) string {
if v == nil {
return ""
}
return *v
}
func getBoolEnv(envName string, defaultValue bool) bool {
env, ok := os.LookupEnv(envName)
if !ok {
return defaultValue
}
if intValue, err := strconv.ParseBool(env); err == nil {
return intValue
}
return defaultValue
}
func boolValue(v *bool) bool {
if v == nil {
return false
}
return *v
}