cmd/cli: ensure ctrld was uninstalled before installing

In some old Windows systems, s.Uninstall does not remove the service
completely at the time s.Install was running, prevent ctrld from being
installed again.

Workaround this by attempting to uninstall ctrld several times, re-check
for service status after each attempt to ensure it was uninstalled.
This commit is contained in:
Cuong Manh Le
2024-02-03 00:35:57 +07:00
committed by Cuong Manh Le
parent d822bf4257
commit 9515db7faf

View File

@@ -266,7 +266,7 @@ func initCLI() {
tasks := []task{
{s.Stop, false},
{func() error { return doGenerateNextDNSConfig(nextdns) }, true},
{s.Uninstall, false},
{func() error { return ensureUninstall(s) }, false},
{s.Install, false},
{s.Start, true},
// Note that startCmd do not actually write ControlD config, but the config file was
@@ -2102,3 +2102,17 @@ func checkDeactivationPin(s service.Service) error {
mainLog.Load().Error().Msg("deactivation pin is invalid")
return errInvalidDeactivationPin
}
// ensureUninstall ensures that s.Uninstall will remove ctrld service from system completely.
func ensureUninstall(s service.Service) error {
maxAttempts := 10
var err error
for i := 0; i < maxAttempts; i++ {
err = s.Uninstall()
if _, err := s.Status(); errors.Is(err, service.ErrNotInstalled) {
return nil
}
time.Sleep(time.Second)
}
return errors.Join(err, errors.New("uninstall failed"))
}