Skip to content

Commit 5829d38

Browse files
feat: Add cross-distro support for web server service names
Detect Linux distribution by parsing /etc/os-release Update server map to include distro-specific service names (apache2 for Ubuntu/Debian, httpd for CentOS/RHEL/Fedora) Modify all server commands (list, start, stop, reload, status) to use appropriate service names based on detected distro Ensure compatibility across Ubuntu, CentOS, Fedora, Debian, RHEL, SLES, and openSUSE
1 parent 3f67733 commit 5829d38

File tree

1 file changed

+108
-33
lines changed

1 file changed

+108
-33
lines changed

cmd/server/server.go

Lines changed: 108 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ Copyright © 2025 Stackroost CLI
55
package server
66

77
import (
8+
"bufio"
89
"fmt"
910
"os"
1011
"os/exec"
12+
"strings"
1113

1214
"github.com/spf13/cobra"
1315
"stackroost-cli/cmd/internal/utils"
@@ -20,21 +22,58 @@ var serverCmd = &cobra.Command{
2022
Long: `Commands for managing web server services including start, stop, reload, and status checks.`,
2123
}
2224

23-
var servers = map[string]string{
24-
"apache": "httpd",
25-
"nginx": "nginx",
26-
"caddy": "caddy",
25+
var servers = map[string]map[string]string{
26+
"ubuntu": {
27+
"apache": "apache2",
28+
"nginx": "nginx",
29+
"caddy": "caddy",
30+
},
31+
"centos": {
32+
"apache": "httpd",
33+
"nginx": "nginx",
34+
"caddy": "caddy",
35+
},
36+
"fedora": {
37+
"apache": "httpd",
38+
"nginx": "nginx",
39+
"caddy": "caddy",
40+
},
41+
"debian": {
42+
"apache": "apache2",
43+
"nginx": "nginx",
44+
"caddy": "caddy",
45+
},
46+
"rhel": {
47+
"apache": "httpd",
48+
"nginx": "nginx",
49+
"caddy": "caddy",
50+
},
51+
"sles": {
52+
"apache": "apache2",
53+
"nginx": "nginx",
54+
"caddy": "caddy",
55+
},
56+
"opensuse": {
57+
"apache": "apache2",
58+
"nginx": "nginx",
59+
"caddy": "caddy",
60+
},
2761
}
2862

2963
var listCmd = &cobra.Command{
3064
Use: "list",
3165
Short: "List installed web servers",
3266
Run: func(cmd *cobra.Command, args []string) {
67+
distro := detectDistro()
3368
fmt.Println("Installed web servers:")
34-
for name, service := range servers {
35-
if isServiceInstalled(service) {
36-
fmt.Printf("- %s (%s)\n", name, service)
69+
if distroServers, ok := servers[distro]; ok {
70+
for name, service := range distroServers {
71+
if isServiceInstalled(service) {
72+
fmt.Printf("- %s (%s)\n", name, service)
73+
}
3774
}
75+
} else {
76+
fmt.Printf("Unsupported distribution: %s\n", distro)
3877
}
3978
},
4079
}
@@ -44,14 +83,18 @@ var startCmd = &cobra.Command{
4483
Short: "Start a web server",
4584
Args: cobra.ExactArgs(1),
4685
Run: func(cmd *cobra.Command, args []string) {
86+
distro := detectDistro()
4787
server := args[0]
48-
service, ok := servers[server]
49-
if !ok {
50-
fmt.Printf("Unknown server: %s\n", server)
51-
return
88+
if distroServers, ok := servers[distro]; ok {
89+
if service, ok := distroServers[server]; ok {
90+
utils.RunCommand("sudo", "systemctl", "start", service)
91+
fmt.Printf("Started %s\n", server)
92+
} else {
93+
fmt.Printf("Unknown server: %s\n", server)
94+
}
95+
} else {
96+
fmt.Printf("Unsupported distribution: %s\n", distro)
5297
}
53-
utils.RunCommand("sudo", "systemctl", "start", service)
54-
fmt.Printf("Started %s\n", server)
5598
},
5699
}
57100

@@ -60,14 +103,18 @@ var stopCmd = &cobra.Command{
60103
Short: "Stop a web server",
61104
Args: cobra.ExactArgs(1),
62105
Run: func(cmd *cobra.Command, args []string) {
106+
distro := detectDistro()
63107
server := args[0]
64-
service, ok := servers[server]
65-
if !ok {
66-
fmt.Printf("Unknown server: %s\n", server)
67-
return
108+
if distroServers, ok := servers[distro]; ok {
109+
if service, ok := distroServers[server]; ok {
110+
utils.RunCommand("sudo", "systemctl", "stop", service)
111+
fmt.Printf("Stopped %s\n", server)
112+
} else {
113+
fmt.Printf("Unknown server: %s\n", server)
114+
}
115+
} else {
116+
fmt.Printf("Unsupported distribution: %s\n", distro)
68117
}
69-
utils.RunCommand("sudo", "systemctl", "stop", service)
70-
fmt.Printf("Stopped %s\n", server)
71118
},
72119
}
73120

@@ -76,30 +123,39 @@ var reloadCmd = &cobra.Command{
76123
Short: "Reload a web server configuration",
77124
Args: cobra.ExactArgs(1),
78125
Run: func(cmd *cobra.Command, args []string) {
126+
distro := detectDistro()
79127
server := args[0]
80-
service, ok := servers[server]
81-
if !ok {
82-
fmt.Printf("Unknown server: %s\n", server)
83-
return
128+
if distroServers, ok := servers[distro]; ok {
129+
if service, ok := distroServers[server]; ok {
130+
utils.RunCommand("sudo", "systemctl", "reload", service)
131+
fmt.Printf("Reloaded %s\n", server)
132+
} else {
133+
fmt.Printf("Unknown server: %s\n", server)
134+
}
135+
} else {
136+
fmt.Printf("Unsupported distribution: %s\n", distro)
84137
}
85-
utils.RunCommand("sudo", "systemctl", "reload", service)
86-
fmt.Printf("Reloaded %s\n", server)
87138
},
88139
}
89140

90141
var statusCmd = &cobra.Command{
91142
Use: "status",
92143
Short: "Show status of web servers",
93144
Run: func(cmd *cobra.Command, args []string) {
94-
for name, service := range servers {
95-
if isServiceInstalled(service) {
96-
fmt.Printf("%s:\n", name)
97-
cmd := exec.Command("systemctl", "status", service, "--no-pager", "-l")
98-
cmd.Stdout = os.Stdout
99-
cmd.Stderr = os.Stderr
100-
cmd.Run()
101-
fmt.Println()
145+
distro := detectDistro()
146+
if distroServers, ok := servers[distro]; ok {
147+
for name, service := range distroServers {
148+
if isServiceInstalled(service) {
149+
fmt.Printf("%s:\n", name)
150+
cmd := exec.Command("systemctl", "status", service, "--no-pager", "-l")
151+
cmd.Stdout = os.Stdout
152+
cmd.Stderr = os.Stderr
153+
cmd.Run()
154+
fmt.Println()
155+
}
102156
}
157+
} else {
158+
fmt.Printf("Unsupported distribution: %s\n", distro)
103159
}
104160
},
105161
}
@@ -114,6 +170,25 @@ func AddServerCmd(root *cobra.Command) {
114170
serverCmd.AddCommand(statusCmd)
115171
}
116172

173+
func detectDistro() string {
174+
file, err := os.Open("/etc/os-release")
175+
if err != nil {
176+
return "unknown"
177+
}
178+
defer file.Close()
179+
180+
scanner := bufio.NewScanner(file)
181+
for scanner.Scan() {
182+
line := scanner.Text()
183+
if strings.HasPrefix(line, "ID=") {
184+
id := strings.TrimPrefix(line, "ID=")
185+
id = strings.Trim(id, "\"")
186+
return id
187+
}
188+
}
189+
return "unknown"
190+
}
191+
117192
func isServiceInstalled(service string) bool {
118193
cmd := exec.Command("systemctl", "is-active", service)
119194
err := cmd.Run()

0 commit comments

Comments
 (0)