From dff54e26509235b130c7c4554eb0d0a08d3538a5 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Thu, 9 Sep 2021 13:41:38 +0200 Subject: [PATCH] implemented CLI interface --- cmd/s3Browser.go | 37 ++++++++++++++++++++++++++++++------- go.mod | 1 + go.sum | 4 ++++ internal/httpServer.go | 4 ++-- internal/s3Broswer.go | 8 ++++++-- 5 files changed, 43 insertions(+), 11 deletions(-) diff --git a/cmd/s3Browser.go b/cmd/s3Browser.go index 40c20ad..dda9514 100644 --- a/cmd/s3Browser.go +++ b/cmd/s3Browser.go @@ -4,16 +4,39 @@ import ( "time" s3browser "git.kapelle.org/niklas/s3browser/internal" + "github.com/alexflint/go-arg" ) +type args struct { + S3Endpoint string `arg:"--s3-endpoint,required,env:S3_ENDPOINT" help:"host[:port]" placeholder:"ENDPOINT"` + S3AccessKey string `arg:"--s3-access-key,required,env:S3_ACCESS_KEY" placeholder:"ACCESS_KEY"` + S3SecretKey string `arg:"--s3-secret-key,required,env:S3_SECRET_KEY" placeholder:"SECRET_KEY"` + S3Buket string `arg:"--s3-buket,required,env:S3_BUKET" placeholder:"BUKET"` + S3DisableSSL bool `arg:"--s3-disable-ssl,env:S3_DISABLE_SSL" default:"false"` + Address string `arg:"--address,env:ADDRESS" default:":3000" help:"what address to listen on" placeholder:"ADDRESS"` + CacheTTL int64 `arg:"--cache-ttl,env:CACHE_TTL" help:"Time in seconds" default:"30" placeholder:"TTL"` + CacheCleanup int64 `arg:"--cache-cleanup,env:CACHE_CLEANUP" help:"Time in seconds" default:"60" placeholder:"CLEANUP"` + Verbose bool `arg:"-v,--verbose,env:VERBOSE" help:"verbosity level" default:"false"` +} + +func (args) Version() string { + // TODO + return "s3Browser 0.1" +} + func main() { + var args args + arg.MustParse(&args) + s3browser.Start(s3browser.AppConfig{ - S3Endoint: "localhost:9000", - S3SSL: false, - S3AccessKey: "testo", - S3SecretKey: "testotesto", - S3Buket: "dev", - CacheTTL: 20 * time.Second, - CacheCleanup: 1 * time.Minute, + S3Endoint: args.S3Endpoint, + S3SSL: !args.S3DisableSSL, + S3AccessKey: args.S3AccessKey, + S3SecretKey: args.S3SecretKey, + S3Buket: args.S3Buket, + CacheTTL: time.Duration(args.CacheTTL) * time.Second, + CacheCleanup: time.Duration(args.CacheCleanup) * time.Second, + Address: args.Address, + LogDebug: args.Verbose, }) } diff --git a/go.mod b/go.mod index 81023e8..75904f7 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.kapelle.org/niklas/s3browser go 1.16 require ( + github.com/alexflint/go-arg v1.4.2 github.com/graph-gophers/dataloader v5.0.0+incompatible github.com/graphql-go/graphql v0.7.9 github.com/graphql-go/handler v0.2.3 diff --git a/go.sum b/go.sum index 289bfa4..c3770ec 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +github.com/alexflint/go-arg v1.4.2 h1:lDWZAXxpAnZUq4qwb86p/3rIJJ2Li81EoMbTMujhVa0= +github.com/alexflint/go-arg v1.4.2/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM= +github.com/alexflint/go-scalar v1.0.0 h1:NGupf1XV/Xb04wXskDFzS0KWOLH632W/EO4fAFi+A70= +github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/internal/httpServer.go b/internal/httpServer.go index 5aa2607..cf91c37 100644 --- a/internal/httpServer.go +++ b/internal/httpServer.go @@ -17,7 +17,7 @@ import ( ) // initHttp setup and start the http server. Blocking -func initHttp(resolveContext context.Context, schema graphql.Schema) error { +func initHttp(resolveContext context.Context, schema graphql.Schema, address string) error { h := handler.New(&handler.Config{ Schema: &schema, Pretty: true, @@ -53,7 +53,7 @@ func initHttp(resolveContext context.Context, schema graphql.Schema) error { // Init the embedded static files initStatic() - return http.ListenAndServe(":8080", nil) + return http.ListenAndServe(address, nil) } func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) { diff --git a/internal/s3Broswer.go b/internal/s3Broswer.go index 022628f..985b5fd 100644 --- a/internal/s3Broswer.go +++ b/internal/s3Broswer.go @@ -19,6 +19,8 @@ type AppConfig struct { S3Buket string CacheTTL time.Duration CacheCleanup time.Duration + Address string + LogDebug bool } // File represents a file with its metadata @@ -68,7 +70,9 @@ func setupS3Client(config AppConfig) (*minio.Client, error) { // Start starts the app func Start(config AppConfig) { - log.SetLevel(log.DebugLevel) + if config.LogDebug { + log.SetLevel(log.DebugLevel) + } log.Info("Starting") log.Debug("Setting up s3 client") @@ -96,7 +100,7 @@ func Start(config AppConfig) { resolveContext = context.WithValue(resolveContext, "loader", loaderMap) log.Debug("Starting HTTP server") - err = initHttp(resolveContext, schema) + err = initHttp(resolveContext, schema, config.Address) if err != nil { log.Error("Failed to start webserver: ", err.Error())