/
main.go
97 lines (91 loc) · 2.61 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
package main
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/mr-pmillz/gofireprox"
"log"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
accessKey := flag.String("access_key", "", "AWS Access Key")
secretAccessKey := flag.String("secret_access_key", "", "AWS Secret Access Key")
sessionToken := flag.String("session_token", "", "AWS Session Token.")
profile := flag.String("profile", "", "AWS Profile Section to use.")
region := flag.String("region", "", "AWS Region")
command := flag.String("command", "", "Commands: list, create, delete, update")
apiID := flag.String("api_id", "", "API ID")
proxyURL := flag.String("url", "", "URL end-point")
verbose := flag.Bool("verbose", false, "toggles verbosity to reduce API requests that fetch additional verbose data")
version := flag.Bool("version", false, "show version of gofireprox")
apiCacheDuration := flag.Duration("cache-duration", 60*time.Second, "sets api list cache duration in seconds. Ex. -cache-duration 120s for 120 seconds or 2m for 2 minutes")
flag.Parse()
fpOptions := &gofireprox.FireProxOptions{
AccessKey: *accessKey,
SecretAccessKey: *secretAccessKey,
SessionToken: *sessionToken,
Profile: *profile,
Region: *region,
Command: *command,
APIID: *apiID,
URL: *proxyURL,
Verbose: *verbose,
Version: *version,
CacheDuration: *apiCacheDuration,
}
if fpOptions.Version {
fmt.Printf("GoFireprox Current Version: %s\n", gofireprox.CurrentVersion)
os.Exit(0)
}
fp, err := gofireprox.NewFireProx(fpOptions)
if err != nil {
log.Fatal(err)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
s := <-c
fmt.Printf("CTRL+C detected: %+v\nCleaning up...", s)
fp.Cleanup()
os.Exit(1)
}()
switch *command {
case "list":
_, err = fp.ListAPIs()
if err != nil {
log.Println("[ERROR] Failed to list APIs:", err)
os.Exit(1)
}
case "delete":
successful := fp.DeleteAPI(aws.ToString(apiID))
var success string
if successful {
success = "Success!"
} else {
success = "Failed!"
}
fmt.Printf("Deleting %s => %s\n", fp.Options.APIID, success)
case "create":
if _, _, err = fp.CreateAPI(); err != nil {
log.Fatal(err)
}
case "update":
successful, err := fp.UpdateAPI(fp.Options.APIID, fp.Options.URL)
if err != nil {
log.Fatal(err)
}
var success string
if successful {
success = "Success!"
} else {
success = "Failed!"
}
fmt.Printf("API Update Complete: %s\n", success)
default:
fmt.Printf("[ERROR] Unsupported command: %s\n", *command)
os.Exit(1)
}
}