-
Notifications
You must be signed in to change notification settings - Fork 162
/
spec.go
138 lines (118 loc) · 4.11 KB
/
spec.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
/*
Copyright The ORAS Authors.
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 option
import (
"fmt"
"strings"
"github.com/spf13/pflag"
"oras.land/oras-go/v2"
oerrors "oras.land/oras/cmd/oras/internal/errors"
)
const (
ImageSpecV1_1 = "v1.1"
ImageSpecV1_0 = "v1.0"
)
const (
DistributionSpecReferrersTagV1_1 = "v1.1-referrers-tag"
DistributionSpecReferrersAPIV1_1 = "v1.1-referrers-api"
)
// ImageSpec option struct which implements pflag.Value interface.
type ImageSpec struct {
Flag string
PackVersion oras.PackManifestVersion
}
// Set validates and sets the flag value from a string argument.
func (is *ImageSpec) Set(value string) error {
is.Flag = value
switch value {
case ImageSpecV1_1:
is.PackVersion = oras.PackManifestVersion1_1
case ImageSpecV1_0:
is.PackVersion = oras.PackManifestVersion1_0
default:
return &oerrors.Error{
Err: fmt.Errorf("unknown image specification flag: %s", value),
Recommendation: fmt.Sprintf("Available options: %s", is.Options()),
}
}
return nil
}
// Type returns the string value of the inner flag.
func (is *ImageSpec) Type() string {
return "string"
}
// Options returns the string of usable options for the flag.
func (is *ImageSpec) Options() string {
return strings.Join([]string{
ImageSpecV1_1,
ImageSpecV1_0,
}, ", ")
}
// String returns the string representation of the flag.
func (is *ImageSpec) String() string {
// to avoid printing default value in usage doc
return ""
}
// ApplyFlags applies flags to a command flag set.
func (is *ImageSpec) ApplyFlags(fs *pflag.FlagSet) {
// default to v1.1, unless --config is used and --artifact-type is not used
is.PackVersion = oras.PackManifestVersion1_1
is.Flag = ImageSpecV1_1
fs.Var(is, "image-spec", `[Preview] specify manifest type for building artifact. Options: v1.1, v1.0 (default v1.1, overridden to v1.0 if --config is used without --artifact-type)`)
}
// DistributionSpec option struct which implements pflag.Value interface.
type DistributionSpec struct {
// ReferrersAPI indicates the preference of the implementation of the Referrers API.
// Set to true for referrers API, false for referrers tag scheme, and nil for auto fallback.
ReferrersAPI *bool
// specFlag should be provided in form of`<version>-<api>-<option>`
flag string
}
// Set validates and sets the flag value from a string argument.
func (ds *DistributionSpec) Set(value string) error {
ds.flag = value
switch ds.flag {
case DistributionSpecReferrersTagV1_1:
isApi := false
ds.ReferrersAPI = &isApi
case DistributionSpecReferrersAPIV1_1:
isApi := true
ds.ReferrersAPI = &isApi
default:
return &oerrors.Error{
Err: fmt.Errorf("unknown distribution specification flag: %s", value),
Recommendation: fmt.Sprintf("Available options: %s", ds.Options()),
}
}
return nil
}
// Type returns the string value of the inner flag.
func (ds *DistributionSpec) Type() string {
return "string"
}
// Options returns the string of usable options for the flag.
func (ds *DistributionSpec) Options() string {
return strings.Join([]string{
DistributionSpecReferrersTagV1_1,
DistributionSpecReferrersAPIV1_1,
}, ", ")
}
// String returns the string representation of the flag.
func (ds *DistributionSpec) String() string {
return ds.flag
}
// ApplyFlagsWithPrefix applies flags to a command flag set with a prefix string.
func (ds *DistributionSpec) ApplyFlagsWithPrefix(fs *pflag.FlagSet, prefix, description string) {
flagPrefix, notePrefix := applyPrefix(prefix, description)
fs.Var(ds, flagPrefix+"distribution-spec", fmt.Sprintf("[Preview] set OCI distribution spec version and API option for %starget. Options: %s", notePrefix, ds.Options()))
}