Skip to content

Commit 624daad

Browse files
committed
add ability to register services for multiple domains
1 parent 2699827 commit 624daad

File tree

5 files changed

+45
-7
lines changed

5 files changed

+45
-7
lines changed

cmd/root.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,27 @@ func NewCmdRoot(changelog string) *cobra.Command {
6161
Use: "smallweb",
6262
Short: "Host websites from your internet folder",
6363
Version: build.Version,
64-
PersistentPreRun: func(cmd *cobra.Command, args []string) {
64+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
6565
flagProvider := posflag.Provider(cmd.Root().PersistentFlags(), ".", k)
6666
_ = k.Load(flagProvider, nil)
6767

6868
configPath := filepath.Join(k.String("dir"), ".smallweb", "config.json")
6969
fileProvider := file.Provider(configPath)
7070
_ = k.Load(fileProvider, utils.ConfigParser())
7171

72+
if k.String("domain") == "" {
73+
return fmt.Errorf("domain is required")
74+
}
75+
7276
_ = fileProvider.Watch(func(event interface{}, err error) {
7377
k = koanf.New(".")
7478
_ = k.Load(defaultProvider, nil)
7579
_ = k.Load(envProvider, nil)
7680
_ = k.Load(posflag.Provider(cmd.PersistentFlags(), ".", k), nil)
7781
_ = k.Load(fileProvider, utils.ConfigParser())
7882
})
83+
84+
return nil
7985
},
8086
ValidArgsFunction: completePlugins,
8187
Args: cobra.MinimumNArgs(1),

cmd/service.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@ package cmd
22

33
import (
44
_ "embed"
5+
"slices"
6+
"strings"
57

68
"github.com/spf13/cobra"
79
)
810

11+
func getServiceName(domain string) string {
12+
parts := strings.Split(domain, ".")
13+
slices.Reverse(parts)
14+
parts = append(parts, "server")
15+
return strings.Join(parts, ".")
16+
}
17+
918
func NewCmdService() *cobra.Command {
1019
cmd := &cobra.Command{
1120
Use: "service",

cmd/service_darwin.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,26 @@ import (
99
"os"
1010
"os/exec"
1111
"path/filepath"
12+
"slices"
13+
"strings"
1214
"text/template"
1315

1416
"github.com/pomdtr/smallweb/utils"
1517
)
1618

17-
//go:embed embed/com.pomdtr.smallweb.plist
19+
//go:embed embed/smallweb.plist
1820
var serviceConfigBytes []byte
1921
var serviceConfig = template.Must(template.New("service").Parse(string(serviceConfigBytes)))
20-
var servicePath = filepath.Join(os.Getenv("HOME"), "Library", "LaunchAgents", "com.pomdtr.smallweb.plist")
22+
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")
28+
}
2129

2230
func InstallService(args []string) error {
31+
servicePath := getServicePath(k.String("domain"))
2332
if utils.FileExists(servicePath) {
2433
return fmt.Errorf("service already installed")
2534
}
@@ -56,23 +65,25 @@ func InstallService(args []string) error {
5665
}
5766

5867
func StartService() error {
68+
servicePath := getServicePath(k.String("domain"))
5969
if !utils.FileExists(servicePath) {
6070
return fmt.Errorf("service not installed")
6171
}
6272

63-
if err := exec.Command("launchctl", "start", "com.pomdtr.smallweb").Run(); err != nil {
73+
if err := exec.Command("launchctl", "start", getServiceName(k.String("domain"))).Run(); err != nil {
6474
return fmt.Errorf("failed to start service: %v", err)
6575
}
6676

6777
return nil
6878
}
6979

7080
func StopService() error {
81+
servicePath := getServicePath(k.String("domain"))
7182
if !utils.FileExists(servicePath) {
7283
return fmt.Errorf("service not installed")
7384
}
7485

75-
if err := exec.Command("launchctl", "stop", "com.pomdtr.smallweb").Run(); err != nil {
86+
if err := exec.Command("launchctl", "stop", getServiceName(k.String("domain"))).Run(); err != nil {
7687
return fmt.Errorf("failed to stop service: %v", err)
7788
}
7889

@@ -84,6 +95,7 @@ func RestartService() error {
8495
}
8596

8697
func UninstallService() error {
98+
servicePath := getServicePath(k.String("domain"))
8799
if !utils.FileExists(servicePath) {
88100
return fmt.Errorf("service not installed")
89101
}
@@ -101,6 +113,7 @@ func UninstallService() error {
101113
}
102114

103115
func PrintServiceLogs(follow bool) error {
116+
servicePath := getServicePath(k.String("domain"))
104117
if !utils.FileExists(servicePath) {
105118
return fmt.Errorf("service not installed")
106119
}
@@ -135,7 +148,7 @@ func PrintServiceLogs(follow bool) error {
135148
}
136149

137150
func ViewServiceStatus() error {
138-
cmd := exec.Command("launchctl", "list", "com.pomdtr.smallweb")
151+
cmd := exec.Command("launchctl", "list", getServiceName(k.String("domain")))
139152
cmd.Stdout = os.Stdout
140153
cmd.Stderr = os.Stderr
141154
if err := cmd.Run(); err != nil {

cmd/service_linux.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os"
99
"os/exec"
10+
"path"
1011
"path/filepath"
1112
"strings"
1213
"text/template"
@@ -17,9 +18,13 @@ import (
1718
//go:embed embed/smallweb.service
1819
var serviceConfigBytes []byte
1920
var serviceConfig = template.Must(template.New("service").Parse(string(serviceConfigBytes)))
20-
var servicePath = filepath.Join(os.Getenv("HOME"), ".config", "systemd", "user", "smallweb.service")
21+
22+
func getServicePath(domain string) string {
23+
return path.Join(os.Getenv("HOME"), ".config", "systemd", "user", getServiceName(domain)+".service")
24+
}
2125

2226
func InstallService(args []string) error {
27+
servicePath := getServicePath(k.String("domain"))
2328
if utils.FileExists(servicePath) {
2429
return fmt.Errorf("service already installed")
2530
}
@@ -66,6 +71,7 @@ func InstallService(args []string) error {
6671
}
6772

6873
func StartService() error {
74+
servicePath := getServicePath(k.String("domain"))
6975
if !utils.FileExists(servicePath) {
7076
return fmt.Errorf("service not installed")
7177
}
@@ -78,6 +84,7 @@ func StartService() error {
7884
}
7985

8086
func StopService() error {
87+
servicePath := getServicePath(k.String("domain"))
8188
if !utils.FileExists(servicePath) {
8289
return fmt.Errorf("service not installed")
8390
}
@@ -90,6 +97,7 @@ func StopService() error {
9097
}
9198

9299
func RestartService() error {
100+
servicePath := getServicePath(k.String("domain"))
93101
if !utils.FileExists(servicePath) {
94102
return fmt.Errorf("service not installed")
95103
}
@@ -102,6 +110,7 @@ func RestartService() error {
102110
}
103111

104112
func UninstallService() error {
113+
servicePath := getServicePath(k.String("domain"))
105114
if !utils.FileExists(servicePath) {
106115
return fmt.Errorf("service not installed")
107116
}
@@ -129,6 +138,7 @@ func UninstallService() error {
129138
}
130139

131140
func PrintServiceLogs(follow bool) error {
141+
servicePath := getServicePath(k.String("domain"))
132142
if !utils.FileExists(servicePath) {
133143
return fmt.Errorf("service not installed")
134144
}

0 commit comments

Comments
 (0)