github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/osext/winsvc/example/main.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Example service program that beeps. It demonstrates how to 6 // create a service and install / remove it on a computer. 7 // It also shows how to stop / start / pause / continue any service, 8 // and how to write to event log. It also shows how to use debug 9 // facilities available in debug package. 10 // 11 package main 12 13 import ( 14 "chai2010.gopkg/osext/winsvc/svc" 15 "fmt" 16 "log" 17 "os" 18 "strings" 19 ) 20 21 func usage(errmsg string) { 22 fmt.Fprintf(os.Stderr, 23 "%s\n\n"+ 24 "usage: %s <command>\n"+ 25 " where <command> is one of\n"+ 26 " install, remove, debug, start, stop, pause or continue.\n", 27 errmsg, os.Args[0]) 28 os.Exit(2) 29 } 30 31 func main() { 32 const svcName = "myservice" 33 34 isIntSess, err := svc.IsAnInteractiveSession() 35 if err != nil { 36 log.Fatalf("failed to determine if we are running in an interactive session: %v", err) 37 } 38 if !isIntSess { 39 runService(svcName, false) 40 return 41 } 42 43 if len(os.Args) < 2 { 44 usage("no command specified") 45 } 46 47 cmd := strings.ToLower(os.Args[1]) 48 switch cmd { 49 case "debug": 50 runService(svcName, true) 51 return 52 case "install": 53 err = installService(svcName, "my service") 54 case "remove": 55 err = removeService(svcName) 56 case "start": 57 err = startService(svcName) 58 case "stop": 59 err = controlService(svcName, svc.Stop, svc.Stopped) 60 case "pause": 61 err = controlService(svcName, svc.Pause, svc.Paused) 62 case "continue": 63 err = controlService(svcName, svc.Continue, svc.Running) 64 default: 65 usage(fmt.Sprintf("invalid command %s", cmd)) 66 } 67 if err != nil { 68 log.Fatalf("failed to %s %s: %v", cmd, svcName, err) 69 } 70 return 71 }