diff --git a/.golangci.yaml b/.golangci.yaml index 303dc0ab0..e013318f8 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -15,10 +15,6 @@ linters: # Logical next step - forcetypeassert # Priority: that can lead to serious crashes. - - gocritic # Priority: - # - security issues (slice copy) - # - unexpected behavior - # - Go practices ('p += k' and not 'p = p + k') - gosec # Priority: Security (as the name implies) - revive # Too many issues. # It's important to at least address: diff --git a/cmds/ocm/app/app.go b/cmds/ocm/app/app.go index 07bd192ff..f6ce4226d 100644 --- a/cmds/ocm/app/app.go +++ b/cmds/ocm/app/app.go @@ -243,10 +243,8 @@ func (o *CLIOptions) Complete() error { } if len(attrs) != 0 { o.Context.CredentialsContext().SetCredentialsForConsumer(id, credentials.NewCredentials(attrs)) - } else { - if len(id) != 0 { - return errors.Newf("empty credential attribute set for %s", id.String()) - } + } else if len(id) != 0 { + return errors.Newf("empty credential attribute set for %s", id.String()) } set, err := common2.ParseLabels(o.Settings, "attribute setting") diff --git a/cmds/ocm/commands/ocicmds/common/handlers/artefacthdlr/clean.go b/cmds/ocm/commands/ocicmds/common/handlers/artefacthdlr/clean.go index 188845766..df5e86cbb 100644 --- a/cmds/ocm/commands/ocicmds/common/handlers/artefacthdlr/clean.go +++ b/cmds/ocm/commands/ocicmds/common/handlers/artefacthdlr/clean.go @@ -60,7 +60,7 @@ func clean(iterable data.Iterable) data.Iterable { d := depth[dig] if l == 0 && l < d && (e.Spec.Tag == nil || *e.Spec.Tag == tags[dig]) { j := i + 1 - prefix := append(e.History, common.NewNameVersion("", dig.String())) + prefix := e.History.Append(common.NewNameVersion("", dig.String())) for ; j < len(data) && data[j].(*Object).History.HasPrefix(prefix); j++ { } data = append(data[:i], data[j:]...) diff --git a/cmds/ocm/commands/ocmcmds/common/inputs/types/spiff/spec.go b/cmds/ocm/commands/ocmcmds/common/inputs/types/spiff/spec.go index 85bac5c51..cb9eaf442 100644 --- a/cmds/ocm/commands/ocmcmds/common/inputs/types/spiff/spec.go +++ b/cmds/ocm/commands/ocmcmds/common/inputs/types/spiff/spec.go @@ -67,10 +67,8 @@ func (s *Spec) Validate(fldPath *field.Path, ctx clictx.Context, inputFilePath s fileInfo, filePath, err := inputs.FileInfo(ctx, v, inputFilePath) if err != nil { allErrs = append(allErrs, field.Invalid(pathField, filePath, err.Error())) - } else { - if !fileInfo.Mode().IsRegular() { - allErrs = append(allErrs, field.Invalid(pathField, filePath, "no regular file")) - } + } else if !fileInfo.Mode().IsRegular() { + allErrs = append(allErrs, field.Invalid(pathField, filePath, "no regular file")) } } return allErrs diff --git a/cmds/ocm/commands/ocmcmds/common/resources.go b/cmds/ocm/commands/ocmcmds/common/resources.go index 3f3870632..3eeba0839 100644 --- a/cmds/ocm/commands/ocmcmds/common/resources.go +++ b/cmds/ocm/commands/ocmcmds/common/resources.go @@ -326,11 +326,9 @@ func Validate(r *ResourceInput, ctx clictx.Context, inputFilePath string) error } raw, _ := r.Access.GetRaw() allErrs = append(allErrs, field.Invalid(fldPath.Child("access"), string(raw), err.Error())) - } else { - if acc.(ocm.AccessSpec).IsLocal(ctx.OCMContext()) { - kind := runtime.ObjectVersionedType(r.Access.ObjectType).GetKind() - allErrs = append(allErrs, field.Invalid(fldPath.Child("access", "type"), kind, "local access no possible")) - } + } else if acc.(ocm.AccessSpec).IsLocal(ctx.OCMContext()) { + kind := runtime.ObjectVersionedType(r.Access.ObjectType).GetKind() + allErrs = append(allErrs, field.Invalid(fldPath.Child("access", "type"), kind, "local access no possible")) } } } diff --git a/cmds/ocm/commands/ocmcmds/references/get/cmd.go b/cmds/ocm/commands/ocmcmds/references/get/cmd.go index d62f09708..711c5533c 100644 --- a/cmds/ocm/commands/ocmcmds/references/get/cmd.go +++ b/cmds/ocm/commands/ocmcmds/references/get/cmd.go @@ -29,7 +29,7 @@ import ( "github.com/open-component-model/ocm/cmds/ocm/pkg/output" "github.com/open-component-model/ocm/cmds/ocm/pkg/processing" "github.com/open-component-model/ocm/cmds/ocm/pkg/utils" - gcommom "github.com/open-component-model/ocm/pkg/common" + gcommon "github.com/open-component-model/ocm/pkg/common" "github.com/open-component-model/ocm/pkg/contexts/clictx" "github.com/open-component-model/ocm/pkg/contexts/ocm" metav1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1" @@ -99,9 +99,9 @@ outer: for i := 0; i < len(slice); i++ { o := slice[i] e := common.Elem(o) - key := gcommom.NewNameVersion(e.ComponentName, e.Version) + key := gcommon.NewNameVersion(e.ComponentName, e.Version) hist := o.GetHistory() - nested := append(hist, key) + nested := hist.Append(key) var j int for j = i + 1; j < len(slice); j++ { n := slice[j] diff --git a/cmds/ocm/commands/ocmcmds/resources/add/resources.go b/cmds/ocm/commands/ocmcmds/resources/add/resources.go index f17c8f97f..f5d203eba 100644 --- a/cmds/ocm/commands/ocmcmds/resources/add/resources.go +++ b/cmds/ocm/commands/ocmcmds/resources/add/resources.go @@ -54,10 +54,8 @@ func (ResourceSpecHandler) Set(v ocm.ComponentVersionAccess, r common.Resource, if spec.Relation == metav1.LocalRelation { if vers == "" || vers == ComponentVersionTag { vers = v.GetVersion() - } else { - if vers != v.GetVersion() { - return errors.Newf("local resource %q (%s) has non-matching version %q", spec.Name, r.Source(), vers) - } + } else if vers != v.GetVersion() { + return errors.Newf("local resource %q (%s) has non-matching version %q", spec.Name, r.Source(), vers) } } if vers == ComponentVersionTag { diff --git a/cmds/ocm/commands/ocmcmds/resources/download/cmd.go b/cmds/ocm/commands/ocmcmds/resources/download/cmd.go index 10d5970d5..4da84f3c5 100644 --- a/cmds/ocm/commands/ocmcmds/resources/download/cmd.go +++ b/cmds/ocm/commands/ocmcmds/resources/download/cmd.go @@ -229,10 +229,8 @@ func (d *action) Save(o *elemhdlr.Object, f string) error { if err != nil { return err } - } else { - if eff != f && pathIn { - out.Outf(d.opts.Context, "output path %q changed to %q by downloader", f, eff) - } + } else if eff != f && pathIn { + out.Outf(d.opts.Context, "output path %q changed to %q by downloader", f, eff) } return nil } diff --git a/cmds/ocm/commands/ocmcmds/sources/add/resources.go b/cmds/ocm/commands/ocmcmds/sources/add/resources.go index 1cc200805..cfe1467f8 100644 --- a/cmds/ocm/commands/ocmcmds/sources/add/resources.go +++ b/cmds/ocm/commands/ocmcmds/sources/add/resources.go @@ -83,11 +83,9 @@ func (r *ResourceSpec) Validate(ctx clictx.Context, input *common.ResourceInput) if err != nil { raw, _ := r.Access.GetRaw() allErrs = append(allErrs, field.Invalid(fldPath.Child("access"), string(raw), err.Error())) - } else { - if acc.(ocm.AccessSpec).IsLocal(ctx.OCMContext()) { - kind := runtime.ObjectVersionedType(r.Access.ObjectType).GetKind() - allErrs = append(allErrs, field.Invalid(fldPath.Child("access", "type"), kind, "local access no possible")) - } + } else if acc.(ocm.AccessSpec).IsLocal(ctx.OCMContext()) { + kind := runtime.ObjectVersionedType(r.Access.ObjectType).GetKind() + allErrs = append(allErrs, field.Invalid(fldPath.Child("access", "type"), kind, "local access no possible")) } } } diff --git a/cmds/ocm/pkg/output/table.go b/cmds/ocm/pkg/output/table.go index 063f4964a..326422086 100644 --- a/cmds/ocm/pkg/output/table.go +++ b/cmds/ocm/pkg/output/table.go @@ -41,10 +41,8 @@ func FormatTable(ctx Context, gap string, data [][]string) { for i, col := range row { if i >= len(columns) { columns = append(columns, len(col)) - } else { - if columns[i] < len(col) { - columns[i] = len(col) - } + } else if columns[i] < len(col) { + columns[i] = len(col) } if len(col) > max { max = len(col) diff --git a/cmds/ocm/pkg/processing/chain.go b/cmds/ocm/pkg/processing/chain.go index b769f8e6f..b8ceb33b7 100644 --- a/cmds/ocm/pkg/processing/chain.go +++ b/cmds/ocm/pkg/processing/chain.go @@ -55,10 +55,8 @@ func (this *_ProcessChain) new(p *_ProcessChain, creator stepCreator) *_ProcessC if p != nil { if p.creator != nil { this.parent = p - } else { - if p.parent != nil { - this.parent = p.parent - } + } else if p.parent != nil { + this.parent = p.parent } } if this.parent != nil && creator == nil { @@ -129,10 +127,8 @@ func (this *_ProcessChain) Process(data data.Iterable) ProcessingResult { p, ok := data.(ProcessingResult) if this.parent != nil { p = this.parent.Process(data) - } else { - if !ok { - p = Process(data) - } + } else if !ok { + p = Process(data) } return this.step(p) } diff --git a/cmds/ocm/pkg/utils/command.go b/cmds/ocm/pkg/utils/command.go index d16e7e69b..45521c8e1 100644 --- a/cmds/ocm/pkg/utils/command.go +++ b/cmds/ocm/pkg/utils/command.go @@ -112,7 +112,7 @@ func SetupCommand(ocmcmd OCMCommand, names ...string) *cobra.Command { return err } if u, ok := ocmcmd.(options.Usage); ok { - c.Long = c.Long + u.Usage() + c.Long += u.Usage() } ocmcmd.AddFlags(c.Flags()) return c diff --git a/pkg/cobrautils/links.go b/pkg/cobrautils/links.go index 273caffef..4377d222a 100644 --- a/pkg/cobrautils/links.go +++ b/pkg/cobrautils/links.go @@ -27,7 +27,7 @@ func LinkForCmd(cmd *cobra.Command) string { func LinkForPath(path string) string { link := path + ".md" - link = strings.Replace(link, " ", "_", -1) + link = strings.ReplaceAll(link, " ", "_") return link } diff --git a/pkg/common/accessobj/filesystemaccess.go b/pkg/common/accessobj/filesystemaccess.go index d8706a4d4..6f2eb014a 100644 --- a/pkg/common/accessobj/filesystemaccess.go +++ b/pkg/common/accessobj/filesystemaccess.go @@ -119,10 +119,8 @@ func (a *FileSystemBlobAccess) AddBlob(blob accessio.BlobAccess) error { if ok, err := vfs.FileExists(a.base.GetFileSystem(), path); ok { return nil - } else { - if err != nil { - return fmt.Errorf("failed to check if '%s' file exists: %w", path, err) - } + } else if err != nil { + return fmt.Errorf("failed to check if '%s' file exists: %w", path, err) } r, err := blob.Reader() diff --git a/pkg/common/accessobj/format.go b/pkg/common/accessobj/format.go index 51ff8a4be..98cdb74d6 100644 --- a/pkg/common/accessobj/format.go +++ b/pkg/common/accessobj/format.go @@ -133,17 +133,18 @@ func DefaultOpenOptsFileHandling(kind string, info *AccessObjectInfo, acc Access var file vfs.File var err error var closer Closer - if opts.Reader != nil { + switch { + case opts.Reader != nil: reader = opts.Reader defer opts.Reader.Close() - } else if opts.File == nil { + case opts.File == nil: // we expect that the path point to a tar file, err = opts.PathFileSystem.Open(path) if err != nil { return nil, fmt.Errorf("unable to open %s from %s: %w", kind, path, err) } defer file.Close() - } else { + default: file = opts.File } if file != nil { diff --git a/pkg/common/history.go b/pkg/common/history.go index b4300fd8b..0e4d30425 100644 --- a/pkg/common/history.go +++ b/pkg/common/history.go @@ -85,6 +85,13 @@ func (h *History) Add(kind string, nv NameVersion) error { return nil } +func (h History) Append(nv ...NameVersion) History { + result := make(History, len(h)+len(nv)) + copy(result, h) + copy(result[len(h):], nv) + return result +} + func (h History) Copy() History { return append(h[:0:0], h...) } diff --git a/pkg/contexts/oci/attrs/cacheattr/attr.go b/pkg/contexts/oci/attrs/cacheattr/attr.go index d82e91c4a..1c042c008 100644 --- a/pkg/contexts/oci/attrs/cacheattr/attr.go +++ b/pkg/contexts/oci/attrs/cacheattr/attr.go @@ -70,10 +70,8 @@ func (a AttributeType) Decode(data []byte, unmarshaller runtime.Unmarshaler) (in if err == nil { return accessio.NewStaticBlobCache(value) } - } else { - if err == nil { - err = errors.Newf("file path missing") - } + } else if err == nil { + err = errors.Newf("file path missing") } return value, err } diff --git a/pkg/contexts/oci/core/uniform.go b/pkg/contexts/oci/core/uniform.go index 56a9fba49..d25ef6281 100644 --- a/pkg/contexts/oci/core/uniform.go +++ b/pkg/contexts/oci/core/uniform.go @@ -82,7 +82,7 @@ func (u *UniformRepositorySpec) ComposeRef(art string) string { func (u *UniformRepositorySpec) RepositoryRef() string { t := u.Type if t != "" { - t = t + "::" + t += "::" } if u.Info != "" { return fmt.Sprintf("%s%s", t, u.Info) diff --git a/pkg/contexts/oci/cpi/flavor_artefact.go b/pkg/contexts/oci/cpi/flavor_artefact.go index b718b60ae..4d890743c 100644 --- a/pkg/contexts/oci/cpi/flavor_artefact.go +++ b/pkg/contexts/oci/cpi/flavor_artefact.go @@ -257,10 +257,8 @@ func AdjustSize(d *artdesc.Descriptor, size int64) error { if size != accessio.BLOB_UNKNOWN_SIZE { if d.Size == accessio.BLOB_UNKNOWN_SIZE { d.Size = size - } else { - if d.Size != size { - return errors.Newf("blob size mismatch %d != %d", size, d.Size) - } + } else if d.Size != size { + return errors.Newf("blob size mismatch %d != %d", size, d.Size) } } return nil diff --git a/pkg/contexts/oci/cpi/support/flavor_artefact.go b/pkg/contexts/oci/cpi/support/flavor_artefact.go index 0670ee8fe..06fc8b5d8 100644 --- a/pkg/contexts/oci/cpi/support/flavor_artefact.go +++ b/pkg/contexts/oci/cpi/support/flavor_artefact.go @@ -232,10 +232,8 @@ func AdjustSize(d *artdesc.Descriptor, size int64) error { if size != accessio.BLOB_UNKNOWN_SIZE { if d.Size == accessio.BLOB_UNKNOWN_SIZE { d.Size = size - } else { - if d.Size != size { - return errors.Newf("blob size mismatch %d != %d", size, d.Size) - } + } else if d.Size != size { + return errors.Newf("blob size mismatch %d != %d", size, d.Size) } } return nil diff --git a/pkg/contexts/oci/ociutils/helm/ignore/rules.go b/pkg/contexts/oci/ociutils/helm/ignore/rules.go index 4153b8000..6c1236f3c 100644 --- a/pkg/contexts/oci/ociutils/helm/ignore/rules.go +++ b/pkg/contexts/oci/ociutils/helm/ignore/rules.go @@ -173,9 +173,10 @@ func (r *Rules) parseRule(rule string) error { rule = strings.TrimSuffix(rule, "/") } - if strings.HasPrefix(rule, "/") { + switch { + case strings.HasPrefix(rule, "/"): // Require path matches the root path. - p.match = func(n string, fi os.FileInfo) bool { + p.match = func(n string, _ os.FileInfo) bool { rule = strings.TrimPrefix(rule, "/") ok, err := filepath.Match(rule, n) if err != nil { @@ -184,9 +185,9 @@ func (r *Rules) parseRule(rule string) error { } return ok } - } else if strings.Contains(rule, "/") { + case strings.Contains(rule, "/"): // require structural match. - p.match = func(n string, fi os.FileInfo) bool { + p.match = func(n string, _ os.FileInfo) bool { ok, err := filepath.Match(rule, n) if err != nil { log.Printf("Failed to compile %q: %s", rule, err) @@ -194,8 +195,8 @@ func (r *Rules) parseRule(rule string) error { } return ok } - } else { - p.match = func(n string, fi os.FileInfo) bool { + default: + p.match = func(n string, _ os.FileInfo) bool { // When there is no slash in the pattern, we evaluate ONLY the // filename. n = filepath.Base(n) diff --git a/pkg/contexts/oci/repositories/ctf/index/index.go b/pkg/contexts/oci/repositories/ctf/index/index.go index 3b593d865..9ca2e1c7c 100644 --- a/pkg/contexts/oci/repositories/ctf/index/index.go +++ b/pkg/contexts/oci/repositories/ctf/index/index.go @@ -135,10 +135,8 @@ func (r *RepositoryIndex) GetTags(repo string) []string { if !strings.HasPrefix(t, "@") { result = append(result, t) digests[a.Digest] = true - } else { - if !digests[a.Digest] { - digests[a.Digest] = false - } + } else if !digests[a.Digest] { + digests[a.Digest] = false } } /* TODO: how to query untagged entries at api level diff --git a/pkg/contexts/ocm/accessmethods/ociblob/method.go b/pkg/contexts/ocm/accessmethods/ociblob/method.go index 41e67c57e..c348ef4cc 100644 --- a/pkg/contexts/ocm/accessmethods/ociblob/method.go +++ b/pkg/contexts/ocm/accessmethods/ociblob/method.go @@ -156,10 +156,8 @@ func (m *accessMethod) getBlob() (cpi.BlobAccess, error) { } if m.spec.Size == accessio.BLOB_UNKNOWN_SIZE { m.spec.Size = size - } else { - if size != accessio.BLOB_UNKNOWN_SIZE { - return nil, errors.Newf("blob size mismatch %d != %d", size, m.spec.Size) - } + } else if size != accessio.BLOB_UNKNOWN_SIZE { + return nil, errors.Newf("blob size mismatch %d != %d", size, m.spec.Size) } m.blob = accessio.BlobAccessForDataAccess(m.spec.Digest, m.spec.Size, m.spec.MediaType, acc) return m.blob, nil diff --git a/pkg/contexts/ocm/compdesc/codecs.go b/pkg/contexts/ocm/compdesc/codecs.go index 8d1e46ce7..f04e50fcf 100644 --- a/pkg/contexts/ocm/compdesc/codecs.go +++ b/pkg/contexts/ocm/compdesc/codecs.go @@ -93,6 +93,6 @@ var DefaultYAMLCodec = CodecWrapper{ // DefaultJSONCodec implements Codec interface with the json decoder encoder. var DefaultJSONLCodec = CodecWrapper{ Decoder: DecoderFunc(json.Unmarshal), - StrictDecoder: StrictDecoderFunc(func(data []byte, obj interface{}) error { return json.Unmarshal(data, obj) }), + StrictDecoder: StrictDecoderFunc(json.Unmarshal), Encoder: EncoderFunc(json.Marshal), } diff --git a/pkg/contexts/ocm/core/uniform.go b/pkg/contexts/ocm/core/uniform.go index 1a514ebc5..ab661a943 100644 --- a/pkg/contexts/ocm/core/uniform.go +++ b/pkg/contexts/ocm/core/uniform.go @@ -59,7 +59,7 @@ func (r *UniformRepositorySpec) CredHost() string { func (u *UniformRepositorySpec) String() string { t := u.Type if t != "" && !ocireg.IsKind(t) { - t = t + "::" + t += "::" } if u.Info != "" { return fmt.Sprintf("%s%s", t, u.Info) diff --git a/pkg/contexts/ocm/cpi/support/compversaccess.go b/pkg/contexts/ocm/cpi/support/compversaccess.go index f16d9feaf..fac68d404 100644 --- a/pkg/contexts/ocm/cpi/support/compversaccess.go +++ b/pkg/contexts/ocm/cpi/support/compversaccess.go @@ -304,7 +304,7 @@ func (c *componentVersionAccessImpl) SetResource(meta *cpi.ResourceMeta, acc com cd := c.GetDescriptor() if idx := cd.GetResourceIndex(meta); idx == -1 { - cd.Resources = append(c.GetDescriptor().Resources, *res) + cd.Resources = append(cd.Resources, *res) cd.Signatures = nil } else { if !cd.Resources[idx].ResourceMeta.HashEqual(&res.ResourceMeta) { @@ -332,8 +332,7 @@ func (c *componentVersionAccessImpl) SetSource(meta *cpi.SourceMeta, acc compdes Access: acc, } - switch res.Version { - case "": + if res.Version == "" { res.Version = c.GetVersion() } diff --git a/pkg/contexts/ocm/download/handlers/helm/handler.go b/pkg/contexts/ocm/download/handlers/helm/handler.go index 1c86c22c7..2b0445b9f 100644 --- a/pkg/contexts/ocm/download/handlers/helm/handler.go +++ b/pkg/contexts/ocm/download/handlers/helm/handler.go @@ -70,7 +70,7 @@ func (h Handler) Download(ctx out.Context, racc cpi.ResourceAccess, path string, return true, "", errors.Newf("no layers found") } if !strings.HasSuffix(path, ".tgz") { - path = path + ".tgz" + path += ".tgz" } blob, err := m.GetBlob(m.GetDescriptor().Layers[0].Digest) if err != nil { diff --git a/pkg/contexts/ocm/ref.go b/pkg/contexts/ocm/ref.go index b4a5b95cd..6e32c82fb 100644 --- a/pkg/contexts/ocm/ref.go +++ b/pkg/contexts/ocm/ref.go @@ -133,7 +133,7 @@ func (r *RefSpec) HostPort() (string, string) { func (r *RefSpec) Reference() string { t := r.Type if t != "" { - t = t + "::" + t += "::" } s := r.SubPath if s != "" { diff --git a/pkg/contexts/ocm/repositories/genericocireg/repository.go b/pkg/contexts/ocm/repositories/genericocireg/repository.go index 407bb2330..ef08c2e99 100644 --- a/pkg/contexts/ocm/repositories/genericocireg/repository.go +++ b/pkg/contexts/ocm/repositories/genericocireg/repository.go @@ -102,7 +102,7 @@ func (r *RepositoryImpl) NumComponents(prefix string) (int, error) { } p := path.Join(r.meta.SubPath, componentmapping.ComponentDescriptorNamespace, prefix) if strings.HasSuffix(prefix, "/") && !strings.HasSuffix(p, "/") { - p = p + "/" + p += "/" } return lister.NumNamespaces(p) } @@ -116,7 +116,7 @@ func (r *RepositoryImpl) GetComponents(prefix string, closure bool) ([]string, e compprefix := len(p) + 1 p = path.Join(p, prefix) if strings.HasSuffix(prefix, "/") && !strings.HasSuffix(p, "/") { - p = p + "/" + p += "/" } tmp, err := lister.GetNamespaces(p, closure) if err != nil { diff --git a/pkg/contexts/ocm/signing/handle.go b/pkg/contexts/ocm/signing/handle.go index f59cc1afe..c1356c586 100644 --- a/pkg/contexts/ocm/signing/handle.go +++ b/pkg/contexts/ocm/signing/handle.go @@ -102,10 +102,8 @@ func apply(printer common.Printer, state common.WalkingState, cv ocm.ComponentVe if reference.Digest == nil { cd.References[i].Digest = calculatedDigest - } else { - if calculatedDigest != nil && !reflect.DeepEqual(reference.Digest, calculatedDigest) { - return nil, errors.Newf(refMsg(reference, state, "calculated reference digest (%+v) mismatches existing digest (%+v) for", calculatedDigest, reference.Digest)) - } + } else if calculatedDigest != nil && !reflect.DeepEqual(reference.Digest, calculatedDigest) { + return nil, errors.Newf(refMsg(reference, state, "calculated reference digest (%+v) mismatches existing digest (%+v) for", calculatedDigest, reference.Digest)) } printer.Printf(" reference %d: %s:%s: digest %s\n", i, reference.ComponentName, reference.Version, calculatedDigest) } diff --git a/pkg/docker/resolver.go b/pkg/docker/resolver.go index 6f41bbe41..1a711a5e3 100644 --- a/pkg/docker/resolver.go +++ b/pkg/docker/resolver.go @@ -471,7 +471,7 @@ func (r *dockerBase) request(host RegistryHost, method string, ps ...string) *re p := path.Join(parts...) // Join strips trailing slash, re-add ending "/" if included if len(parts) > 0 && strings.HasSuffix(parts[len(parts)-1], "/") { - p = p + "/" + p += "/" } return &request{ method: method, @@ -505,12 +505,12 @@ func (r *request) addNamespace(ns string) (err error) { return } } else { - r.path = r.path + "?" + r.path += "?" q = url.Values{} } q.Add("ns", ns) - r.path = r.path + q.Encode() + r.path += q.Encode() return } diff --git a/pkg/errors/format.go b/pkg/errors/format.go index c65f861a1..9e5b4d406 100644 --- a/pkg/errors/format.go +++ b/pkg/errors/format.go @@ -26,7 +26,7 @@ type defaultFormatter struct { func NewDefaultFormatter(verb, msg, preposition string) ErrorFormatter { if verb != "" { - verb = verb + " " + verb += " " } return &defaultFormatter{ verb: verb, @@ -44,7 +44,7 @@ func (f *defaultFormatter) Format(kind string, elem *string, ctx string) string elems = "\"" + *elem + "\" " } if kind != "" { - kind = kind + " " + kind += " " } if kind == "" && elems == "" { return f.msg + ctx diff --git a/pkg/signing/handlers/rsa/format.go b/pkg/signing/handlers/rsa/format.go index c34d8d8c9..7f345f766 100644 --- a/pkg/signing/handlers/rsa/format.go +++ b/pkg/signing/handlers/rsa/format.go @@ -37,8 +37,7 @@ func GetPublicKey(key interface{}) (*rsa.PublicKey, []string, error) { case *rsa.PrivateKey: return &k.PublicKey, nil, nil case *x509.Certificate: - switch p := k.PublicKey.(type) { - case *rsa.PublicKey: + if p, ok := k.PublicKey.(*rsa.PublicKey); ok { return p, k.DNSNames, nil } return nil, nil, fmt.Errorf("unknown key public key %T in certificate", k) diff --git a/pkg/signing/handlers/rsa/handler.go b/pkg/signing/handlers/rsa/handler.go index 70cc0a956..1e4853dce 100644 --- a/pkg/signing/handlers/rsa/handler.go +++ b/pkg/signing/handlers/rsa/handler.go @@ -69,10 +69,6 @@ func (h Handler) Sign(digest string, hash crypto.Hash, issuer string, key interf if err != nil { return nil, fmt.Errorf("failed decoding hash to bytes") } - // ensure length of hash is correct - //if len(decodedHash) != 32 { - // return "", "", fmt.Errorf("hash to sign has invalid length") - //} sig, err := rsa.SignPKCS1v15(rand.Reader, privateKey, hash, decodedHash) if err != nil { return nil, fmt.Errorf("failed signing hash, %w", err) @@ -134,10 +130,6 @@ func (h Handler) Verify(digest string, hash crypto.Hash, signature *signing.Sign } } } - // ensure length of hash is correct - //if len(decodedHash) != 32 { - // return fmt.Errorf("hash to verify has invalid length") - //} if err := rsa.VerifyPKCS1v15(publicKey, hash, decodedHash, signatureBytes); err != nil { return fmt.Errorf("signature verification failed, %w", err) } diff --git a/pkg/toi/support/app.go b/pkg/toi/support/app.go index 75915449e..b781e44b1 100644 --- a/pkg/toi/support/app.go +++ b/pkg/toi/support/app.go @@ -133,10 +133,8 @@ func (o *BootstrapperCLIOptions) Complete() error { if len(attrs) != 0 { o.Context.CredentialsContext().SetCredentialsForConsumer(id, credentials.NewCredentials(attrs)) - } else { - if len(id) != 0 { - return errors.Newf("empty credential attribute set for %s", id.String()) - } + } else if len(id) != 0 { + return errors.Newf("empty credential attribute set for %s", id.String()) } set, err := common2.ParseLabels(o.Settings, "attribute setting") diff --git a/pkg/utils/selector/selector.go b/pkg/utils/selector/selector.go index 6ef13939b..cfc15030c 100644 --- a/pkg/utils/selector/selector.go +++ b/pkg/utils/selector/selector.go @@ -229,9 +229,9 @@ func NewJSONSchemaSelectorFromGoStruct(src interface{}) (JSONSchemaSelector, err var _ Interface = JSONSchemaSelector{} -func (J JSONSchemaSelector) Match(obj map[string]string) (bool, error) { +func (j JSONSchemaSelector) Match(obj map[string]string) (bool, error) { documentLoader := gojsonschema.NewGoLoader(obj) - res, err := J.Scheme.Validate(documentLoader) + res, err := j.Scheme.Validate(documentLoader) if err != nil { return false, err } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 089e02beb..42688cdb9 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -147,13 +147,13 @@ func BytesString(bytes uint64, accuracy int) string { switch { case bytes >= GIBIBYTE: unit = "GiB" - value = value / GIBIBYTE + value /= GIBIBYTE case bytes >= MEBIBYTE: unit = "MiB" - value = value / MEBIBYTE + value /= MEBIBYTE case bytes >= KIBIBYTE: unit = "KiB" - value = value / KIBIBYTE + value /= KIBIBYTE case bytes >= BYTE: unit = "bytes" case bytes == 0: