Skip to content

Commit

Permalink
refactor: service commands in one file
Browse files Browse the repository at this point in the history
  • Loading branch information
clementd64 committed Apr 24, 2024
1 parent 880c659 commit e346b3f
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 129 deletions.
33 changes: 5 additions & 28 deletions cmd/main-windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,19 @@ import (
"log/slog"
"os"

"github.com/spacefoot/wsproxy/internal/core"
"github.com/spacefoot/wsproxy/internal/windows"
"golang.org/x/sys/windows/svc"
)

func main() {
inService, err := svc.IsWindowsService()
inService, err := windows.RunIfService()
if err != nil {
slog.Error("failed to determine if we are running in service", "err", err)
os.Exit(1)
}

if inService {
svc.Run(windows.Name, &Handler{})
slog.Error("failed to run service", "err", err)
return
}

if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}

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)
}
if !inService {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
}
27 changes: 0 additions & 27 deletions cmd/service-controls.go

This file was deleted.

32 changes: 0 additions & 32 deletions cmd/service-install.go

This file was deleted.

26 changes: 0 additions & 26 deletions cmd/service-remove.go

This file was deleted.

67 changes: 67 additions & 0 deletions cmd/service-windows.go
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)
}
16 changes: 0 additions & 16 deletions cmd/service.go

This file was deleted.

42 changes: 42 additions & 0 deletions internal/windows/run.go
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)
}
}
}
}

0 comments on commit e346b3f

Please sign in to comment.