From 9f656269ac70ce5c3bbad2e1e4958f29dd97a629 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Tue, 29 Jul 2025 15:43:55 +0700 Subject: [PATCH] fix: complete porting of initUninstallCmd logic to ServiceCommand.Uninstall Add missing selfDeleteExe() call and supportedSelfDelete check that were present in the original initUninstallCmd function. This ensures the uninstall command properly handles self-deletion of the binary when cleanup is enabled. The original logic included: - selfDeleteExe() call for self-deletion - supportedSelfDelete check for platform-specific behavior - Proper error handling and logging This completes the porting of all functionality from the original initUninstallCmd to the new ServiceCommand.Uninstall method. --- cmd/cli/commands_service.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/cmd/cli/commands_service.go b/cmd/cli/commands_service.go index 88ad552..6e57980 100644 --- a/cmd/cli/commands_service.go +++ b/cmd/cli/commands_service.go @@ -551,6 +551,18 @@ func (sc *ServiceCommand) Uninstall(cmd *cobra.Command, args []string) error { files = append(files, file) return nil }) + bin, err := os.Executable() + if err != nil { + mainLog.Load().Warn().Err(err).Msg("failed to get executable path") + } + if bin != "" && supportedSelfDelete { + files = append(files, bin) + } + // Backup file after upgrading. + oldBin := bin + oldBinSuffix + if _, err := os.Stat(oldBin); err == nil { + files = append(files, oldBin) + } for _, file := range files { if file == "" { continue @@ -559,6 +571,14 @@ func (sc *ServiceCommand) Uninstall(cmd *cobra.Command, args []string) error { mainLog.Load().Notice().Msgf("removed %s", file) } } + // Self-delete the ctrld binary if supported + if err := selfDeleteExe(); err != nil { + mainLog.Load().Warn().Err(err).Msg("failed to delete ctrld binary") + } else { + if !supportedSelfDelete { + mainLog.Load().Debug().Msgf("file removed: %s", bin) + } + } } return nil }