-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: service commands in one file
- Loading branch information
1 parent
880c659
commit e346b3f
Showing
7 changed files
with
114 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//go:build windows | ||
|
||
package main | ||
|
||
import ( | ||
"log/slog" | ||
|
||
"github.com/spacefoot/wsproxy/internal/windows" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var serviceCmd = &cobra.Command{ | ||
Use: "service", | ||
Short: "Manage Windows service", | ||
} | ||
|
||
var serviceInstallCmd = &cobra.Command{ | ||
Use: "install", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if err := windows.Install(); err != nil { | ||
return err | ||
} | ||
|
||
slog.Info("Service installed") | ||
|
||
if err := windows.Start(); err != nil { | ||
return err | ||
} | ||
|
||
slog.Info("Service started") | ||
return nil | ||
}, | ||
} | ||
|
||
var serviceRemoveCmd = &cobra.Command{ | ||
Use: "remove", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if err := windows.Remove(); err != nil { | ||
return err | ||
} | ||
|
||
slog.Info("Service scheduled for removal (after next reboot)") | ||
return nil | ||
}, | ||
} | ||
|
||
func generateControlCommand(name string, handler func() error) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: name, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return handler() | ||
}, | ||
} | ||
} | ||
|
||
var ( | ||
serviceStartCmd = generateControlCommand("start", windows.Start) | ||
serviceStopCmd = generateControlCommand("stop", windows.Stop) | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(serviceCmd) | ||
serviceCmd.AddCommand(serviceInstallCmd) | ||
serviceCmd.AddCommand(serviceRemoveCmd) | ||
serviceCmd.AddCommand(serviceStopCmd) | ||
serviceCmd.AddCommand(serviceStartCmd) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//go:build windows | ||
|
||
package windows | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spacefoot/wsproxy/internal/core" | ||
"golang.org/x/sys/windows/svc" | ||
) | ||
|
||
func RunIfService() (bool, error) { | ||
inService, err := svc.IsWindowsService() | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if !inService { | ||
return false, nil | ||
} | ||
|
||
svc.Run(Name, &Handler{}) | ||
return true, nil | ||
} | ||
|
||
type Handler struct{} | ||
|
||
func (m *Handler) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) { | ||
changes <- svc.Status{State: svc.StartPending} | ||
go core.Run() | ||
changes <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown} | ||
for { | ||
select { | ||
case c := <-r: | ||
switch c.Cmd { | ||
case svc.Stop, svc.Shutdown: | ||
changes <- svc.Status{State: svc.StopPending} | ||
os.Exit(0) | ||
} | ||
} | ||
} | ||
} |