11package main
22
33import (
4+ "context"
45 "flag"
56 "fmt"
67 "os"
8+ "os/signal"
79
810 "github.com/aslamcodes/appstreamfile/internal/backend"
911 "github.com/aslamcodes/appstreamfile/internal/logger"
@@ -12,13 +14,17 @@ import (
1214)
1315
1416type RunOptions struct {
15- location string
16- bucket string
17- key string
18- versionId string
17+ location string
18+ SourceType string
19+ bucket string
20+ key string
21+ versionId string
1922}
2023
2124func main () {
25+ ctx , cancel := signal .NotifyContext (context .Background (), os .Interrupt )
26+ defer cancel ()
27+
2228 source := flag .String ("source" , "" , "Configuration source: s3 or local" )
2329 location := flag .String ("location" , "" , "Local filesystem path to the config file" )
2430 bucket := flag .String ("bucket" , "" , "S3 bucket containing the config file" )
@@ -30,24 +36,25 @@ func main() {
3036 logger .Init ()
3137
3238 runOptions := & RunOptions {
33- location : * location ,
34- bucket : * bucket ,
35- key : * key ,
36- versionId : * versionId ,
39+ SourceType : * source ,
40+ location : * location ,
41+ bucket : * bucket ,
42+ key : * key ,
43+ versionId : * versionId ,
3744 }
3845
39- if err := run (* source , runOptions ); err != nil {
46+ if err := run (ctx , runOptions ); err != nil {
4047 fmt .Fprintln (os .Stderr , err )
4148 os .Exit (1 )
4249 }
4350}
4451
45- func run (sourceType string , opts * RunOptions ) error {
52+ func run (ctx context. Context , opts * RunOptions ) error {
4653 var backendSource backend.BackendSource
4754 var err error
4855
49- switch sourceType {
50- case "local" :
56+ switch opts . SourceType {
57+ case "local" :
5158 if opts .location == "" {
5259 return fmt .Errorf ("location of config file must be provided" )
5360 }
@@ -58,7 +65,6 @@ func run(sourceType string, opts *RunOptions) error {
5865 }
5966 backendSource , err = backend .NewS3Backend (opts .bucket , opts .key , opts .versionId , "appstream_machine_role" )
6067
61-
6268 default :
6369 return fmt .Errorf ("invalid source provided" )
6470 }
@@ -67,13 +73,13 @@ func run(sourceType string, opts *RunOptions) error {
6773 return fmt .Errorf ("unable to create backend source: %w" , err )
6874 }
6975
70- config , err := backendSource .GetConfig ()
76+ config , err := backendSource .GetConfig (ctx )
7177
7278 if err != nil {
7379 return fmt .Errorf ("failed to fetch config from backend: %w" , err )
7480 }
7581
76- if err := validator .ValidateConfig (config ); err != nil {
82+ if err := validator .ValidateConfig (ctx , config ); err != nil {
7783 return fmt .Errorf ("config file validation failed: %w" , err )
7884 }
7985
0 commit comments