diff --git a/cmd/storagenode/main.go b/cmd/storagenode/main.go index 8ebc61ab4..37615b9dc 100644 --- a/cmd/storagenode/main.go +++ b/cmd/storagenode/main.go @@ -16,13 +16,13 @@ import ( func main() { process.SetHardcodedApplicationName("storagenode") - if startAsService() { - return - } - allowDefaults := !isFilewalkerCommand() rootCmd, _ := newRootCmd(allowDefaults) + if startAsService(rootCmd) { + return + } + loggerFunc := func(logger *zap.Logger) *zap.Logger { return logger.With(zap.String("process", rootCmd.Use)) } diff --git a/cmd/storagenode/service_nonwindows.go b/cmd/storagenode/service_nonwindows.go index 8775ef406..8d84e2eee 100644 --- a/cmd/storagenode/service_nonwindows.go +++ b/cmd/storagenode/service_nonwindows.go @@ -6,6 +6,8 @@ package main -func startAsService() bool { +import "github.com/spf13/cobra" + +func startAsService(*cobra.Command) bool { return false } diff --git a/cmd/storagenode/service_windows.go b/cmd/storagenode/service_windows.go index 848ba322d..597be0e6e 100644 --- a/cmd/storagenode/service_windows.go +++ b/cmd/storagenode/service_windows.go @@ -23,9 +23,7 @@ import ( "storj.io/private/process" ) -var rootCmd, runCmd *cobra.Command - -func startAsService() bool { +func startAsService(cmd *cobra.Command) bool { isService, err := svc.IsWindowsService() if err != nil { zap.L().Fatal("Failed to determine if session is a service.", zap.Error(err)) @@ -43,12 +41,10 @@ func startAsService() bool { return false } - var factory *Factory - rootCmd, factory = newRootCmd(true) - runCmd = newRunCmd(factory) - // Initialize the Windows Service handler - err = svc.Run("storagenode", &service{}) + err = svc.Run("storagenode", &service{ + rootCmd: cmd, + }) if err != nil { zap.L().Fatal("Service failed.", zap.Error(err)) } @@ -56,16 +52,29 @@ func startAsService() bool { return true } -type service struct{} +type service struct { + rootCmd *cobra.Command +} func (m *service) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) { const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown + var runCmd *cobra.Command + for _, c := range m.rootCmd.Commands() { + if c.Use == "run" { + runCmd = c + } + } + + if runCmd == nil { + panic("Assertion is failed: 'run' sub-command is not found.") + } + changes <- svc.Status{State: svc.StartPending} var group errgroup.Group group.Go(func() error { - process.Exec(rootCmd) + process.Exec(m.rootCmd) return nil }) defer func() { _ = group.Wait() }()