Skip to content

Commit 3d196b6

Browse files
committed
feat(registry): add configurable timeout for tag deletion
1 parent 4995e6c commit 3d196b6

File tree

7 files changed

+785
-4
lines changed

7 files changed

+785
-4
lines changed

core/testing.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,13 @@ func ExecBeforeCmdArgs(args []string) BeforeFunc {
674674
}
675675
}
676676

677+
// ExecBeforeCmdWithResult executes the given command and returns its result.
678+
func ExecBeforeCmdWithResult(ctx *BeforeFuncCtx, cmd string) any {
679+
args := cmdToArgs(ctx.Meta, cmd)
680+
ctx.Logger.Debugf("ExecBeforeCmd: args=%s\n", args)
681+
return ctx.ExecuteCmd(args)
682+
}
683+
677684
// ExecAfterCmd executes the given before command.
678685
func ExecAfterCmd(cmd string) AfterFunc {
679686
return func(ctx *AfterFuncCtx) error {

internal/namespaces/registry/v1/custom.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func GetCommands() *core.Commands {
2727
registryInstallDockerHelperCommand(),
2828
))
2929

30+
cmds.MustFind("registry", "tag", "delete").Override(tagDeleteBuilder)
3031
cmds.MustFind("registry", "tag", "get").Override(tagGetBuilder)
3132
cmds.MustFind("registry", "tag", "list").Override(tagListBuilder)
3233

internal/namespaces/registry/v1/custom_tag.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package registry
33
import (
44
"context"
55
"fmt"
6+
"reflect"
7+
"time"
68

79
"github.com/fatih/color"
810
"github.com/scaleway/scaleway-cli/v2/core"
@@ -25,7 +27,7 @@ var (
2527
}
2628
)
2729

28-
type customTag struct {
30+
type CustomTag struct {
2931
registry.Tag
3032
FullName string
3133
}
@@ -59,7 +61,7 @@ func tagGetBuilder(c *core.Command) *core.Command {
5961
return getTagResp, nil
6062
}
6163

62-
res := customTag{
64+
res := CustomTag{
6365
Tag: *tag,
6466
FullName: fmt.Sprintf("%s/%s:%s", namespace.Endpoint, image.Name, tag.Name),
6567
}
@@ -112,9 +114,9 @@ func tagListBuilder(c *core.Command) *core.Command {
112114
return listTagResp, err
113115
}
114116

115-
var customRes []customTag
117+
var customRes []CustomTag
116118
for _, tag := range listTagResp.([]*registry.Tag) {
117-
customRes = append(customRes, customTag{
119+
customRes = append(customRes, CustomTag{
118120
Tag: *tag,
119121
FullName: fmt.Sprintf("%s/%s:%s",
120122
namespace.Endpoint,
@@ -129,3 +131,29 @@ func tagListBuilder(c *core.Command) *core.Command {
129131

130132
return c
131133
}
134+
135+
type customTagDeleteArgs struct {
136+
*registry.DeleteTagRequest
137+
timeout time.Duration
138+
}
139+
140+
func tagDeleteBuilder(c *core.Command) *core.Command {
141+
c.ArgsType = reflect.TypeOf(customTagDeleteArgs{})
142+
c.ArgSpecs.AddBefore("force", &core.ArgSpec{
143+
Name: "timeout",
144+
Short: "Maximum time to handle the request",
145+
Required: false,
146+
Positional: false,
147+
})
148+
149+
c.Interceptor = func(ctx context.Context, argsI any, runner core.CommandRunner) (any, error) {
150+
args := argsI.(*customTagDeleteArgs)
151+
152+
ctxWithTimeout, cancel := context.WithTimeout(ctx, args.timeout)
153+
defer cancel()
154+
155+
return runner(ctxWithTimeout, args.DeleteTagRequest)
156+
}
157+
158+
return c
159+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package registry_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/scaleway/scaleway-cli/v2/core"
8+
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/registry/v1"
9+
"github.com/scaleway/scaleway-cli/v2/internal/testhelpers"
10+
)
11+
12+
func Test_RegistryTagDelete(t *testing.T) {
13+
registryNamespaceMetaKey := "RegistryNamespace"
14+
helloWorldImage := "hello-world:latest"
15+
helloWorldImageMetaKey := "HelloWorldImage"
16+
tagIDMetaKey := "TagID"
17+
18+
t.Run("timeout-ok", core.Test(&core.TestConfig{
19+
Commands: registry.GetCommands(),
20+
BeforeFunc: core.BeforeFuncCombine(
21+
core.ExecStoreBeforeCmd(
22+
registryNamespaceMetaKey,
23+
fmt.Sprintf("scw registry namespace create name=%s is-public=false",
24+
core.GetRandomName("test-rg-tag-delete"),
25+
),
26+
),
27+
core.BeforeFuncWhenUpdatingCassette(
28+
core.BeforeFuncCombine(
29+
core.ExecBeforeCmd("scw registry login"),
30+
testhelpers.PushRegistryImage(helloWorldImage, registryNamespaceMetaKey),
31+
),
32+
),
33+
testhelpers.StoreImageIdentifierInMeta(
34+
registryNamespaceMetaKey,
35+
helloWorldImage,
36+
helloWorldImageMetaKey,
37+
),
38+
testhelpers.StoreTagIDInMeta(registryNamespaceMetaKey, helloWorldImage, tagIDMetaKey),
39+
),
40+
Cmd: fmt.Sprintf("scw registry tag delete {{ .%s }} timeout=1s", tagIDMetaKey),
41+
Check: core.TestCheckCombine(
42+
core.TestCheckGolden(),
43+
core.TestCheckExitCode(0),
44+
),
45+
AfterFunc: func(ctx *core.AfterFuncCtx) error {
46+
return core.ExecAfterCmd(
47+
fmt.Sprintf(
48+
"scw registry namespace delete {{ .%s.ID }}",
49+
registryNamespaceMetaKey,
50+
),
51+
)(
52+
ctx,
53+
)
54+
},
55+
}))
56+
}

0 commit comments

Comments
 (0)