Skip to content

Commit d43fb40

Browse files
committed
feat: Allow using multiple tags
Also allow pointing to sha256 digest when --new-tag is used. Fixes #16
1 parent 620dd01 commit d43fb40

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

handler.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const (
3737
artifactsDbName = "artifacts.db"
3838
)
3939

40-
func indexAndPush(ctx context.Context, repo string, tag string, newTag string, registryUrl string, authToken string) (string, error) {
40+
func indexAndPush(ctx context.Context, repo string, tag string, newTags []string, registryUrl string, authToken string) (string, error) {
4141
ctx = context.WithValue(ctx, "RegistryURL", registryUrl)
4242

4343
registry, err := registryutils.Init(ctx, registryUrl, authToken)
@@ -86,11 +86,18 @@ func indexAndPush(ctx context.Context, repo string, tag string, newTag string, r
8686
}
8787
ctx = context.WithValue(ctx, "SOCIIndexDigest", indexDescriptor.Digest.String())
8888

89-
err = registry.Push(ctx, sociStore, *indexDescriptor, repo, newTag)
89+
err = registry.Push(ctx, sociStore, *indexDescriptor, repo)
9090
if err != nil {
9191
return logAndReturnError(ctx, PushFailedMessage, err)
9292
}
9393

94+
for _, newTag := range newTags {
95+
err = registry.Tag(ctx, *indexDescriptor, repo, newTag)
96+
if err != nil {
97+
return logAndReturnError(ctx, PushFailedMessage, err)
98+
}
99+
}
100+
94101
log.Info(ctx, BuildAndPushSuccessMessage)
95102
}
96103
return BuildAndPushSuccessMessage, nil

main.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ var (
2020
)
2121

2222
var (
23-
auth string
24-
newTag string
23+
auth string
24+
newTags []string
2525
)
2626

2727
func parseImageDesc(desc string) (repo, tag, registry string, err error) {
@@ -52,8 +52,8 @@ func main() {
5252
os.Exit(1)
5353
}
5454

55-
if strings.Contains(tag, ":") {
56-
log.Error(ctx, "Tag cannot be a digest", nil)
55+
if strings.Contains(tag, ":") && len(newTags) == 0 {
56+
log.Error(ctx, "Tag cannot be a digest without --new-tag", nil)
5757
os.Exit(1)
5858
}
5959

@@ -62,21 +62,21 @@ func main() {
6262
os.Exit(1)
6363
}
6464

65-
if newTag == "" {
66-
newTag = tag
65+
if len(newTags) == 0 {
66+
newTags = append(newTags, tag)
6767
}
6868

69-
log.Info(ctx, fmt.Sprintf("Indexing %s:%s and pushing with tag %s to %s", repo, tag, newTag, registry))
69+
log.Info(ctx, fmt.Sprintf("Indexing %s:%s and pushing with tags %s to %s", repo, tag, newTags, registry))
7070

71-
_, err = indexAndPush(ctx, repo, tag, newTag, registry, auth)
71+
_, err = indexAndPush(ctx, repo, tag, newTags, registry, auth)
7272
if err != nil {
7373
os.Exit(1)
7474
}
7575
},
7676
}
7777

7878
rootCmd.Flags().StringVarP(&auth, "auth", "a", "", "Registry authentication token (usually USER:PASSWORD)")
79-
rootCmd.Flags().StringVarP(&newTag, "new-tag", "t", "", "Push indexed image with this tag")
79+
rootCmd.Flags().StringArrayVarP(&newTags, "new-tag", "t", nil, "Push indexed image with this tag")
8080

8181
if err := rootCmd.Execute(); err != nil {
8282
_, _ = fmt.Fprintln(os.Stderr, err)

utils/registry/registry.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (registry *Registry) Pull(ctx context.Context, repositoryName string, sociS
9898
// Push a OCI artifact to remote registry
9999
// descriptor: ocispec Descriptor of the artifact
100100
// ociStore: the local OCI store
101-
func (registry *Registry) Push(ctx context.Context, sociStore *store.SociStore, indexDesc ocispec.Descriptor, repositoryName, tag string) error {
101+
func (registry *Registry) Push(ctx context.Context, sociStore *store.SociStore, indexDesc ocispec.Descriptor, repositoryName string) error {
102102
log.Info(ctx, "Pushing artifact")
103103

104104
repo, err := registry.registry.Repository(ctx, repositoryName)
@@ -116,12 +116,19 @@ func (registry *Registry) Push(ctx context.Context, sociStore *store.SociStore,
116116
return err
117117
}
118118

119-
if tag != "" {
120-
log.Info(ctx, fmt.Sprintf("Tagging index with %s", tag))
121-
err = repo.Tag(ctx, indexDesc, tag)
122-
if err != nil {
123-
return fmt.Errorf("failed to tag artifact: %w", err)
124-
}
119+
return nil
120+
}
121+
122+
func (registry *Registry) Tag(ctx context.Context, indexDesc ocispec.Descriptor, repositoryName, tag string) error {
123+
repo, err := registry.registry.Repository(ctx, repositoryName)
124+
if err != nil {
125+
return err
126+
}
127+
128+
log.Info(ctx, fmt.Sprintf("Tagging index with %s", tag))
129+
err = repo.Tag(ctx, indexDesc, tag)
130+
if err != nil {
131+
return fmt.Errorf("failed to tag artifact: %w", err)
125132
}
126133

127134
return nil

0 commit comments

Comments
 (0)