This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POC: Make authenticated request to display "my repositories" (#15)
- Loading branch information
1 parent
e5db269
commit b9fe20d
Showing
17 changed files
with
422 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package data_user | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/imroc/req/v3" | ||
) | ||
|
||
type RepositoryPage struct { | ||
Count int `json:"count"` | ||
NextUrl string `json:"next"` | ||
PreviousUrl string `json:"previous"` | ||
Repositories []Repository `json:"results"` | ||
} | ||
|
||
type Repository struct { | ||
Name string `json:"name"` | ||
Namespace string `json:"namespace"` | ||
RepositoryType string `json:"repository_type"` | ||
Status int `json:"status"` | ||
IsPrivate bool `json:"is_private"` | ||
StarCount int `json:"star_count"` | ||
PullCount int `json:"pull_count"` | ||
UpdatedAt time.Time `json:"last_updated"` | ||
CreatedAt time.Time `json:"date_registered"` | ||
Affiliation string `json:"affiliation"` | ||
} | ||
|
||
func (data Repository) GetUrl() string { | ||
return fmt.Sprintf("https://hub.docker.com/repository/docker/%s/%s", data.Namespace, data.Name) | ||
} | ||
|
||
func FetchRepositories() ([]Repository, error) { | ||
client := req.C(). | ||
SetTimeout(5 * time.Second) | ||
|
||
var repositoryPage RepositoryPage | ||
if os.Getenv("DOCKER_USERNAME") == "" { | ||
return nil, nil | ||
} | ||
repo_url := fmt.Sprintf("https://hub.docker.com/v2/repositories/%s", os.Getenv("DOCKER_USERNAME")) | ||
resp, err := client.R(). | ||
SetHeader("Authorization", fmt.Sprintf("Bearer %s", os.Getenv("DOCKER_BEARER"))). | ||
SetResult(&repositoryPage). | ||
EnableDump(). | ||
SetQueryParam("page_size", "100"). | ||
SetQueryParam("page", "1"). | ||
SetQueryParam("ordering", "last_updated"). | ||
Get(repo_url) | ||
if err != nil { | ||
log.Println("error:", err) | ||
log.Println("raw content:") | ||
log.Println(resp.Dump()) | ||
return nil, err | ||
} | ||
|
||
if resp.IsSuccess() { | ||
return repositoryPage.Repositories, err | ||
} | ||
|
||
log.Println("unknown status", resp.Status) | ||
log.Println("raw content:") | ||
log.Println(resp.Dump()) // Record raw content when server returned unknown status code. | ||
|
||
return repositoryPage.Repositories, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
package data | ||
|
||
import "time" | ||
|
||
type RowData interface { | ||
GetRepoNameWithOwner() string | ||
GetUrl() string | ||
GetLastUpdate() time.Time | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1 @@ | ||
package repository | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/charmbracelet/lipgloss" | ||
"github.com/victorbersy/docker-hub-cli/internal/data" | ||
"github.com/victorbersy/docker-hub-cli/internal/ui/components/table" | ||
"github.com/victorbersy/docker-hub-cli/internal/utils" | ||
) | ||
|
||
type Repository struct { | ||
Data data.RepositoryData | ||
} | ||
|
||
func (repo Repository) ToTableRow() table.Row { | ||
return table.Row{ | ||
repo.renderName(), | ||
repo.renderLabels(), | ||
repo.renderPublisher(), | ||
repo.renderstatsDownloads(), | ||
repo.renderstatsStars(), | ||
repo.renderLastUpdate(), | ||
repo.renderDescription(), | ||
} | ||
} | ||
|
||
func (repo Repository) renderName() string { | ||
return repo.Data.Name | ||
} | ||
|
||
func (repo Repository) renderLabels() string { | ||
labels := []string{} | ||
for _, label := range repo.Data.Labels { | ||
if label.Enabled { | ||
labels = append(labels, lipgloss.NewStyle().Foreground(label.Color).Width(3).Render(label.Glyph)) | ||
} else { | ||
labels = append(labels, lipgloss.NewStyle().Width(3).Render("")) | ||
} | ||
} | ||
return lipgloss.JoinHorizontal( | ||
lipgloss.Top, | ||
labels..., | ||
) | ||
} | ||
|
||
func (repo Repository) renderPublisher() string { | ||
return lipgloss.NewStyle(). | ||
Render(repo.Data.Publisher.Name) | ||
} | ||
|
||
func (repo Repository) renderstatsDownloads() string { | ||
return lipgloss.NewStyle(). | ||
Render(repo.Data.PullCount) | ||
} | ||
|
||
func (repo Repository) renderstatsStars() string { | ||
return lipgloss.NewStyle(). | ||
Render(fmt.Sprint(repo.Data.StarCount)) | ||
} | ||
|
||
func (repo Repository) renderDescription() string { | ||
return lipgloss.NewStyle(). | ||
Render(repo.Data.Description) | ||
} | ||
|
||
func (repo Repository) renderLastUpdate() string { | ||
return lipgloss.NewStyle(). | ||
Render(utils.TimeElapsed(repo.Data.Updated_at)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package repository_search | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/charmbracelet/lipgloss" | ||
data_search "github.com/victorbersy/docker-hub-cli/internal/data/search" | ||
"github.com/victorbersy/docker-hub-cli/internal/ui/components/table" | ||
"github.com/victorbersy/docker-hub-cli/internal/utils" | ||
) | ||
|
||
type Repository struct { | ||
Data data_search.Repository | ||
} | ||
|
||
func (repo Repository) ToTableRow() table.Row { | ||
return table.Row{ | ||
repo.renderName(), | ||
repo.renderLabels(), | ||
repo.renderPublisher(), | ||
repo.renderstatsDownloads(), | ||
repo.renderstatsStars(), | ||
repo.renderLastUpdate(), | ||
repo.renderDescription(), | ||
} | ||
} | ||
|
||
func (repo Repository) renderName() string { | ||
return repo.Data.Name | ||
} | ||
|
||
func (repo Repository) renderLabels() string { | ||
labels := []string{} | ||
for _, label := range repo.Data.Labels { | ||
if label.Enabled { | ||
labels = append(labels, lipgloss.NewStyle().Foreground(label.Color).Width(3).Render(label.Glyph)) | ||
} else { | ||
labels = append(labels, lipgloss.NewStyle().Width(3).Render("")) | ||
} | ||
} | ||
return lipgloss.JoinHorizontal( | ||
lipgloss.Top, | ||
labels..., | ||
) | ||
} | ||
|
||
func (repo Repository) renderPublisher() string { | ||
return lipgloss.NewStyle(). | ||
Render(repo.Data.Publisher.Name) | ||
} | ||
|
||
func (repo Repository) renderstatsDownloads() string { | ||
return lipgloss.NewStyle(). | ||
Render(repo.Data.PullCount) | ||
} | ||
|
||
func (repo Repository) renderstatsStars() string { | ||
return lipgloss.NewStyle(). | ||
Render(fmt.Sprint(repo.Data.StarCount)) | ||
} | ||
|
||
func (repo Repository) renderDescription() string { | ||
return lipgloss.NewStyle(). | ||
Render(repo.Data.Description) | ||
} | ||
|
||
func (repo Repository) renderLastUpdate() string { | ||
return lipgloss.NewStyle(). | ||
Render(utils.TimeElapsed(repo.Data.UpdatedAt)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package repository_user | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/charmbracelet/lipgloss" | ||
data_user "github.com/victorbersy/docker-hub-cli/internal/data/user" | ||
"github.com/victorbersy/docker-hub-cli/internal/ui/components/table" | ||
"github.com/victorbersy/docker-hub-cli/internal/ui/constants" | ||
"github.com/victorbersy/docker-hub-cli/internal/utils" | ||
) | ||
|
||
type Repository struct { | ||
Data data_user.Repository | ||
} | ||
|
||
func (repo Repository) ToTableRow() table.Row { | ||
return table.Row{ | ||
repo.renderName(), | ||
repo.renderIsPrivate(), | ||
repo.renderstatsDownloads(), | ||
repo.renderstatsStars(), | ||
repo.renderLastUpdate(), | ||
repo.renderCreatedAt(), | ||
} | ||
} | ||
|
||
func (repo Repository) renderName() string { | ||
return repo.Data.Name | ||
} | ||
|
||
func (repo Repository) renderIsPrivate() string { | ||
if repo.Data.IsPrivate { | ||
return lipgloss.NewStyle().Foreground(lipgloss.Color("#EF476F")).Render(constants.GlyphPrivate) | ||
} else { | ||
return lipgloss.NewStyle().Foreground(lipgloss.Color("#06D6A0")).Render(constants.GlyphPublic) | ||
} | ||
} | ||
|
||
func (repo Repository) renderstatsDownloads() string { | ||
return lipgloss.NewStyle(). | ||
Render(fmt.Sprint(repo.Data.PullCount)) | ||
} | ||
|
||
func (repo Repository) renderstatsStars() string { | ||
return lipgloss.NewStyle(). | ||
Render(fmt.Sprint(repo.Data.StarCount)) | ||
} | ||
|
||
func (repo Repository) renderLastUpdate() string { | ||
return lipgloss.NewStyle(). | ||
Render(utils.TimeElapsed(repo.Data.UpdatedAt)) | ||
} | ||
|
||
func (repo Repository) renderCreatedAt() string { | ||
return lipgloss.NewStyle(). | ||
Render(utils.TimeElapsed(repo.Data.CreatedAt)) | ||
} |
Oops, something went wrong.