Scert/backend/internal/servicehost/service.go

66 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package servicehost
import (
"errors"
"fmt"
"path/filepath"
"strings"
)
type Options struct {
Name string
DisplayName string
Description string
Executable string
ConfigPath string
WorkDir string
}
func (o Options) Validate() error {
if strings.TrimSpace(o.Name) == "" {
return errors.New("服务名称不能为空")
}
if strings.ContainsAny(o.Name, `/\\`) {
return errors.New("服务名称不能包含路径分隔符")
}
if strings.TrimSpace(o.Executable) == "" {
return errors.New("服务程序路径不能为空")
}
if filepath.Clean(o.Executable) == "." {
return errors.New("服务程序路径无效")
}
if strings.TrimSpace(o.ConfigPath) == "" {
return errors.New("配置文件路径不能为空")
}
if filepath.Clean(o.ConfigPath) == "." {
return errors.New("配置文件路径无效")
}
if strings.TrimSpace(o.WorkDir) == "" {
return errors.New("服务工作目录不能为空")
}
return nil
}
func (o Options) normalized() (Options, error) {
if err := o.Validate(); err != nil {
return Options{}, err
}
o.Name = strings.TrimSpace(o.Name)
o.DisplayName = strings.TrimSpace(o.DisplayName)
if o.DisplayName == "" {
o.DisplayName = o.Name
}
o.Description = strings.TrimSpace(o.Description)
if o.Description == "" {
o.Description = o.DisplayName
}
o.Executable = filepath.Clean(strings.TrimSpace(o.Executable))
o.ConfigPath = filepath.Clean(strings.TrimSpace(o.ConfigPath))
o.WorkDir = filepath.Clean(strings.TrimSpace(o.WorkDir))
return o, nil
}
func commandError(action string, err error) error {
return fmt.Errorf("%s服务失败:%w", action, err)
}