|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + |
| 9 | + "github.com/kardianos/service" |
| 10 | + flag "github.com/spf13/pflag" |
| 11 | +) |
| 12 | + |
| 13 | +func handleServiceCommand(args []string) { |
| 14 | + fs := flag.NewFlagSet("service", flag.ExitOnError) |
| 15 | + fs.ParseErrorsWhitelist.UnknownFlags = false |
| 16 | + var ( |
| 17 | + cfgFlag string |
| 18 | + nameFlag string |
| 19 | + ) |
| 20 | + fs.StringVarP(&cfgFlag, "config", "C", "", "configuration file") |
| 21 | + fs.StringVar(&nameFlag, "name", "greyproxy", "service name") |
| 22 | + fs.Usage = printServiceUsage |
| 23 | + |
| 24 | + if len(args) == 0 { |
| 25 | + printServiceUsage() |
| 26 | + os.Exit(1) |
| 27 | + } |
| 28 | + |
| 29 | + action := args[0] |
| 30 | + if err := fs.Parse(args[1:]); err != nil { |
| 31 | + os.Exit(1) |
| 32 | + } |
| 33 | + |
| 34 | + svcConfig := &service.Config{ |
| 35 | + Name: nameFlag, |
| 36 | + DisplayName: "Greyproxy", |
| 37 | + Description: "Greyproxy network proxy service", |
| 38 | + Option: service.KeyValue{ |
| 39 | + "UserService": true, |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + // On install, pass -C to the service config so the service manager |
| 44 | + // starts the binary with the correct config path. |
| 45 | + if action == "install" && cfgFlag != "" { |
| 46 | + absPath, err := filepath.Abs(cfgFlag) |
| 47 | + if err != nil { |
| 48 | + fmt.Fprintf(os.Stderr, "error: resolving config path: %v\n", err) |
| 49 | + os.Exit(1) |
| 50 | + } |
| 51 | + svcConfig.Arguments = []string{"-C", absPath} |
| 52 | + } |
| 53 | + |
| 54 | + p := &program{} |
| 55 | + s, err := service.New(p, svcConfig) |
| 56 | + if err != nil { |
| 57 | + fmt.Fprintf(os.Stderr, "error: %v\n", err) |
| 58 | + os.Exit(1) |
| 59 | + } |
| 60 | + |
| 61 | + switch action { |
| 62 | + case "install", "uninstall", "start", "stop", "restart": |
| 63 | + if err := service.Control(s, action); err != nil { |
| 64 | + fmt.Fprintf(os.Stderr, "error: %v\n", err) |
| 65 | + os.Exit(1) |
| 66 | + } |
| 67 | + fmt.Printf("service %s: %s succeeded\n", nameFlag, action) |
| 68 | + |
| 69 | + case "status": |
| 70 | + status, err := s.Status() |
| 71 | + if err != nil { |
| 72 | + fmt.Fprintf(os.Stderr, "error: %v\n", err) |
| 73 | + os.Exit(1) |
| 74 | + } |
| 75 | + switch status { |
| 76 | + case service.StatusRunning: |
| 77 | + fmt.Printf("service %s: running\n", nameFlag) |
| 78 | + case service.StatusStopped: |
| 79 | + fmt.Printf("service %s: stopped\n", nameFlag) |
| 80 | + default: |
| 81 | + fmt.Printf("service %s: unknown\n", nameFlag) |
| 82 | + } |
| 83 | + |
| 84 | + default: |
| 85 | + fmt.Fprintf(os.Stderr, "unknown action: %s\n", action) |
| 86 | + printServiceUsage() |
| 87 | + os.Exit(1) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func printServiceUsage() { |
| 92 | + fmt.Fprintf(os.Stderr, `greyproxy %s (%s %s/%s) |
| 93 | +
|
| 94 | +Usage: greyproxy service <action> [flags] |
| 95 | +
|
| 96 | +Actions: |
| 97 | + install Register greyproxy as an OS service |
| 98 | + uninstall Remove the OS service registration |
| 99 | + start Start the service |
| 100 | + stop Stop the service |
| 101 | + restart Restart the service |
| 102 | + status Show service status |
| 103 | +
|
| 104 | +Flags: |
| 105 | + -C, --config string Configuration file (optional, only used with install) |
| 106 | + --name string Service name (default "greyproxy") |
| 107 | +`, version, runtime.Version(), runtime.GOOS, runtime.GOARCH) |
| 108 | +} |
0 commit comments