/
root.go
127 lines (110 loc) 路 4.24 KB
/
root.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
// Copyright (c) karl-cardenas-coding
// SPDX-License-Identifier: MIT
package cmd
import (
"crypto/tls"
"fmt"
"net/http"
"os"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
// VersionString is the version of the CLI
VersionString string = "0.0.0"
// ProfileFlag is the AWS crendentials profile passed in
ProfileFlag string
// CredentialsFile is a boolean for the credentials provider logic
CredentialsFile bool
// RegionFlag is the AWS Region to target for the execution
RegionFlag string
// Retain is the number of versions to retain excluding $LATEST
Retain int8
// Verbose is to enable debug output
Verbose bool
// DryRun is to enable a preview of what an actual execution would do
DryRun bool
// LambdaListFile points a file that contains a listof Lambdas to delete
LambdaListFile string
// MoreLambdaDetails is to show information about the Lambda being worked on
MoreLambdaDetails bool
// SizeIEC is used to display the size in IEC units
SizeIEC bool
// CliConfig is the struct that holds the CLI configuration
GlobalCliConfig cliConfig
// HTTPClient is the HTTP client to use for the AWS API calls
GlobalHTTPClient *http.Client
// UserAgent is the value to use for the User-Agent header
UserAgent string
// SkipAliases indicates that lambda versions attached to an alias should be skipped from deletion
SkipAliases bool
)
const (
// IssueMSG is a default message to pass to the user
IssueMSG = " Please open up a Github issue to report this error! https://github.com/karl-cardenas-coding/go-clean-lambda"
)
var rootCmd = &cobra.Command{
Use: "glc",
Short: "A CLI tool for cleaning up AWS Lambda versions",
Long: `A CLI tool for cleaning up AWS Lambda versions`,
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Help()
if err != nil {
log.Info("Error executing the CLI!")
return err
}
return err
},
}
func init() {
rootCmd.PersistentFlags().StringVarP(&RegionFlag, "region", "r", "", "Specify the desired AWS region to target.")
rootCmd.PersistentFlags().StringVarP(&ProfileFlag, "profile", "p", "", "Specify the AWS profile to leverage for authentication.")
rootCmd.PersistentFlags().StringVarP(&LambdaListFile, "listFile", "l", "", "Specify a file containing Lambdas to delete.")
rootCmd.PersistentFlags().BoolVarP(&MoreLambdaDetails, "moreLambdaDetails", "m", false, "Set to true to show Lambda names and count of versions to be removed (bool)")
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "Set to true to enable debugging (bool)")
rootCmd.PersistentFlags().BoolVarP(&DryRun, "dryrun", "d", false, "Executes a dry run (bool)")
rootCmd.PersistentFlags().BoolVarP(&SizeIEC, "size-iec", "i", false, "Displays file sizes in IEC units (bool)")
cleanCmd.Flags().Int8VarP(&Retain, "count", "c", 1, "The number of versions to retain from $LATEST-(n)")
cleanCmd.Flags().BoolVarP(&SkipAliases, "skip-aliases", "s", false, "Skip trying to delete versions with aliases attached")
GlobalCliConfig.RegionFlag = &RegionFlag
GlobalCliConfig.ProfileFlag = &ProfileFlag
GlobalCliConfig.LambdaListFile = &LambdaListFile
GlobalCliConfig.MoreLambdaDetails = &MoreLambdaDetails
GlobalCliConfig.Verbose = &Verbose
GlobalCliConfig.DryRun = &DryRun
GlobalCliConfig.SizeIEC = &SizeIEC
GlobalCliConfig.Retain = &Retain
GlobalCliConfig.SkipAliases = &SkipAliases
UserAgent = fmt.Sprintf("go-clean-lambda/%s", VersionString)
// Establish logging default
log.SetFormatter(&log.TextFormatter{
DisableColors: false,
TimestampFormat: "01/02/06",
FullTimestamp: true,
})
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel)
GlobalHTTPClient = createHTTPClient()
}
// Execute is the main execution function
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Error(err)
os.Exit(1)
}
}
// createHTTPClient creates an HTTP client with TLS
func createHTTPClient() *http.Client {
// Setup client header to use TLS 1.2
tr := &http.Transport{
// Reads PROXY configuration from environment variables
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
}
// Needed due to custom client being leveraged, otherwise HTTP2 will not be used.
tr.ForceAttemptHTTP2 = true
// Create the client
return &http.Client{Transport: tr}
}