diff --git a/cmd/create.go b/cmd/create.go index 06e5628..e048550 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -7,11 +7,11 @@ import ( "github.com/elasticpath/epcc-cli/external/aliases" "github.com/elasticpath/epcc-cli/external/autofill" "github.com/elasticpath/epcc-cli/external/completion" - "github.com/elasticpath/epcc-cli/external/crud" "github.com/elasticpath/epcc-cli/external/encoding" "github.com/elasticpath/epcc-cli/external/httpclient" "github.com/elasticpath/epcc-cli/external/json" "github.com/elasticpath/epcc-cli/external/resources" + "github.com/elasticpath/epcc-cli/external/shutdown" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "io" @@ -215,8 +215,8 @@ func NewCreateCommand(parentCmd *cobra.Command) func() { } func createInternal(ctx context.Context, overrides *httpclient.HttpParameterOverrides, args []string, autoFillOnCreate bool, aliasName string) (string, error) { - crud.OutstandingRequestCounter.Add(1) - defer crud.OutstandingRequestCounter.Done() + shutdown.OutstandingOpCounter.Add(1) + defer shutdown.OutstandingOpCounter.Done() // Find Resource resource, ok := resources.GetResourceByName(args[0]) diff --git a/cmd/delete.go b/cmd/delete.go index 49b9656..726ec5f 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -5,10 +5,10 @@ import ( "fmt" "github.com/elasticpath/epcc-cli/external/aliases" "github.com/elasticpath/epcc-cli/external/completion" - "github.com/elasticpath/epcc-cli/external/crud" "github.com/elasticpath/epcc-cli/external/httpclient" "github.com/elasticpath/epcc-cli/external/json" "github.com/elasticpath/epcc-cli/external/resources" + "github.com/elasticpath/epcc-cli/external/shutdown" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "io" @@ -171,8 +171,8 @@ func NewDeleteCommand(parentCmd *cobra.Command) func() { return resetFunc } func deleteInternal(ctx context.Context, overrides *httpclient.HttpParameterOverrides, allow404 bool, args []string) (string, error) { - crud.OutstandingRequestCounter.Add(1) - defer crud.OutstandingRequestCounter.Done() + shutdown.OutstandingOpCounter.Add(1) + defer shutdown.OutstandingOpCounter.Done() resource, ok := resources.GetResourceByName(args[0]) if !ok { diff --git a/cmd/get.go b/cmd/get.go index 6092aee..4516553 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -6,10 +6,10 @@ import ( "fmt" "github.com/elasticpath/epcc-cli/external/aliases" "github.com/elasticpath/epcc-cli/external/completion" - "github.com/elasticpath/epcc-cli/external/crud" "github.com/elasticpath/epcc-cli/external/httpclient" "github.com/elasticpath/epcc-cli/external/json" "github.com/elasticpath/epcc-cli/external/resources" + "github.com/elasticpath/epcc-cli/external/shutdown" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "io" @@ -326,8 +326,8 @@ func getUrl(resource resources.Resource, args []string) (*resources.CrudEntityIn } func getResource(ctx context.Context, overrides *httpclient.HttpParameterOverrides, args []string) (*http.Response, error) { - crud.OutstandingRequestCounter.Add(1) - defer crud.OutstandingRequestCounter.Done() + shutdown.OutstandingOpCounter.Add(1) + defer shutdown.OutstandingOpCounter.Done() // Find Resource resource, ok := resources.GetResourceByName(args[0]) diff --git a/cmd/root.go b/cmd/root.go index 92033f3..d155dd0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/elasticpath/epcc-cli/config" "github.com/elasticpath/epcc-cli/external/aliases" - "github.com/elasticpath/epcc-cli/external/crud" + "github.com/elasticpath/epcc-cli/external/clictx" "github.com/elasticpath/epcc-cli/external/httpclient" "github.com/elasticpath/epcc-cli/external/logger" "github.com/elasticpath/epcc-cli/external/profiles" @@ -221,6 +221,7 @@ func Execute() { case sig := <-sigs: log.Warnf("Shutting down program due to signal [%v]", sig) shutdown.ShutdownFlag.Store(true) + clictx.Cancel() exit = true case <-normalShutdown: } @@ -231,10 +232,10 @@ func Execute() { go func() { time.Sleep(2 * time.Second) - log.Infof("Waiting for all outstanding requests to finish") + log.Infof("Waiting for all outstanding operations to finish") }() - crud.OutstandingRequestCounter.Wait() + shutdown.OutstandingOpCounter.Wait() httpclient.LogStats() aliases.FlushAliases() diff --git a/cmd/update.go b/cmd/update.go index 7a2c0d3..ed07de0 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -6,10 +6,10 @@ import ( "fmt" "github.com/elasticpath/epcc-cli/external/aliases" "github.com/elasticpath/epcc-cli/external/completion" - "github.com/elasticpath/epcc-cli/external/crud" "github.com/elasticpath/epcc-cli/external/httpclient" "github.com/elasticpath/epcc-cli/external/json" "github.com/elasticpath/epcc-cli/external/resources" + "github.com/elasticpath/epcc-cli/external/shutdown" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "io" @@ -188,8 +188,8 @@ func NewUpdateCommand(parentCmd *cobra.Command) func() { } func updateInternal(ctx context.Context, overrides *httpclient.HttpParameterOverrides, args []string) (string, error) { - crud.OutstandingRequestCounter.Add(1) - defer crud.OutstandingRequestCounter.Done() + shutdown.OutstandingOpCounter.Add(1) + defer shutdown.OutstandingOpCounter.Done() // Find Resource resource, ok := resources.GetResourceByName(args[0]) diff --git a/external/clictx/ctx.go b/external/clictx/ctx.go new file mode 100644 index 0000000..88638ac --- /dev/null +++ b/external/clictx/ctx.go @@ -0,0 +1,11 @@ +package clictx + +import "context" + +var Ctx context.Context = nil + +var Cancel context.CancelFunc = nil + +func init() { + Ctx, Cancel = context.WithCancel(context.Background()) +} diff --git a/external/crud/outstanding_request_counter.go b/external/crud/outstanding_request_counter.go deleted file mode 100644 index d3e6e41..0000000 --- a/external/crud/outstanding_request_counter.go +++ /dev/null @@ -1,5 +0,0 @@ -package crud - -import "sync" - -var OutstandingRequestCounter = sync.WaitGroup{} diff --git a/external/shutdown/op_counter.go b/external/shutdown/op_counter.go new file mode 100644 index 0000000..d77a8a9 --- /dev/null +++ b/external/shutdown/op_counter.go @@ -0,0 +1,5 @@ +package shutdown + +import "sync" + +var OutstandingOpCounter = sync.WaitGroup{}