generated from ViBiOh/goweb
-
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.
feat: Starting to add docker support
Signed-off-by: Vincent Boutour <[email protected]>
- Loading branch information
Showing
7 changed files
with
162 additions
and
22 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
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,120 @@ | ||
package docker | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/ViBiOh/httputils/v4/pkg/flags" | ||
"github.com/ViBiOh/httputils/v4/pkg/httpjson" | ||
"github.com/ViBiOh/httputils/v4/pkg/request" | ||
"github.com/ViBiOh/ketchup/pkg/model" | ||
"github.com/ViBiOh/ketchup/pkg/semver" | ||
) | ||
|
||
type authResponse struct { | ||
AccessToken string `json:"access_token"` | ||
} | ||
|
||
type tagsResponse struct { | ||
Tags []string `json:"tags"` | ||
} | ||
|
||
// App of package | ||
type App interface { | ||
LatestVersions(string, []string) (map[string]semver.Version, error) | ||
} | ||
|
||
// Config of package | ||
type Config struct { | ||
registryURL *string | ||
authURL *string | ||
username *string | ||
password *string | ||
} | ||
|
||
type app struct { | ||
registryURL string | ||
authURL string | ||
username string | ||
password string | ||
} | ||
|
||
// Flags adds flags for configuring package | ||
func Flags(fs *flag.FlagSet, prefix string, overrides ...flags.Override) Config { | ||
return Config{ | ||
registryURL: flags.New(prefix, "FlagPrefix").Name("Registry").Default(flags.Default("Registry", "https://index.docker.io/v2/", overrides)).Label("Registry API URL").ToString(fs), | ||
authURL: flags.New(prefix, "FlagPrefix").Name("OAuth URL").Default(flags.Default("Registry", "https://auth.docker.io/token", overrides)).Label("Registry API URL").ToString(fs), | ||
username: flags.New(prefix, "FlagPrefix").Name("Username").Default(flags.Default("Username", "", overrides)).Label("Registry Username").ToString(fs), | ||
password: flags.New(prefix, "FlagPrefix").Name("Password").Default(flags.Default("Password", "", overrides)).Label("Registry Password").ToString(fs), | ||
} | ||
} | ||
|
||
// New creates new App from Config | ||
func New(config Config) App { | ||
return app{ | ||
registryURL: strings.TrimSpace(*config.registryURL), | ||
authURL: strings.TrimSpace(*config.authURL), | ||
username: strings.TrimSpace(*config.username), | ||
password: strings.TrimSpace(*config.password), | ||
} | ||
} | ||
|
||
func (a app) LatestVersions(repository string, patterns []string) (map[string]semver.Version, error) { | ||
ctx := context.Background() | ||
|
||
versions, compiledPatterns, err := model.PreparePatternMatching(patterns) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to prepare pattern matching: %s", err) | ||
} | ||
|
||
bearerToken, err := a.login(ctx, repository) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to login to registry: %s", err) | ||
} | ||
|
||
resp, err := request.New().Get(fmt.Sprintf("%s%s/tags/list", a.registryURL, repository)).Header("Authorization", fmt.Sprintf("Bearer %s", bearerToken)).Send(ctx, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to fetch tags: %s", err) | ||
} | ||
|
||
var tagsContent tagsResponse | ||
if err := httpjson.Read(resp, &tagsContent, "docker-tags"); err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, tag := range tagsContent.Tags { | ||
tagVersion, err := semver.Parse(tag) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
model.CheckPatternsMatching(versions, compiledPatterns, tagVersion) | ||
} | ||
|
||
return versions, nil | ||
} | ||
|
||
func (a app) login(ctx context.Context, repository string) (string, error) { | ||
values := url.Values{} | ||
values.Set("grant_type", "password") | ||
values.Set("service", "registry.docker.io") | ||
values.Set("client_id", "ketchup") | ||
values.Set("scope", fmt.Sprintf("repository:%s:pull", repository)) | ||
values.Set("username", a.username) | ||
values.Set("password", a.password) | ||
|
||
resp, err := request.New().Post(a.authURL).Form(ctx, values) | ||
if err != nil { | ||
return "", fmt.Errorf("unable to authenticate to `%s`: %s", a.authURL, err) | ||
} | ||
|
||
var authContent authResponse | ||
if err := httpjson.Read(resp, &authContent, "docker-login"); err != nil { | ||
return "", err | ||
} | ||
|
||
return authContent.AccessToken, nil | ||
} |
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
Oops, something went wrong.