Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions pkg/commands/dockerhub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package commands

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)

type DockerHubResult struct {
RepoName string `json:"repo_name"`
ShortDescription string `json:"short_description"`
StarCount int `json:"star_count"`
PullCount int `json:"pull_count"`
IsOfficial bool `json:"is_official"`
IsAutomated bool `json:"is_automated"`
}

type dockerHubSearchResponse struct {
Count int `json:"count"`
Results []DockerHubResult `json:"results"`
}

func SearchDockerHub(query string, pageSize int) ([]DockerHubResult, error) {
if pageSize <= 0 {
pageSize = 25
}

apiURL := fmt.Sprintf(
"https://hub.docker.com/v2/search/repositories/?query=%s&page_size=%d",
url.QueryEscape(query),
pageSize,
)

client := &http.Client{Timeout: 15 * time.Second}

req, err := http.NewRequest(http.MethodGet, apiURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to build Docker Hub request: %w", err)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", "lazydocker")

resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("Docker Hub search failed: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Docker Hub returned non-OK status: %s", resp.Status)
}

var searchResp dockerHubSearchResponse
if err := json.NewDecoder(resp.Body).Decode(&searchResp); err != nil {
return nil, fmt.Errorf("failed to parse Docker Hub response: %w", err)
}

return searchResp.Results, nil
}
92 changes: 92 additions & 0 deletions pkg/gui/dockerhub_panel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package gui

import (
"fmt"

"github.com/fatih/color"
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazydocker/pkg/commands"
"github.com/jesseduffield/lazydocker/pkg/gui/types"
"github.com/jesseduffield/lazydocker/pkg/utils"
)

func (gui *Gui) handleDockerHubSearch() error {
return gui.createPromptPanel(gui.Tr.DockerHubSearchTitle, func(g *gocui.Gui, v *gocui.View) error {
query := gui.trimmedContent(v)
if query == "" {
return nil
}
return gui.searchAndShowDockerHubResults(query)
})
}

func (gui *Gui) searchAndShowDockerHubResults(query string) error {
return gui.WithWaitingStatus(gui.Tr.PullingImageStatus, func() error {
results, err := commands.SearchDockerHub(query, 25)
if err != nil {
return gui.createErrorPanel(fmt.Sprintf("Docker Hub search failed: %s", err.Error()))
}

gui.g.Update(func(g *gocui.Gui) error {
return gui.showDockerHubResults(results)
})
return nil
})
}

func (gui *Gui) showDockerHubResults(results []commands.DockerHubResult) error {
if len(results) == 0 {
return gui.createErrorPanel(gui.Tr.DockerHubNoResults)
}

menuItems := make([]*types.MenuItem, 0, len(results))
for _, r := range results {
r := r

officialBadge := ""
if r.IsOfficial {
officialBadge = utils.ColoredString("[Official] ", color.FgGreen)
}

name := officialBadge + utils.ColoredString(r.RepoName, color.FgCyan)

description := r.ShortDescription
if len(description) > 60 {
description = description[:57] + "..."
}

stats := fmt.Sprintf(
"⭐ %s ⬇ %s",
formatCount(r.StarCount),
formatCount(r.PullCount),
)

menuItems = append(menuItems, &types.MenuItem{
LabelColumns: []string{name, description, stats},
OnPress: func() error {
return gui.handlePullDockerHubImage(r)
},
})
}

return gui.Menu(CreateMenuOptions{
Title: "Docker Hub Results",
Items: menuItems,
})
}

func (gui *Gui) handlePullDockerHubImage(result commands.DockerHubResult) error {
cmd := gui.OSCommand.ExecutableFromString(fmt.Sprintf("docker pull %s", result.RepoName))
return gui.runSubprocessWithMessage(cmd, fmt.Sprintf("Pulling %s from Docker Hub…", result.RepoName))
}

func formatCount(n int) string {
switch {
case n >= 1_000_000:
return fmt.Sprintf("%.1fM", float64(n)/1_000_000)
case n >= 1_000:
return fmt.Sprintf("%.1fK", float64(n)/1_000)
default:
return fmt.Sprintf("%d", n)
}
}
7 changes: 7 additions & 0 deletions pkg/gui/keybindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
Handler: gui.handleImagesBulkCommand,
Description: gui.Tr.ViewBulkCommands,
},
{
ViewName: "images",
Key: 'p',
Modifier: gocui.ModNone,
Handler: wrappedHandler(gui.handleDockerHubSearch),
Description: gui.Tr.DockerHubSearch,
},
{
ViewName: "volumes",
Key: 'c',
Expand Down
12 changes: 12 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ type TranslationSet struct {
FocusImages string
FocusVolumes string
FocusNetworks string

DockerHubSearch string
DockerHubSearchTitle string
DockerHubNoResults string
PullImage string
PullingImageStatus string
}

func englishSet() TranslationSet {
Expand Down Expand Up @@ -279,5 +285,11 @@ func englishSet() TranslationSet {
FocusImages: "focus images panel",
FocusVolumes: "focus volumes panel",
FocusNetworks: "focus networks panel",

DockerHubSearch: "search Docker Hub",
DockerHubSearchTitle: "Search Docker Hub:",
DockerHubNoResults: "No results found on Docker Hub",
PullImage: "pull image from Docker Hub",
PullingImageStatus: "pulling image",
}
}