GetServicePath returns the configuration file path for the specified service. If serviceName is empty, it returns the default directory path based on the system's service manager. For non-empty serviceName, it retrieves the exact service file path. Parameters: - serviceName: Name of the service. If
(serviceName string)
| 233 | // - string: The service configuration file path. |
| 234 | // - error: Error if the service manager is unsupported or command execution fails. |
| 235 | func GetServicePath(serviceName string) (string, error) { |
| 236 | if serviceName == "" { |
| 237 | client, err := New() |
| 238 | if err != nil { |
| 239 | return "", err |
| 240 | } |
| 241 | switch client.Name() { |
| 242 | case "systemd": |
| 243 | return "/etc/systemd/system/", nil |
| 244 | case "openrc", "sysvinit": |
| 245 | return "/etc/init.d/", nil |
| 246 | default: |
| 247 | return "", fmt.Errorf("unsupported manager: %s", client.Name()) |
| 248 | } |
| 249 | } |
| 250 | service, err := LoadServiceName(serviceName) |
| 251 | if err != nil { |
| 252 | return "", err |
| 253 | } |
| 254 | client, err := New() |
| 255 | if err != nil { |
| 256 | return "", err |
| 257 | } |
| 258 | switch client.Name() { |
| 259 | case "systemd": |
| 260 | stdout, err := exec.Command("systemctl", "show", "-p", "FragmentPath", service).Output() |
| 261 | if err != nil { |
| 262 | return "", err |
| 263 | } |
| 264 | parts := strings.SplitN(string(stdout), "=", 2) |
| 265 | if len(parts) != 2 { |
| 266 | return "", fmt.Errorf("unexpected output: %s", string(stdout)) |
| 267 | } |
| 268 | return strings.TrimSpace(parts[1]), nil |
| 269 | case "openrc", "sysvinit": |
| 270 | return fmt.Sprintf("/etc/init.d/%s", service), nil |
| 271 | default: |
| 272 | return "", fmt.Errorf("unsupported manager: %s", client.Name()) |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | func SelectInitScript(keyword string) (string, error) { |
| 277 | client, err := New() |
nothing calls this directly
no test coverage detected