Skip to content

Commit 59b2acd

Browse files
committed
Simplify image tag command
1 parent be0c5be commit 59b2acd

3 files changed

Lines changed: 26 additions & 30 deletions

File tree

pkg/cmd/imagecmd_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ func TestTagCommandPostsEscapedSourceAndTarget(t *testing.T) {
9696
assert.Contains(t, string(output), "docker.io/library/myapp:latest")
9797
}
9898

99-
func TestTagCommandFallsBackToDockerWhenHypemanMisses(t *testing.T) {
99+
func TestTagCommandPropagatesNotFound(t *testing.T) {
100100
server := httptest.NewServer(http.NotFoundHandler())
101101
defer server.Close()
102102

103103
err := Command.Run(context.Background(), []string{
104104
"hypeman", "--base-url", server.URL,
105-
"tag", "not a valid image", "myapp:latest",
105+
"tag", "alpine:missing", "myapp:latest",
106106
})
107-
require.ErrorContains(t, err, "was not found in Hypeman; stage it from Docker")
107+
require.ErrorContains(t, err, "404")
108108
}
109109

110110
func TestTagCommandPropagatesNotReady(t *testing.T) {

pkg/cmd/pushcmd.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ func handleRemotePushTarget(ctx context.Context, cmd *cli.Command, target string
105105
return fmt.Errorf("get cached image %s: %w", target, err)
106106
}
107107

108-
// If Hypeman does not have the image, preserve the Docker-daemon fallback.
109108
if _, err := stageDockerImage(ctx, cmd, &client, target, target); err != nil {
110109
return fmt.Errorf("image %q was not found in Hypeman; stage it from Docker: %w", target, err)
111110
}

pkg/cmd/tag.go

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,11 @@ import (
1313
)
1414

1515
var tagCmd = cli.Command{
16-
Name: "tag",
17-
Usage: "Create a local image tag",
18-
ArgsUsage: "<source> <target>",
19-
Description: "Create a local image tag in Hypeman without pulling or converting the image.",
20-
Action: handleTag,
21-
HideHelpCommand: true,
22-
}
23-
24-
type tagImageRequest struct {
25-
Target string `json:"target"`
16+
Name: "tag",
17+
Usage: "Create a local image tag",
18+
ArgsUsage: "<source> <target>",
19+
Description: "Create a local image tag in Hypeman without pulling or converting the image.",
20+
Action: handleTag,
2621
}
2722

2823
func handleTag(ctx context.Context, cmd *cli.Command) error {
@@ -33,30 +28,32 @@ func handleTag(ctx context.Context, cmd *cli.Command) error {
3328

3429
source, target := args[0], args[1]
3530
client := hypeman.NewClient(getDefaultRequestOptions(cmd)...)
31+
res, err := tagImage(ctx, cmd, &client, source, target)
32+
if err != nil {
33+
return err
34+
}
35+
36+
return printTagResult(cmd, target, res)
37+
}
3638

37-
var opts []option.RequestOption
39+
func tagImage(ctx context.Context, cmd *cli.Command, client *hypeman.Client, source, target string) ([]byte, error) {
40+
var res []byte
41+
opts := []option.RequestOption{option.WithResponseBodyInto(&res)}
3842
if cmd.Root().Bool("debug") {
3943
opts = append(opts, debugMiddlewareOption)
4044
}
4145

42-
var res []byte
43-
opts = append(opts, option.WithResponseBodyInto(&res))
4446
path := "/images/" + url.PathEscape(source) + "/tag"
45-
if err := client.Post(ctx, path, tagImageRequest{Target: target}, nil, opts...); err != nil {
46-
if !isNotFoundError(err) {
47-
return err
48-
}
49-
50-
// Keep the Docker fallback for sources that have not been imported into
51-
// Hypeman yet. Once staged, the image is available under the requested
52-
// target and can be pushed through the normal cached-image flow.
53-
staged, stageErr := stageDockerImage(ctx, cmd, &client, source, target)
54-
if stageErr != nil {
55-
return fmt.Errorf("image %q was not found in Hypeman; stage it from Docker: %w", source, stageErr)
56-
}
57-
res = []byte(staged.RawJSON())
47+
body := struct {
48+
Target string `json:"target"`
49+
}{Target: target}
50+
if err := client.Post(ctx, path, body, nil, opts...); err != nil {
51+
return nil, err
5852
}
53+
return res, nil
54+
}
5955

56+
func printTagResult(cmd *cli.Command, target string, res []byte) error {
6057
format := cmd.Root().String("format")
6158
transform := cmd.Root().String("transform")
6259
result := gjson.ParseBytes(res)

0 commit comments

Comments
 (0)