-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
facedfa
commit 4f42f37
Showing
19 changed files
with
705 additions
and
51 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
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
82 changes: 82 additions & 0 deletions
82
cmds/ocm/commands/ocmcmds/common/options/hashoption/option.go
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,82 @@ | ||
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package hashoption | ||
|
||
import ( | ||
"github.com/spf13/pflag" | ||
|
||
"github.com/open-component-model/ocm/cmds/ocm/pkg/options" | ||
"github.com/open-component-model/ocm/cmds/ocm/pkg/utils" | ||
"github.com/open-component-model/ocm/pkg/contexts/clictx" | ||
"github.com/open-component-model/ocm/pkg/contexts/ocm/attrs/signingattr" | ||
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc" | ||
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/normalizations/jsonv1" | ||
ocmsign "github.com/open-component-model/ocm/pkg/contexts/ocm/signing" | ||
"github.com/open-component-model/ocm/pkg/errors" | ||
"github.com/open-component-model/ocm/pkg/signing" | ||
"github.com/open-component-model/ocm/pkg/signing/hasher/sha256" | ||
) | ||
|
||
func From(o options.OptionSetProvider) *Option { | ||
var opt *Option | ||
o.AsOptionSet().Get(&opt) | ||
return opt | ||
} | ||
|
||
var _ options.Options = (*Option)(nil) | ||
|
||
func New() *Option { | ||
return &Option{} | ||
} | ||
|
||
type Option struct { | ||
Hasher signing.Hasher | ||
NormAlgorithm string | ||
hashAlgorithm string | ||
} | ||
|
||
func (o *Option) AddFlags(fs *pflag.FlagSet) { | ||
fs.StringVarP(&o.NormAlgorithm, "normalization", "N", jsonv1.Algorithm, "normalization algorithm") | ||
fs.StringVarP(&o.hashAlgorithm, "hash", "H", sha256.Algorithm, "hash algorithm") | ||
} | ||
|
||
func (o *Option) Complete(ctx clictx.Context) error { | ||
if o.NormAlgorithm == "" { | ||
o.NormAlgorithm = jsonv1.Algorithm | ||
} | ||
if o.hashAlgorithm == "" { | ||
o.hashAlgorithm = sha256.Algorithm | ||
} | ||
x := compdesc.Normalizations.Get(o.NormAlgorithm) | ||
if x == nil { | ||
return errors.ErrUnknown(compdesc.KIND_NORM_ALGORITHM, o.NormAlgorithm) | ||
} | ||
o.Hasher = signingattr.Get(ctx).GetHasher(o.hashAlgorithm) | ||
if o.Hasher == nil { | ||
return errors.ErrUnknown(compdesc.KIND_HASH_ALGORITHM, o.hashAlgorithm) | ||
} | ||
return nil | ||
} | ||
|
||
func (o *Option) Usage() string { | ||
s := ` | ||
The following normalization modes are supported with option <code>--normalization</code>: | ||
` + utils.FormatList(jsonv1.Algorithm, compdesc.Normalizations.Names()...) | ||
|
||
s += ` | ||
The following hash modes are supported with option <code>--hash</code>: | ||
` + utils.FormatList(sha256.Algorithm, signing.DefaultRegistry().HasherNames()...) | ||
|
||
signing.DefaultRegistry().HasherNames() | ||
return s | ||
} | ||
|
||
var _ ocmsign.Option = (*Option)(nil) | ||
|
||
func (o *Option) ApplySigningOption(opts *ocmsign.Options) { | ||
opts.NormalizationAlgo = o.NormAlgorithm | ||
opts.Hasher = o.Hasher | ||
} |
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.