Skip to content

Commit

Permalink
Add support for multiple search repositories
Browse files Browse the repository at this point in the history
Up until now, whalebrew restricts its searches to https://hub.docker.com/u/whalebrew

This commit introduces the ability to customize search organization
under docker hub.

To customize it, place a file like

```
registries:
- dockerHub:
    owner: whalebrew
```

You can add as many search repositories as desired, all of them will be
searched.

Currently, there is no authentication to the search repositories. Only
public images will be retrieved.

A step forward for whaleebrew tap discussed in #102
  • Loading branch information
tjamet committed Oct 25, 2020
1 parent ba15e21 commit ead2fdc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 24 deletions.
55 changes: 31 additions & 24 deletions cmd/search.go
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"

"github.com/spf13/cobra"
"github.com/whalebrew/whalebrew/config"
)

type imageResult struct {
Expand All @@ -30,31 +31,37 @@ var searchCommand = &cobra.Command{
if len(args) > 1 {
return fmt.Errorf("Only one search term is supported")
}
for _, registry := range config.GetConfig().Registries {
if registry.DockerHub != nil {
params := url.Values{}
params.Set("page_size", "100")
params.Set("ordering", "last_updated")
if len(args) > 0 {
params.Set("name", args[0])
}
u := url.URL{
Scheme: "https",
Host: "hub.docker.com",
Path: fmt.Sprintf("/v2/repositories/%s/", registry.DockerHub.Owner),
RawQuery: params.Encode(),
}

params := url.Values{}
params.Set("page_size", "100")
params.Set("ordering", "last_updated")
if len(args) > 0 {
params.Set("name", args[0])
}
u := url.URL{
Scheme: "https",
Host: "hub.docker.com",
Path: "/v2/repositories/whalebrew/",
RawQuery: params.Encode(),
}

r, err := http.Get(u.String())
if err != nil {
return err
}
answer := searchAnswer{}
err = json.NewDecoder(r.Body).Decode(&answer)
if err != nil {
return err
}
for _, image := range answer.Results {
fmt.Printf("%s/%s\n", image.User, image.Name)
r, err := http.Get(u.String())
if err != nil {
return err
}
answer := searchAnswer{}
err = json.NewDecoder(r.Body).Decode(&answer)
if err != nil {
return err
}
for _, image := range answer.Results {
fmt.Printf("%s/%s\n", image.User, image.Name)
}
} else {
err := fmt.Errorf("Unsupported registry %v. Only docker revistry is supported", registry)
return err
}
}
return nil
},
Expand Down
35 changes: 35 additions & 0 deletions config/config.go
@@ -0,0 +1,35 @@
package config

import (
"os"
"path/filepath"

"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)

type DockerHubRegistry struct {
Owner string `yaml:"owner"`
}

type Registry struct {
DockerHub *DockerHubRegistry `yaml:"dockerHub"`
}

type Config struct {
Registries []Registry `yaml:"registries"`
}

func GetConfig() Config {
configPath := filepath.Join(viper.GetString("config_dir"), "config.yaml")
fd, err := os.Open(configPath)
c := Config{}
if err == nil {
defer fd.Close()
yaml.NewDecoder(fd).Decode(&c)
}
if len(c.Registries) == 0 {
c.Registries = []Registry{{DockerHub: &DockerHubRegistry{Owner: "whalebrew"}}}
}
return c
}

0 comments on commit ead2fdc

Please sign in to comment.