Skip to content

Commit

Permalink
Fix hanging on non-serving commands
Browse files Browse the repository at this point in the history
  • Loading branch information
patricksanders committed Dec 11, 2020
1 parent 27da05a commit 993d4a4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
4 changes: 1 addition & 3 deletions cmd/ecs_credential_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func init() {

var ecsCredentialProvider = &cobra.Command{
Use: "ecs_credential_provider",
Short: "Run a local ECS Credential Provider endpoint that serves and caches credentials for roles on demand",
Short: "RunService a local ECS Credential Provider endpoint that serves and caches credentials for roles on demand",
RunE: runEcsMetadata,
}

Expand All @@ -65,8 +65,6 @@ func runEcsMetadata(cmd *cobra.Command, args []string) error {
// Check for interrupt signal and exit cleanly
<-shutdown
log.Print("Shutdown signal received, exiting weep...")
// Send a signal to show that we're done shutting down
done <- 0

return nil
}
4 changes: 1 addition & 3 deletions cmd/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func init() {

var metadataCmd = &cobra.Command{
Use: "metadata [role_name]",
Short: "Run a local Instance Metadata Service (IMDS) endpoint that serves credentials",
Short: "RunService a local Instance Metadata Service (IMDS) endpoint that serves credentials",
Args: cobra.ExactArgs(1),
RunE: runMetadata,
}
Expand Down Expand Up @@ -86,8 +86,6 @@ func runMetadata(cmd *cobra.Command, args []string) error {
// Check for interrupt signal and exit cleanly
<-shutdown
log.Print("Shutdown signal received, exiting weep meta-data service...")
// Send a signal to show that we're done shutting down
done <- 0

return nil
}
23 changes: 14 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package cmd
import (
"fmt"
"os"
"os/signal"
"path"
"runtime"
"strings"
"syscall"

"github.com/netflix/weep/util"

Expand All @@ -41,8 +43,6 @@ var (
Long: "Weep is a CLI tool that manages AWS access via ConsoleMe for local development.",
DisableAutoGenTag: true,
}
shutdown chan os.Signal
done chan int
)

func init() {
Expand All @@ -53,15 +53,20 @@ func init() {
rootCmd.PersistentFlags().StringSliceVarP(&assumeRole, "assume-role", "A", make([]string, 0), "one or more roles to assume after retrieving credentials")
rootCmd.PersistentFlags().StringVar(&logFormat, "log-format", "", "log format (json or tty)")
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "", "log level (debug, info, warn)")

rootCmd.PersistentFlags().BoolVarP(&runAsService, "svc", "s", false, "run weep as a service")
}

func Execute(shutdownSignal chan os.Signal, doneSignal chan int) {
shutdown = shutdownSignal
done = doneSignal
if err := rootCmd.Execute(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
func Execute() {
shutdown := make(chan os.Signal, 1)
done = make(chan int, 1)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
if runAsService {
RunService()
} else {
if err := rootCmd.Execute(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
}

Expand Down
17 changes: 5 additions & 12 deletions run/run.go → cmd/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,14 @@
* limitations under the License.
*/

package run
package cmd

import (
"os"
"os/signal"
"syscall"

"github.com/kardianos/service"
"github.com/netflix/weep/cmd"
log "github.com/sirupsen/logrus"
)

var svcLogger service.Logger
var done chan int

type program struct{}

Expand All @@ -37,18 +31,16 @@ func (p *program) Start(s service.Service) error {
}

func (p *program) run() {
shutdown := make(chan os.Signal, 1)
done = make(chan int, 1)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
cmd.Execute(shutdown, done)
_ = rootCmd.Execute()
done <- 0
}

func (p *program) Stop(s service.Service) error {
<-done
return nil
}

func Run() {
func RunService() {
svcConfig := &service.Config{
Name: "Weep",
DisplayName: "Weep",
Expand All @@ -68,4 +60,5 @@ func Run() {
if err != nil {
_ = svcLogger.Error(err)
}
done <- 0
}
5 changes: 5 additions & 0 deletions cmd/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package cmd

import "os"

var (
assumeRole []string
role string
Expand All @@ -32,4 +34,7 @@ var (
cfgFile string
logLevel string
logFormat string
runAsService bool
shutdown chan os.Signal
done chan int
)
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ package main
import (
"os"

"github.com/netflix/weep/run"
"github.com/netflix/weep/cmd"

log "github.com/sirupsen/logrus"
)

Expand All @@ -29,5 +30,5 @@ func init() {
}

func main() {
run.Run()
cmd.Execute()
}

0 comments on commit 993d4a4

Please sign in to comment.