cmd/cli: unify reset DNS task

The task is used in multiple places, easy to be missed and cause problem
if modifying in one place but not the others.
This commit is contained in:
Cuong Manh Le authored and Cuong Manh Le committed 2024-05-27 15:16:17 +07:00
1 parent 34801382f5
commit 411f7434f4
1 file changed
+25 -17
+25 -17
View File
@@ -183,7 +183,6 @@ func initCLI() {
}
status, err := s.Status()
isCtrldInstalled := !errors.Is(err, service.ErrNotInstalled)
isCtrldRunning := status == service.StatusRunning
// If pin code was set, do not allow running start command.
@@ -309,21 +308,7 @@ func initCLI() {
}
tasks := []task{
{func() error {
// Always reset DNS first, ensuring DNS setting is in a good state.
// resetDNS must use the "iface" value of current running ctrld
// process to reset what setDNS has done properly.
oldIface := iface
iface = "auto"
if isCtrldRunning {
iface = runningIface(s)
}
if isCtrldInstalled {
resetDnsNoLog(p)
}
iface = oldIface
return nil
}, false},
resetDnsTask(p, s),
{s.Stop, false},
{func() error { return doGenerateNextDNSConfig(nextdns) }, true},
{func() error { return ensureUninstall(s) }, false},
@@ -903,6 +888,7 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
mainLog.Load().Error().Msg(err.Error())
return
}
svcInstalled := true
if _, err := s.Status(); errors.Is(err, service.ErrNotInstalled) {
svcInstalled = false
@@ -941,8 +927,8 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
return true
}
tasks := []task{
resetDnsTask(p, s),
{s.Stop, false},
{func() error { resetDnsNoLog(p); return nil }, false},
{s.Start, false},
}
if doTasks(tasks) {
@@ -2561,3 +2547,25 @@ func resetDnsNoLog(p *prog) {
p.resetDNS()
zerolog.SetGlobalLevel(lvl)
}
// resetDnsTask returns a task which perform reset DNS operation.
func resetDnsTask(p *prog, s service.Service) task {
status, err := s.Status()
isCtrldInstalled := !errors.Is(err, service.ErrNotInstalled)
isCtrldRunning := status == service.StatusRunning
return task{func() error {
// Always reset DNS first, ensuring DNS setting is in a good state.
// resetDNS must use the "iface" value of current running ctrld
// process to reset what setDNS has done properly.
oldIface := iface
iface = "auto"
if isCtrldRunning {
iface = runningIface(s)
}
if isCtrldInstalled {
resetDnsNoLog(p)
}
iface = oldIface
return nil
}, false}
}