this repo has no description smallweb.run
smallweb
4
fork

Configure Feed

Select the types of activity you want to include in your feed.

remove dynamic service path

pomdtr 265377d5 d7caa5cc

+55 -77
+12 -36
cmd/service.go
··· 2 2 3 3 import ( 4 4 _ "embed" 5 - "errors" 6 - "slices" 7 - "strings" 8 5 9 6 "github.com/spf13/cobra" 10 7 ) 11 - 12 - func getServiceName(domain string) string { 13 - parts := strings.Split(domain, ".") 14 - slices.Reverse(parts) 15 - parts = append(parts, "server") 16 - return strings.Join(parts, ".") 17 - } 18 8 19 9 func NewCmdService() *cobra.Command { 20 10 cmd := &cobra.Command{ ··· 35 25 36 26 func NewCmdServiceInstall() *cobra.Command { 37 27 cmd := &cobra.Command{ 38 - Use: "install [args...]", 39 - Short: "Install smallweb as a service", 40 - PreRunE: requireDomain, 28 + Use: "install [args...]", 29 + Short: "Install smallweb as a service", 41 30 RunE: func(cmd *cobra.Command, args []string) error { 42 31 if err := InstallService(args); err != nil { 43 32 return err ··· 53 42 54 43 func NewCmdServiceUninstall() *cobra.Command { 55 44 cmd := &cobra.Command{ 56 - Use: "uninstall", 57 - Short: "Uninstall smallweb service", 58 - PreRunE: requireDomain, 45 + Use: "uninstall", 46 + Short: "Uninstall smallweb service", 59 47 RunE: func(cmd *cobra.Command, args []string) error { 60 48 if err := UninstallService(); err != nil { 61 49 return err ··· 70 58 71 59 func NewCmdServiceStart() *cobra.Command { 72 60 return &cobra.Command{ 73 - Use: "start", 74 - Short: "Start smallweb service", 75 - PreRunE: requireDomain, 61 + Use: "start", 62 + Short: "Start smallweb service", 76 63 RunE: func(cmd *cobra.Command, args []string) error { 77 64 if err := StartService(); err != nil { 78 65 return err ··· 86 73 87 74 func NewCmdServiceStop() *cobra.Command { 88 75 return &cobra.Command{ 89 - Use: "stop", 90 - Short: "Stop smallweb service", 91 - PreRunE: requireDomain, 76 + Use: "stop", 77 + Short: "Stop smallweb service", 92 78 RunE: func(cmd *cobra.Command, args []string) error { 93 79 if err := StopService(); err != nil { 94 80 return err ··· 101 87 102 88 func NewCmdServiceRestart() *cobra.Command { 103 89 return &cobra.Command{ 104 - Use: "restart", 105 - Short: "Restart smallweb service", 106 - PreRunE: requireDomain, 90 + Use: "restart", 91 + Short: "Restart smallweb service", 107 92 RunE: func(cmd *cobra.Command, args []string) error { 108 93 if err := RestartService(); err != nil { 109 94 return err ··· 123 108 cmd := &cobra.Command{ 124 109 Use: "logs", 125 110 Short: "Print service logs", 126 - PreRunE: requireDomain, 127 111 Aliases: []string{"log"}, 128 112 RunE: func(cmd *cobra.Command, args []string) error { 129 113 return PrintServiceLogs(flags.follow) ··· 136 120 137 121 func NewCmdServiceStatus() *cobra.Command { 138 122 return &cobra.Command{ 139 - Use: "status", 140 - Short: "View service status", 141 - PreRunE: requireDomain, 123 + Use: "status", 124 + Short: "View service status", 142 125 RunE: func(cmd *cobra.Command, args []string) error { 143 126 return ViewServiceStatus() 144 127 }, 145 128 } 146 129 } 147 - 148 - func requireDomain(cmd *cobra.Command, args []string) error { 149 - if k.String("domain") == "" { 150 - return errors.New("missing domain") 151 - } 152 - return nil 153 - }
+10 -15
cmd/service_darwin.go
··· 9 9 "os" 10 10 "os/exec" 11 11 "path/filepath" 12 - "slices" 13 - "strings" 14 12 "text/template" 15 13 16 14 "github.com/pomdtr/smallweb/utils" ··· 20 18 var serviceConfigBytes []byte 21 19 var serviceConfig = template.Must(template.New("service").Parse(string(serviceConfigBytes))) 22 20 23 - func getServicePath(domain string) string { 24 - parts := strings.Split(domain, ".") 25 - slices.Reverse(parts) 26 - 27 - return filepath.Join(os.Getenv("HOME"), "Library", "LaunchAgents", getServiceName(domain)+".plist") 21 + func getServicePath() string { 22 + return filepath.Join(os.Getenv("HOME"), "Library", "LaunchAgents", "com.github.pomdtr.smallweb.plist") 28 23 } 29 24 30 25 func InstallService(args []string) error { 31 - servicePath := getServicePath(k.String("domain")) 26 + servicePath := getServicePath() 32 27 if utils.FileExists(servicePath) { 33 28 return fmt.Errorf("service already installed") 34 29 } ··· 65 60 } 66 61 67 62 func StartService() error { 68 - servicePath := getServicePath(k.String("domain")) 63 + servicePath := getServicePath() 69 64 if !utils.FileExists(servicePath) { 70 65 return fmt.Errorf("service not installed") 71 66 } 72 67 73 - if err := exec.Command("launchctl", "start", getServiceName(k.String("domain"))).Run(); err != nil { 68 + if err := exec.Command("launchctl", "start", "com.github.pomdtr.smallweb").Run(); err != nil { 74 69 return fmt.Errorf("failed to start service: %v", err) 75 70 } 76 71 ··· 78 73 } 79 74 80 75 func StopService() error { 81 - servicePath := getServicePath(k.String("domain")) 76 + servicePath := getServicePath() 82 77 if !utils.FileExists(servicePath) { 83 78 return fmt.Errorf("service not installed") 84 79 } 85 80 86 - if err := exec.Command("launchctl", "stop", getServiceName(k.String("domain"))).Run(); err != nil { 81 + if err := exec.Command("launchctl", "stop", "com.github.pomdtr.smallweb").Run(); err != nil { 87 82 return fmt.Errorf("failed to stop service: %v", err) 88 83 } 89 84 ··· 95 90 } 96 91 97 92 func UninstallService() error { 98 - servicePath := getServicePath(k.String("domain")) 93 + servicePath := getServicePath() 99 94 if !utils.FileExists(servicePath) { 100 95 return fmt.Errorf("service not installed") 101 96 } ··· 113 108 } 114 109 115 110 func PrintServiceLogs(follow bool) error { 116 - servicePath := getServicePath(k.String("domain")) 111 + servicePath := getServicePath() 117 112 if !utils.FileExists(servicePath) { 118 113 return fmt.Errorf("service not installed") 119 114 } ··· 148 143 } 149 144 150 145 func ViewServiceStatus() error { 151 - cmd := exec.Command("launchctl", "list", getServiceName(k.String("domain"))) 146 + cmd := exec.Command("launchctl", "list", "com.github.pomdtr.smallweb") 152 147 cmd.Stdout = os.Stdout 153 148 cmd.Stderr = os.Stderr 154 149 if err := cmd.Run(); err != nil {
+26 -26
cmd/service_linux.go
··· 20 20 var serviceConfigBytes []byte 21 21 var serviceConfig = template.Must(template.New("service").Parse(string(serviceConfigBytes))) 22 22 23 - func getServicePath(uid int, domain string) string { 23 + func getServicePath(uid int) string { 24 24 if uid == 0 { 25 - return path.Join("/etc", "systemd", "system", getServiceName(domain)+".service") 25 + return path.Join("/etc", "systemd", "system", "com.github.pomdtr.smallweb.service") 26 26 } 27 27 28 - return path.Join(os.Getenv("HOME"), ".config", "systemd", "user", getServiceName(domain)+".service") 28 + return path.Join(os.Getenv("HOME"), ".config", "systemd", "user", "com.github.pomdtr.smallweb.service") 29 29 } 30 30 31 31 func InstallService(args []string) error { 32 32 uid := os.Getuid() 33 - servicePath := getServicePath(uid, k.String("domain")) 33 + servicePath := getServicePath(uid) 34 34 if utils.FileExists(servicePath) { 35 35 return fmt.Errorf("service already installed") 36 36 } ··· 69 69 return fmt.Errorf("failed to reload systemd manager configuration: %v", err) 70 70 } 71 71 72 - if err := exec.Command("systemctl", "enable", getServiceName(k.String("domain"))).Run(); err != nil { 72 + if err := exec.Command("systemctl", "enable", "com.github.pomdtr.smallweb").Run(); err != nil { 73 73 return fmt.Errorf("failed to enable service: %v", err) 74 74 } 75 75 76 - if err := exec.Command("systemctl", "start", getServiceName(k.String("domain"))).Run(); err != nil { 76 + if err := exec.Command("systemctl", "start", "com.github.pomdtr.smallweb").Run(); err != nil { 77 77 return fmt.Errorf("failed to start service: %v", err) 78 78 } 79 79 ··· 86 86 } 87 87 88 88 // Enable the service to start on boot 89 - if err := exec.Command("systemctl", "--user", "enable", getServiceName(k.String("domain"))).Run(); err != nil { 89 + if err := exec.Command("systemctl", "--user", "enable", "com.github.pomdtr.smallweb").Run(); err != nil { 90 90 return fmt.Errorf("failed to enable service: %v", err) 91 91 } 92 92 93 93 // Start the service immediately 94 - if err := exec.Command("systemctl", "--user", "start", getServiceName(k.String("domain"))).Run(); err != nil { 94 + if err := exec.Command("systemctl", "--user", "start", "com.github.pomdtr.smallweb").Run(); err != nil { 95 95 return fmt.Errorf("failed to start service: %v", err) 96 96 } 97 97 ··· 100 100 101 101 func StartService() error { 102 102 uid := os.Getuid() 103 - servicePath := getServicePath(uid, k.String("domain")) 103 + servicePath := getServicePath(uid) 104 104 if !utils.FileExists(servicePath) { 105 105 return fmt.Errorf("service not installed") 106 106 } 107 107 108 108 if uid == 0 { 109 - if err := exec.Command("systemctl", "start", getServiceName(k.String("domain"))).Run(); err != nil { 109 + if err := exec.Command("systemctl", "start", "com.github.pomdtr.smallweb").Run(); err != nil { 110 110 return fmt.Errorf("failed to start service: %v", err) 111 111 } 112 112 113 113 return nil 114 114 } 115 115 116 - if err := exec.Command("systemctl", "--user", "start", getServiceName(k.String("domain"))).Run(); err != nil { 116 + if err := exec.Command("systemctl", "--user", "start", "com.github.pomdtr.smallweb").Run(); err != nil { 117 117 return fmt.Errorf("failed to start service: %v", err) 118 118 } 119 119 ··· 122 122 123 123 func StopService() error { 124 124 uid := os.Getuid() 125 - servicePath := getServicePath(uid, k.String("domain")) 125 + servicePath := getServicePath(uid) 126 126 if !utils.FileExists(servicePath) { 127 127 return fmt.Errorf("service not installed") 128 128 } 129 129 130 130 if uid == 0 { 131 - if err := exec.Command("systemctl", "stop", getServiceName(k.String("domain"))).Run(); err != nil { 131 + if err := exec.Command("systemctl", "stop", "com.github.pomdtr.smallweb").Run(); err != nil { 132 132 return fmt.Errorf("failed to stop service: %v", err) 133 133 } 134 134 135 135 return nil 136 136 } 137 137 138 - if err := exec.Command("systemctl", "--user", "stop", getServiceName(k.String("domain"))).Run(); err != nil { 138 + if err := exec.Command("systemctl", "--user", "stop", "com.github.pomdtr.smallweb").Run(); err != nil { 139 139 return fmt.Errorf("failed to stop service: %v", err) 140 140 } 141 141 ··· 144 144 145 145 func RestartService() error { 146 146 uid := os.Getuid() 147 - servicePath := getServicePath(uid, k.String("domain")) 147 + servicePath := getServicePath(uid) 148 148 if !utils.FileExists(servicePath) { 149 149 return fmt.Errorf("service not installed") 150 150 } 151 151 152 152 if uid == 0 { 153 - if err := exec.Command("systemctl", "restart", getServiceName(k.String("domain"))).Run(); err != nil { 153 + if err := exec.Command("systemctl", "restart", "com.github.pomdtr.smallweb").Run(); err != nil { 154 154 return fmt.Errorf("failed to restart service: %v", err) 155 155 } 156 156 157 157 return nil 158 158 } 159 159 160 - if err := exec.Command("systemctl", "--user", "restart", getServiceName(k.String("domain"))).Run(); err != nil { 160 + if err := exec.Command("systemctl", "--user", "restart", "com.github.pomdtr.smallweb").Run(); err != nil { 161 161 return fmt.Errorf("failed to restart service: %v", err) 162 162 } 163 163 ··· 166 166 167 167 func UninstallService() error { 168 168 uid := os.Getuid() 169 - servicePath := getServicePath(uid, k.String("domain")) 169 + servicePath := getServicePath(uid) 170 170 if !utils.FileExists(servicePath) { 171 171 return fmt.Errorf("service not installed") 172 172 } 173 173 174 174 if uid == 0 { 175 - if err := exec.Command("systemctl", "stop", getServiceName(k.String("domain"))).Run(); err != nil { 175 + if err := exec.Command("systemctl", "stop", "com.github.pomdtr.smallweb").Run(); err != nil { 176 176 return fmt.Errorf("failed to stop service: %v", err) 177 177 } 178 178 179 - if err := exec.Command("systemctl", "disable", getServiceName(k.String("domain"))).Run(); err != nil { 179 + if err := exec.Command("systemctl", "disable", "com.github.pomdtr.smallweb").Run(); err != nil { 180 180 return fmt.Errorf("failed to disable service: %v", err) 181 181 } 182 182 ··· 192 192 } 193 193 194 194 // Stop the service if it is running 195 - if err := exec.Command("systemctl", "--user", "stop", getServiceName(k.String("domain"))).Run(); err != nil { 195 + if err := exec.Command("systemctl", "--user", "stop", "com.github.pomdtr.smallweb").Run(); err != nil { 196 196 return fmt.Errorf("failed to stop service: %v", err) 197 197 } 198 198 199 199 // Disable the service so it doesn't start on boot 200 - if err := exec.Command("systemctl", "--user", "disable", getServiceName(k.String("domain"))).Run(); err != nil { 200 + if err := exec.Command("systemctl", "--user", "disable", "com.github.pomdtr.smallweb").Run(); err != nil { 201 201 return fmt.Errorf("failed to disable service: %v", err) 202 202 } 203 203 ··· 216 216 func PrintServiceLogs(follow bool) error { 217 217 uid := os.Getuid() 218 218 if uid == 0 { 219 - logCmdArg := []string{getServiceName(k.String("domain"))} 219 + logCmdArg := []string{"com.github.pomdtr.smallweb"} 220 220 if follow { 221 221 logCmdArg = append(logCmdArg, "-f") 222 222 } ··· 231 231 return nil 232 232 } 233 233 234 - logCmdArgs := []string{"--user", "--user-unit", getServiceName(k.String("domain"))} 234 + logCmdArgs := []string{"--user", "--user-unit", "com.github.pomdtr.smallweb"} 235 235 if follow { 236 236 logCmdArgs = append(logCmdArgs, "-f") 237 237 } ··· 250 250 uid := os.Getuid() 251 251 252 252 if uid == 0 { 253 - statusCmd := exec.Command("systemctl", "status", getServiceName(k.String("domain"))) 253 + statusCmd := exec.Command("systemctl", "status", "com.github.pomdtr.smallweb") 254 254 statusCmd.Stdout = os.Stdout 255 255 statusCmd.Stderr = os.Stderr 256 256 if err := statusCmd.Run(); err != nil { ··· 259 259 return nil 260 260 } 261 261 262 - statusCmd := exec.Command("systemctl", "--user", "status", getServiceName(k.String("domain"))) 262 + statusCmd := exec.Command("systemctl", "--user", "status", "com.github.pomdtr.smallweb") 263 263 statusCmd.Stdout = os.Stdout 264 264 statusCmd.Stderr = os.Stderr 265 265 if err := statusCmd.Run(); err != nil {
+7
cmd/up.go
··· 287 287 me.workers[appname] = wk 288 288 return wk, nil 289 289 } 290 + 291 + func requireDomain(cmd *cobra.Command, args []string) error { 292 + if k.String("domain") == "" { 293 + return errors.New("missing domain") 294 + } 295 + return nil 296 + }