From 6becd3462d717fb9220fa43cfeae9364f7266e2a Mon Sep 17 00:00:00 2001 From: jb Date: Fri, 19 Jun 2026 11:54:08 -0300 Subject: [PATCH] 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 --- internal/tui/cmds.go | 30 +++--------------------------- internal/tui/update.go | 6 +++--- 2 files changed, 6 insertions(+), 30 deletions(-) diff --git a/internal/tui/cmds.go b/internal/tui/cmds.go index b002b4a..7aa98c1 100644 --- a/internal/tui/cmds.go +++ b/internal/tui/cmds.go @@ -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 { - url := imageName - loginOut, err := exec.Command( - "docker", "login", url, + "docker", "login", image, "-u", username, "-p", password, ).CombinedOutput() @@ -60,29 +58,7 @@ func DownloadImageCmd(username, password string) tea.Cmd { } } - message, err := PullImage(url) - 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) + message, err := PullImage(image) return ImageDownloadFinishedMsg{Message: message, Err: err} } } diff --git a/internal/tui/update.go b/internal/tui/update.go index 3878eb3..1c71191 100644 --- a/internal/tui/update.go +++ b/internal/tui/update.go @@ -45,7 +45,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.configValues.Login = m.loginForm.Values() 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 @@ -245,7 +245,7 @@ func (m Model) updateGenerateWireguardFile(msg tea.Msg) (tea.Model, tea.Cmd) { if msg.Err == nil { 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: @@ -254,7 +254,7 @@ func (m Model) updateGenerateWireguardFile(msg tea.Msg) (tea.Model, tea.Cmd) { } else if m.finishedFile && m.configFileError == nil { 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"]) } }