refactor: unify image download into a single DownloadImageCmd

DownloadImageCmd and DownloadWireguardImageCmd were identical except for
the image URL. Collapse them into one DownloadImageCmd that takes the
image as a parameter, passing imageName/wireguardImageName at the call
sites.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
jb
2026-06-19 11:54:08 -03:00
co-authored by Claude Opus 4.8
parent 3379ada59a
commit 6becd3462d
2 changed files with 6 additions and 30 deletions
+3 -27
View File
@@ -43,12 +43,10 @@ func TickCmd() tea.Cmd {
}) })
} }
func DownloadImageCmd(username, password string) tea.Cmd { func DownloadImageCmd(image, username, password string) tea.Cmd {
return func() tea.Msg { return func() tea.Msg {
url := imageName
loginOut, err := exec.Command( loginOut, err := exec.Command(
"docker", "login", url, "docker", "login", image,
"-u", username, "-u", username,
"-p", password, "-p", password,
).CombinedOutput() ).CombinedOutput()
@@ -60,29 +58,7 @@ func DownloadImageCmd(username, password string) tea.Cmd {
} }
} }
message, err := PullImage(url) message, err := PullImage(image)
return ImageDownloadFinishedMsg{Message: message, Err: err}
}
}
func DownloadWireguardImageCmd(username, password string) tea.Cmd {
return func() tea.Msg {
url := wireguardImageName
loginOut, err := exec.Command(
"docker", "login", url,
"-u", username,
"-p", password,
).CombinedOutput()
if err != nil {
return ImageDownloadFinishedMsg{
Message: string(loginOut),
Err: fmt.Errorf("falha no login: %w", err),
}
}
message, err := PullImage(url)
return ImageDownloadFinishedMsg{Message: message, Err: err} return ImageDownloadFinishedMsg{Message: message, Err: err}
} }
} }
+3 -3
View File
@@ -45,7 +45,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.configValues.Login = m.loginForm.Values() m.configValues.Login = m.loginForm.Values()
m.currentStep = StepDownloadImage m.currentStep = StepDownloadImage
return m, DownloadImageCmd(m.configValues.Login["user"], m.configValues.Login["password"]) return m, DownloadImageCmd(imageName, m.configValues.Login["user"], m.configValues.Login["password"])
} }
return m, cmd return m, cmd
@@ -245,7 +245,7 @@ func (m Model) updateGenerateWireguardFile(msg tea.Msg) (tea.Model, tea.Cmd) {
if msg.Err == nil { if msg.Err == nil {
m.currentStep = StepDownloadWireguard m.currentStep = StepDownloadWireguard
return m, DownloadWireguardImageCmd(m.configValues.Login["user"], m.configValues.Login["password"]) return m, DownloadImageCmd(wireguardImageName, m.configValues.Login["user"], m.configValues.Login["password"])
} }
case tea.KeyPressMsg: case tea.KeyPressMsg:
@@ -254,7 +254,7 @@ func (m Model) updateGenerateWireguardFile(msg tea.Msg) (tea.Model, tea.Cmd) {
} else if m.finishedFile && m.configFileError == nil { } else if m.finishedFile && m.configFileError == nil {
m.currentStep = StepDownloadWireguard m.currentStep = StepDownloadWireguard
return m, DownloadWireguardImageCmd(m.configValues.Login["user"], m.configValues.Login["password"]) return m, DownloadImageCmd(wireguardImageName, m.configValues.Login["user"], m.configValues.Login["password"])
} }
} }