-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Xiaoxuan Wang <[email protected]>
- Loading branch information
1 parent
961e9f8
commit 8dc05a7
Showing
8 changed files
with
179 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
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 ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
oerrors "oras.land/oras/cmd/oras/internal/errors" | ||
) | ||
|
||
var ( | ||
errAnnotationFormat = errors.New("annotation value doesn't match the required format") | ||
errAnnotationDuplication = errors.New("duplicate annotation key") | ||
) | ||
|
||
// Annotation option struct. | ||
type Annotation struct { | ||
// ManifestAnnotations contains raw input of manifest annotation "key=value" pairs | ||
ManifestAnnotations []string | ||
|
||
// Annotations contains parsed manifest and config annotations | ||
Annotations map[string]map[string]string | ||
} | ||
|
||
// ApplyFlags applies flags to a command flag set. | ||
func (opts *Annotation) ApplyFlags(fs *pflag.FlagSet) { | ||
fs.StringArrayVarP(&opts.ManifestAnnotations, "annotation", "a", nil, "manifest annotations") | ||
} | ||
|
||
// Parse parses the input annotation flags. | ||
func (opts *Annotation) Parse(*cobra.Command) error { | ||
manifestAnnotations := make(map[string]string) | ||
for _, anno := range opts.ManifestAnnotations { | ||
key, val, success := strings.Cut(anno, "=") | ||
if !success { | ||
return &oerrors.Error{ | ||
Err: errAnnotationFormat, | ||
Recommendation: `Please use the correct format in the flag: --annotation "key=value"`, | ||
} | ||
} | ||
if _, ok := manifestAnnotations[key]; ok { | ||
return fmt.Errorf("%w: %v, ", errAnnotationDuplication, key) | ||
} | ||
manifestAnnotations[key] = val | ||
} | ||
opts.Annotations = map[string]map[string]string{ | ||
AnnotationManifest: manifestAnnotations, | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.