This repository has been archived by the owner on Jun 21, 2020. It is now read-only.
forked from opencontainers/umoci
/
tag.go
169 lines (138 loc) · 4.42 KB
/
tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* umoci: Umoci Modifies Open Containers' Images
* Copyright (C) 2016, 2017, 2018 SUSE LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"fmt"
"github.com/apex/log"
"github.com/openSUSE/umoci/oci/cas/dir"
"github.com/openSUSE/umoci/oci/casext"
"github.com/pkg/errors"
"github.com/urfave/cli"
"golang.org/x/net/context"
)
var tagAddCommand = cli.Command{
Name: "tag",
Usage: "creates a new tag in an OCI image",
ArgsUsage: `--image <image-path>[:<tag>] <new-tag>
Where "<image-path>" is the path to the OCI image, "<tag>" is the old name of
the tag and "<new-tag>" is the new name of the tag.`,
// tag modifies an image layout.
Category: "image",
Action: tagAdd,
Before: func(ctx *cli.Context) error {
if ctx.NArg() != 1 {
return errors.Errorf("invalid number of positional arguments: expected <new-tag>")
}
if ctx.Args().First() == "" {
return errors.Errorf("new tag cannot be empty")
}
if !casext.IsValidReferenceName(ctx.Args().First()) {
return errors.Errorf("new tag is an invalid reference")
}
ctx.App.Metadata["new-tag"] = ctx.Args().First()
return nil
},
}
func tagAdd(ctx *cli.Context) error {
imagePath := ctx.App.Metadata["--image-path"].(string)
fromName := ctx.App.Metadata["--image-tag"].(string)
tagName := ctx.App.Metadata["new-tag"].(string)
// Get a reference to the CAS.
engine, err := dir.Open(imagePath)
if err != nil {
return errors.Wrap(err, "open CAS")
}
engineExt := casext.NewEngine(engine)
defer engine.Close()
// Get original descriptor.
descriptorPaths, err := engineExt.ResolveReference(context.Background(), fromName)
if err != nil {
return errors.Wrap(err, "get descriptor")
}
if len(descriptorPaths) == 0 {
return errors.Errorf("tag not found: %s", fromName)
}
if len(descriptorPaths) != 1 {
// TODO: Handle this more nicely.
return errors.Errorf("tag is ambiguous: %s", fromName)
}
descriptor := descriptorPaths[0].Descriptor()
// Add it.
if err := engineExt.UpdateReference(context.Background(), tagName, descriptor); err != nil {
return errors.Wrap(err, "put reference")
}
log.Infof("created new tag: %q -> %q", tagName, fromName)
return nil
}
var tagRemoveCommand = cli.Command{
Name: "remove",
Aliases: []string{"rm"},
Usage: "removes a tag from an OCI image",
ArgsUsage: `--image <image-path>[:<tag>]
Where "<image-path>" is the path to the OCI image, "<tag>" is the name of the
tag to remove.`,
// tag modifies an image layout.
Category: "image",
Action: tagRemove,
}
func tagRemove(ctx *cli.Context) error {
imagePath := ctx.App.Metadata["--image-path"].(string)
tagName := ctx.App.Metadata["--image-tag"].(string)
// Get a reference to the CAS.
engine, err := dir.Open(imagePath)
if err != nil {
return errors.Wrap(err, "open CAS")
}
engineExt := casext.NewEngine(engine)
defer engine.Close()
// Remove it.
if err := engineExt.DeleteReference(context.Background(), tagName); err != nil {
return errors.Wrap(err, "delete reference")
}
log.Infof("removed tag: %s", tagName)
return nil
}
var tagListCommand = cli.Command{
Name: "list",
Aliases: []string{"ls"},
Usage: "lists the set of tags in an OCI layout",
ArgsUsage: `--layout <image-path>
Where "<image-path>" is the path to the OCI layout.
Gives the full list of tags in an OCI layout, with each tag name on a single
line. See umoci-stat(1) to get more information about each tagged image.`,
// tag modifies an image layout.
Category: "layout",
Action: tagList,
}
func tagList(ctx *cli.Context) error {
imagePath := ctx.App.Metadata["--image-path"].(string)
// Get a reference to the CAS.
engine, err := dir.Open(imagePath)
if err != nil {
return errors.Wrap(err, "open CAS")
}
engineExt := casext.NewEngine(engine)
defer engine.Close()
names, err := engineExt.ListReferences(context.Background())
if err != nil {
return errors.Wrap(err, "list references")
}
for _, name := range names {
fmt.Println(name)
}
return nil
}