MCPcopy Index your code
hub / github.com/1Panel-dev/1Panel / GetServicePath

Function GetServicePath

agent/utils/controller/controller.go:211–250  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

209// - string: The service configuration file path.
210// - error: Error if the service manager is unsupported or command execution fails.
211func 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
252func SelectInitScript(keyword string) (string, error) {
253 client, err := New()

Callers

nothing calls this directly

Calls 3

NewFunction · 0.70
LoadServiceNameFunction · 0.70
NameMethod · 0.65

Tested by

no test coverage detected