service: update windows to use arguments.
This commit is contained in:
+7
-3
@@ -6,8 +6,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"bitbucket.org/kardianos/service2beta"
|
"bitbucket.org/kardianos/service2beta"
|
||||||
@@ -61,6 +61,9 @@ func (p *program) Stop(s service.Service) error {
|
|||||||
// Handle service controls (optional).
|
// Handle service controls (optional).
|
||||||
// Run the service.
|
// Run the service.
|
||||||
func main() {
|
func main() {
|
||||||
|
svcFlag := flag.String("service", "", "Control the system service.")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
svcConfig := &service.Config{
|
svcConfig := &service.Config{
|
||||||
Name: "GoServiceTest",
|
Name: "GoServiceTest",
|
||||||
DisplayName: "Go Service Test",
|
DisplayName: "Go Service Test",
|
||||||
@@ -77,6 +80,7 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
err := <-errs
|
err := <-errs
|
||||||
@@ -86,8 +90,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if len(os.Args) > 1 {
|
if len(*svcFlag) != 0 {
|
||||||
err := service.Control(s, os.Args[1])
|
err := service.Control(s, *svcFlag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Valid actions: %q\n", service.ControlAction)
|
log.Printf("Valid actions: %q\n", service.ControlAction)
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|||||||
+22
-6
@@ -5,9 +5,11 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -163,8 +165,20 @@ func (ws *windowsService) Install() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Used if path contains a space.
|
binPath := &bytes.Buffer{}
|
||||||
exepath = `"` + exepath + `"`
|
// Quote exe path in case it contains a string.
|
||||||
|
binPath.WriteRune('"')
|
||||||
|
binPath.WriteString(exepath)
|
||||||
|
binPath.WriteRune('"')
|
||||||
|
|
||||||
|
// Arguments are encoded with the binary path to service.
|
||||||
|
// Enclose arguments in quotes. Escape quotes with a backslash.
|
||||||
|
for _, arg := range ws.Arguments {
|
||||||
|
binPath.WriteRune(' ')
|
||||||
|
binPath.WriteString(`"`)
|
||||||
|
binPath.WriteString(strings.Replace(arg, `"`, `\"`, -1))
|
||||||
|
binPath.WriteString(`"`)
|
||||||
|
}
|
||||||
m, err := mgr.Connect()
|
m, err := mgr.Connect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -175,10 +189,12 @@ func (ws *windowsService) Install() error {
|
|||||||
s.Close()
|
s.Close()
|
||||||
return fmt.Errorf("service %s already exists", ws.Name)
|
return fmt.Errorf("service %s already exists", ws.Name)
|
||||||
}
|
}
|
||||||
s, err = m.CreateService(ws.Name, exepath, mgr.Config{
|
s, err = m.CreateService(ws.Name, binPath.String(), mgr.Config{
|
||||||
DisplayName: ws.DisplayName,
|
DisplayName: ws.DisplayName,
|
||||||
Description: ws.Description,
|
Description: ws.Description,
|
||||||
StartType: mgr.StartAutomatic,
|
StartType: mgr.StartAutomatic,
|
||||||
|
ServiceStartName: ws.UserName,
|
||||||
|
Password: ws.KV.string("Password", ""),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user