-
Notifications
You must be signed in to change notification settings - Fork 1
feat: 'release pull' command implementation #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+178
−3
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0347381
Initial command to pull a release image
nickschuch 11ce339
Small quality of life improvements
nterbogt d7819e2
Bad comment.
nterbogt 7672f9a
More copypasta
nterbogt b0decc2
Move the writer out of the loop
nterbogt 3fa3a51
Include the command in the parent help. Consider removing all the par…
nterbogt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,40 @@ | ||
| package pull | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/skpr/cli/containers/docker" | ||
| v1pull "github.com/skpr/cli/internal/command/release/pull" | ||
| ) | ||
|
|
||
| var ( | ||
| cmdLong = `Pulls the packaged container images for a release.` | ||
|
|
||
| cmdExample = ` | ||
| # Pull the packaged container images for a release. | ||
| skpr release pull VERSION` | ||
| ) | ||
|
|
||
| // NewCommand creates a new cobra.Command for 'pull' sub command | ||
| func NewCommand(clientId docker.DockerClientId) *cobra.Command { | ||
| command := v1pull.Command{} | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "pull <version>", | ||
| Args: cobra.ExactArgs(1), | ||
| DisableFlagsInUseLine: true, | ||
| Short: "Pull release images.", | ||
| Long: cmdLong, | ||
| Example: cmdExample, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| command.Params.Name = args[0] | ||
| command.ClientId = clientId | ||
|
|
||
| return command.Run(cmd.Context()) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&command.Params.Service, "service", command.Params.Service, "A specific service image to pull") | ||
|
|
||
| return cmd | ||
| } |
This file contains hidden or 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,129 @@ | ||
| package pull | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log/slog" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/gosuri/uilive" | ||
| "github.com/pkg/errors" | ||
| "github.com/skpr/api/pb" | ||
|
|
||
| "github.com/skpr/cli/containers/buildpack/utils/aws/ecr" | ||
| "github.com/skpr/cli/containers/docker" | ||
| "github.com/skpr/cli/containers/docker/types" | ||
| "github.com/skpr/cli/internal/client" | ||
| skprlog "github.com/skpr/cli/internal/log" | ||
| ) | ||
|
|
||
| // Command to pull release images. | ||
| type Command struct { | ||
| Params Params | ||
| ClientId docker.DockerClientId | ||
| } | ||
|
|
||
| // Params provided to this command. | ||
| type Params struct { | ||
| Name string | ||
| Service string | ||
| } | ||
|
|
||
| // Run the command. | ||
| func (cmd *Command) Run(ctx context.Context) error { | ||
| ctx, client, err := client.New(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| prettyHandler := skprlog.NewHandler(os.Stderr, &slog.HandlerOptions{ | ||
| Level: slog.LevelInfo, | ||
| AddSource: false, | ||
| ReplaceAttr: nil, | ||
| }) | ||
|
|
||
| logger := slog.New(prettyHandler) | ||
|
|
||
| release, err := client.Release().Info(ctx, &pb.ReleaseInfoRequest{ | ||
| Name: cmd.Params.Name, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("could not get release: %w", err) | ||
| } | ||
|
|
||
| writer := uilive.New() | ||
| writer.Start() | ||
| defer writer.Stop() | ||
|
|
||
| for _, image := range release.Images { | ||
| if cmd.Params.Service != "" { | ||
| if image.Name != cmd.Params.Service { | ||
| continue | ||
| } | ||
| } | ||
| repository, tag, err := ParseImage(image.URI) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to parse image reference") | ||
| } | ||
|
|
||
| auth := types.Auth{ | ||
| Username: client.Credentials.Username, | ||
| Password: client.Credentials.Password, | ||
| Session: client.Credentials.Session, | ||
| } | ||
|
|
||
| // @todo, Consider abstracting this if another registry + credentials pair is required. | ||
| if ecr.IsRegistry(repository) { | ||
| auth, err = ecr.UpgradeAuth(ctx, repository, auth) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to upgrade AWS ECR authentication") | ||
| } | ||
| } | ||
|
|
||
| c, err := docker.NewClientFromUserConfig(auth, cmd.ClientId) | ||
| if err != nil { | ||
| return errors.Wrap(err, "failed to create Docker client") | ||
| } | ||
|
|
||
| logger.Info(fmt.Sprintf("Pulling: %s", image.URI)) | ||
|
|
||
| err = c.PullImage(ctx, repository, tag, writer) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| logger.Info(fmt.Sprintf("Successfully pulled image: %s", image.URI)) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func ParseImage(image string) (repository string, tag string, err error) { | ||
| if image == "" { | ||
| return "", "", fmt.Errorf("image reference is empty") | ||
| } | ||
|
|
||
| // Reject digest references explicitly | ||
| if strings.Contains(image, "@") { | ||
| return "", "", fmt.Errorf("digest references are not supported") | ||
| } | ||
|
|
||
| // Split on the last colon to preserve registry ports | ||
| lastColon := strings.LastIndex(image, ":") | ||
| if lastColon == -1 { | ||
| return "", "", fmt.Errorf("image reference does not contain a tag") | ||
| } | ||
|
|
||
| repository = image[:lastColon] | ||
| tag = image[lastColon+1:] | ||
|
|
||
| if repository == "" { | ||
| return "", "", fmt.Errorf("repository is empty") | ||
| } | ||
| if tag == "" { | ||
| return "", "", fmt.Errorf("tag is empty") | ||
| } | ||
|
|
||
| return repository, tag, nil | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.