Skip to content

Commit 360d8fd

Browse files
authored
Add flag for toggling daemon mode (#4)
* Add flag for toggling daemon mode The DAEMON flag (defaults true) toggles whether to start a run loop or execute run() once and exit. * Refactor daemon flag * Correct indentation
1 parent 45752a3 commit 360d8fd

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ The following instructions show how to setup the environment to run this code wi
4444

4545
## How to use
4646

47-
> the tool runs indefinitely until the process is killed
47+
> the tool runs indefinitely until the process is killed. This can be dissabled by setting the `DAEMON` option to `false`.
4848

4949
- <a name="setup"></a>Setup the following data:
5050

main.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func setupViper() {
2828
tokenDuration = flag.String("CODEARTIFACT_DURATION", os.Getenv("CODEARTIFACT_DURATION"), "duration of the AWS CodeArtifact authToken")
2929
codeartifactDomain = flag.String("CODEARTIFACT_DOMAIN", os.Getenv("CODEARTIFACT_DOMAIN"), "AWS CodeArtifact Domain for which access is required")
3030
codeartifactDomainOwner = flag.String("CODEARTIFACT_DOMAIN_OWNER", os.Getenv("CODEARTIFACT_DOMAIN_OWNER"), "owner (AWS acc) for the AWS CodeArtifact domain")
31+
daemon = flag.Bool("DAEMON", true, "whether to run in Daemon mode, re-authenticating every 10 hours.")
3132
)
3233

3334
flag.Parse()
@@ -39,6 +40,7 @@ func setupViper() {
3940
viper.Set("CODEARTIFACT_DURATION", tokenDuration)
4041
viper.Set("CODEARTIFACT_DOMAIN", codeartifactDomain)
4142
viper.Set("CODEARTIFACT_DOMAIN_OWNER", codeartifactDomainOwner)
43+
viper.Set("DAEMON", daemon)
4244
}
4345

4446
func main() {
@@ -62,22 +64,23 @@ func main() {
6264
}
6365
}()
6466

65-
go func() {
66-
mux := http.NewServeMux()
67-
mux.Handle("/metrics", promhttp.Handler())
68-
69-
http.ListenAndServe("0.0.0.0:8701", mux)
70-
}()
71-
7267
cfg, err := config.LoadDefaultConfig(ctx)
7368
if err != nil {
7469
logrus.Fatalf("AWS config could not be created: %v", err)
7570
}
7671

7772
run(ctx, cfg)
73+
if viper.GetBool("DAEMON") {
74+
go func() {
75+
mux := http.NewServeMux()
76+
mux.Handle("/metrics", promhttp.Handler())
7877

79-
for range time.NewTicker(time.Hour * 10).C {
80-
run(ctx, cfg)
78+
http.ListenAndServe("0.0.0.0:8701", mux)
79+
}()
80+
81+
for range time.NewTicker(time.Hour * 10).C {
82+
run(ctx, cfg)
83+
}
8184
}
8285
}
8386

@@ -111,6 +114,7 @@ func getCodeArtifactSecret(ctx context.Context, cfg aws.Config) (*string, error)
111114
Domain: &domain,
112115
DomainOwner: &domainOwner,
113116
})
117+
114118
if err != nil {
115119
return nil, fmt.Errorf("retrieving code artifact secret: %w", err)
116120
}

0 commit comments

Comments
 (0)