refactor: simplify service installation process

This commit is contained in:
2026-07-19 20:07:26 +08:00
parent a2630df9e0
commit dcdb54b76c
3 changed files with 75 additions and 9 deletions

View File

@ -40,6 +40,9 @@ func newServiceInstallCmd() *cobra.Command {
Short: "Install mihomo as a system service",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if err := ensureRoot(); err != nil {
return err
}
m, err := newManagers("", "", "")
if err != nil {
return err
@ -62,6 +65,9 @@ func newServiceUninstallCmd() *cobra.Command {
Short: "Uninstall mihomo system service",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if err := ensureRoot(); err != nil {
return err
}
m, err := newManagers("", "", "")
if err != nil {
return err
@ -83,6 +89,9 @@ func newServiceStartCmd() *cobra.Command {
Short: "Start mihomo system service",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if err := ensureRoot(); err != nil {
return err
}
m, err := newManagers("", "", "")
if err != nil {
return err
@ -104,6 +113,9 @@ func newServiceStopCmd() *cobra.Command {
Short: "Stop mihomo system service",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if err := ensureRoot(); err != nil {
return err
}
m, err := newManagers("", "", "")
if err != nil {
return err
@ -125,6 +137,9 @@ func newServiceRestartCmd() *cobra.Command {
Short: "Restart mihomo system service",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if err := ensureRoot(); err != nil {
return err
}
m, err := newManagers("", "", "")
if err != nil {
return err