| 
									
										
										
										
											2019-07-01 02:44:48 -06:00
										 |  |  | //go:generate go run -mod=vendor github.com/UnnoTed/fileb0x b0x.toml | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 01:36:35 -06:00
										 |  |  | package manager | 
					
						
							| 
									
										
										
										
											2019-07-01 02:44:48 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2019-07-02 00:02:09 -06:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2019-07-01 02:44:48 -06:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2019-07-03 02:11:50 -06:00
										 |  |  | 	"os/exec" | 
					
						
							| 
									
										
										
										
											2019-07-01 02:44:48 -06:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2019-07-10 01:16:45 -06:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2019-07-01 02:44:48 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-02 23:51:30 -06:00
										 |  |  | 	"git.rootprojects.org/root/go-serviceman/service" | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2019-07-01 02:44:48 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Install will do a best-effort attempt to install a start-on-startup | 
					
						
							|  |  |  | // user or system service via systemd, launchd, or reg.exe | 
					
						
							| 
									
										
										
										
											2019-07-13 20:50:00 -06:00
										 |  |  | func Install(c *service.Service) (string, error) { | 
					
						
							| 
									
										
										
										
											2019-07-01 02:44:48 -06:00
										 |  |  | 	if "" == c.Exec { | 
					
						
							|  |  |  | 		c.Exec = c.Name | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !c.System { | 
					
						
							|  |  |  | 		home, err := os.UserHomeDir() | 
					
						
							|  |  |  | 		if nil != err { | 
					
						
							| 
									
										
										
										
											2019-07-02 00:02:09 -06:00
										 |  |  | 			fmt.Fprintf(os.Stderr, "Unrecoverable Error: %s", err) | 
					
						
							|  |  |  | 			os.Exit(4) | 
					
						
							| 
									
										
										
										
											2019-07-13 20:50:00 -06:00
										 |  |  | 			return "", err | 
					
						
							| 
									
										
										
										
											2019-07-02 00:02:09 -06:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2019-07-02 23:51:30 -06:00
										 |  |  | 			c.Home = home | 
					
						
							| 
									
										
										
										
											2019-07-01 02:44:48 -06:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-13 20:50:00 -06:00
										 |  |  | 	name, err := install(c) | 
					
						
							| 
									
										
										
										
											2019-07-01 02:44:48 -06:00
										 |  |  | 	if nil != err { | 
					
						
							| 
									
										
										
										
											2019-07-13 20:50:00 -06:00
										 |  |  | 		return "", err | 
					
						
							| 
									
										
										
										
											2019-07-01 02:44:48 -06:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-02 00:25:16 -06:00
										 |  |  | 	err = os.MkdirAll(c.Logdir, 0755) | 
					
						
							| 
									
										
										
										
											2019-07-01 02:44:48 -06:00
										 |  |  | 	if nil != err { | 
					
						
							| 
									
										
										
										
											2019-07-13 20:50:00 -06:00
										 |  |  | 		return "", err | 
					
						
							| 
									
										
										
										
											2019-07-01 02:44:48 -06:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-13 20:50:00 -06:00
										 |  |  | 	return name, nil | 
					
						
							| 
									
										
										
										
											2019-07-01 02:44:48 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-10 01:16:45 -06:00
										 |  |  | func Start(conf *service.Service) error { | 
					
						
							|  |  |  | 	return start(conf) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func Stop(conf *service.Service) error { | 
					
						
							|  |  |  | 	return stop(conf) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-02 14:53:43 -06:00
										 |  |  | func List(conf *service.Service) ([]string, []string, []error) { | 
					
						
							|  |  |  | 	return list(conf) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 02:11:50 -06:00
										 |  |  | // IsPrivileged returns true if we suspect that the current user (or process) will be able | 
					
						
							| 
									
										
										
										
											2019-07-01 02:44:48 -06:00
										 |  |  | // to write to system folders, bind to privileged ports, and otherwise | 
					
						
							|  |  |  | // successfully run a system service. | 
					
						
							|  |  |  | func IsPrivileged() bool { | 
					
						
							|  |  |  | 	return isPrivileged() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 02:11:50 -06:00
										 |  |  | // WhereIs uses exec.LookPath to return an absolute filepath with forward slashes | 
					
						
							|  |  |  | func WhereIs(exe string) (string, error) { | 
					
						
							|  |  |  | 	exepath, err := exec.LookPath(exe) | 
					
						
							|  |  |  | 	if nil != err { | 
					
						
							|  |  |  | 		return "", err | 
					
						
							| 
									
										
										
										
											2019-07-01 02:44:48 -06:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-07-03 02:11:50 -06:00
										 |  |  | 	return filepath.Abs(filepath.ToSlash(exepath)) | 
					
						
							| 
									
										
										
										
											2019-07-01 02:44:48 -06:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-07-10 01:16:45 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-02 14:53:43 -06:00
										 |  |  | type ManageError struct { | 
					
						
							|  |  |  | 	Name   string | 
					
						
							|  |  |  | 	Hint   string | 
					
						
							|  |  |  | 	Parent error | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (e *ManageError) Error() string { | 
					
						
							|  |  |  | 	return e.Name + ": " + e.Hint + ": " + e.Parent.Error() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-10 01:16:45 -06:00
										 |  |  | type ErrDaemonize struct { | 
					
						
							|  |  |  | 	DaemonArgs []string | 
					
						
							|  |  |  | 	error      string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (e *ErrDaemonize) Error() string { | 
					
						
							|  |  |  | 	return e.error + "\nYou need to switch on ErrDaemonize, and use .DaemonArgs, which would run this:" + strings.Join(e.DaemonArgs, " ") | 
					
						
							|  |  |  | } |