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)
| 209 | // - string: The service configuration file path. |
| 210 | // - error: Error if the service manager is unsupported or command execution fails. |
| 211 | func GetServicePath(serviceName string) (string, error) { |
| 212 | if serviceName == "" { |
| 213 | client, err := New() |
| 214 | if err != nil { |
| 215 | return "", err |
| 216 | } |
| 217 | switch client.Name() { |
| 218 | case "systemd": |
| 219 | return "/etc/systemd/system/", nil |
| 220 | case "openrc", "sysvinit": |
| 221 | return "/etc/init.d/", nil |
| 222 | default: |
| 223 | return "", fmt.Errorf("unsupported manager: %s", client.Name()) |
| 224 | } |
| 225 | } |
| 226 | service, err := LoadServiceName(serviceName) |
| 227 | if err != nil { |
| 228 | return "", err |
| 229 | } |
| 230 | client, err := New() |
| 231 | if err != nil { |
| 232 | return "", err |
| 233 | } |
| 234 | switch client.Name() { |
| 235 | case "systemd": |
| 236 | stdout, err := exec.Command("systemctl", "show", "-p", "FragmentPath", service).Output() |
| 237 | if err != nil { |
| 238 | return "", err |
| 239 | } |
| 240 | parts := strings.SplitN(string(stdout), "=", 2) |
| 241 | if len(parts) != 2 { |
| 242 | return "", fmt.Errorf("unexpected output: %s", string(stdout)) |
| 243 | } |
| 244 | return strings.TrimSpace(parts[1]), nil |
| 245 | case "openrc", "sysvinit": |
| 246 | return fmt.Sprintf("/etc/init.d/%s", service), nil |
| 247 | default: |
| 248 | return "", fmt.Errorf("unsupported manager: %s", client.Name()) |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | func SelectInitScript(keyword string) (string, error) { |
| 253 | client, err := New() |
nothing calls this directly
no test coverage detected