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.
This commit is contained in:
Cuong Manh Le
2025-07-29 15:43:55 +07:00
committed by Cuong Manh Le
parent 13de41d854
commit 9f656269ac

View File

@@ -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
}