diff --git a/.golangci.yaml b/.golangci.yaml index 606b13589..6e3f83770 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -2,6 +2,14 @@ linters: disable: - errcheck +linters-settings: + goimports: + local-prefixes: github.com/open-component-model/ocm + staticcheck: + go: "1.18" + stylecheck: + go: "1.18" + issues: exclude: - composites diff --git a/cmds/helminstaller/app/execute.go b/cmds/helminstaller/app/execute.go index 1f1ce8ff4..460cb738c 100644 --- a/cmds/helminstaller/app/execute.go +++ b/cmds/helminstaller/app/execute.go @@ -16,7 +16,6 @@ package app import ( "fmt" - "io/ioutil" "os" "strings" @@ -68,7 +67,7 @@ func Execute(d driver.Driver, action string, ctx ocm.Context, octx out.Context, } // have to use the OS filesystem here for using the helm library - file, err := ioutil.TempFile("", "helm-*") + file, err := os.CreateTemp("", "helm-*") if err != nil { return err } diff --git a/hack/check.sh b/hack/check.sh index 6c80d5f28..46a022981 100755 --- a/hack/check.sh +++ b/hack/check.sh @@ -23,18 +23,4 @@ echo "Executing golangci-lint" echo " golangci-lint run $GOLANGCI_LINT_CONFIG_FILE --timeout 10m $@" golangci-lint run $GOLANGCI_LINT_CONFIG_FILE --timeout 10m $@ -echo "Executing goimports" -folders=() -for f in $@; do - folders+=( "$(echo "$f" | sed 's/\(.*\)\/\.\.\./\1/')" ) -done - -GOBIN=$(go env GOPATH) -unformatted_files="$("${GOBIN}/bin"/goimports -l -local=github.com/open-component-model/ocm ${folders[*]})" -if [[ "$unformatted_files" ]]; then - echo "Unformatted files detected:" - echo "$unformatted_files" - exit 1 -fi - echo "All checks successful" diff --git a/pkg/common/compression/c_bzip2.go b/pkg/common/compression/c_bzip2.go index 369af1386..402ebd963 100644 --- a/pkg/common/compression/c_bzip2.go +++ b/pkg/common/compression/c_bzip2.go @@ -18,7 +18,6 @@ import ( "compress/bzip2" "fmt" "io" - "io/ioutil" ) // Bzip2AlgorithmName is the name used by pkg/compression.Bzip2. @@ -37,7 +36,7 @@ func init() { // bzip2Decompressor is a DecompressorFunc for the bzip2 compression algorithm. func bzip2Decompressor(r io.Reader) (io.ReadCloser, error) { - return ioutil.NopCloser(bzip2.NewReader(r)), nil + return io.NopCloser(bzip2.NewReader(r)), nil } // bzip2Compressor is a CompressorFunc for the bzip2 compression algorithm. diff --git a/pkg/common/compression/c_xz.go b/pkg/common/compression/c_xz.go index 399302a1b..be42337d2 100644 --- a/pkg/common/compression/c_xz.go +++ b/pkg/common/compression/c_xz.go @@ -16,7 +16,6 @@ package compression import ( "io" - "io/ioutil" "github.com/ulikunitz/xz" ) @@ -41,7 +40,7 @@ func xzDecompressor(r io.Reader) (io.ReadCloser, error) { if err != nil { return nil, err } - return ioutil.NopCloser(r), nil + return io.NopCloser(r), nil } // xzCompressor is a CompressorFunc for the xz compression algorithm. diff --git a/pkg/common/compression/compression_test.go b/pkg/common/compression/compression_test.go index 8516c9d6b..a5774d85a 100644 --- a/pkg/common/compression/compression_test.go +++ b/pkg/common/compression/compression_test.go @@ -18,7 +18,6 @@ import ( "bytes" "fmt" "io" - "io/ioutil" "os" "testing" @@ -59,7 +58,7 @@ func TestDetectCompression(t *testing.T) { // The original stream is preserved. fmt.Printf("***preserve***\n") for _, c := range cases { - originalContents, err := ioutil.ReadFile(c) + originalContents, err := os.ReadFile(c) require.NoError(t, err, c) stream, err := os.Open(c) @@ -70,7 +69,7 @@ func TestDetectCompression(t *testing.T) { _, updatedStream, err := DetectCompression(stream) require.NoError(t, err, c) - updatedContents, err := ioutil.ReadAll(updatedStream) + updatedContents, err := io.ReadAll(updatedStream) require.NoError(t, err, c) assert.Equal(t, originalContents, updatedContents, c) } @@ -92,7 +91,7 @@ func TestDetectCompression(t *testing.T) { defer s.Close() updatedStream = s - uncompressedContents, err := ioutil.ReadAll(updatedStream) + uncompressedContents, err := io.ReadAll(updatedStream) require.NoError(t, err, c) assert.Equal(t, []byte("Hello"), uncompressedContents, c) } @@ -103,7 +102,7 @@ func TestDetectCompression(t *testing.T) { algo, updatedStream, err := DetectCompression(bytes.NewReader([]byte{})) require.NoError(t, err) assert.Equal(t, None, algo) - updatedContents, err := ioutil.ReadAll(updatedStream) + updatedContents, err := io.ReadAll(updatedStream) require.NoError(t, err) assert.Equal(t, []byte{}, updatedContents) @@ -139,7 +138,7 @@ func TestAutoDecompress(t *testing.T) { assert.Equal(t, c.isCompressed, isCompressed) - uncompressedContents, err := ioutil.ReadAll(uncompressedStream) + uncompressedContents, err := io.ReadAll(uncompressedStream) require.NoError(t, err, c.filename) assert.Equal(t, []byte("Hello"), uncompressedContents, c.filename) } @@ -148,7 +147,7 @@ func TestAutoDecompress(t *testing.T) { uncompressedStream, isCompressed, err := AutoDecompress(bytes.NewReader([]byte{})) require.NoError(t, err) assert.False(t, isCompressed) - uncompressedContents, err := ioutil.ReadAll(uncompressedStream) + uncompressedContents, err := io.ReadAll(uncompressedStream) require.NoError(t, err) assert.Equal(t, []byte{}, uncompressedContents) diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index d1dcf2f76..68593105c 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -16,7 +16,6 @@ package config import ( "fmt" - "io/ioutil" "os" "strings" @@ -47,7 +46,7 @@ func Configure(file string) error { } file = h + file[1:] } - data, err := ioutil.ReadFile(file) + data, err := os.ReadFile(file) if err != nil { return errors.Wrapf(err, "cannot read config file %q", file) } diff --git a/pkg/contexts/config/config/context_test.go b/pkg/contexts/config/config/context_test.go index 8a6c571ab..6a0e34628 100644 --- a/pkg/contexts/config/config/context_test.go +++ b/pkg/contexts/config/config/context_test.go @@ -15,7 +15,7 @@ package config_test import ( - "io/ioutil" + "os" "reflect" . "github.com/onsi/ginkgo" @@ -31,7 +31,7 @@ var _ = Describe("generic config handling", func() { var scheme config.ConfigTypeScheme var cfgctx config.Context - testdataconfig, _ := ioutil.ReadFile("testdata/config.yaml") + testdataconfig, _ := os.ReadFile("testdata/config.yaml") testdatajson, _ := yaml.YAMLToJSON(testdataconfig) _ = testdatajson diff --git a/pkg/contexts/credentials/repositories/dockerconfig/repository.go b/pkg/contexts/credentials/repositories/dockerconfig/repository.go index ff1580c62..88dbdb3b7 100644 --- a/pkg/contexts/credentials/repositories/dockerconfig/repository.go +++ b/pkg/contexts/credentials/repositories/dockerconfig/repository.go @@ -17,7 +17,6 @@ package dockerconfig import ( "bytes" "fmt" - "io/ioutil" "os" "strings" "sync" @@ -97,7 +96,8 @@ func (r *Repository) Read(force bool) error { home := os.Getenv("HOME") path = home + path[1:] } - data, err := ioutil.ReadFile(path) + + data, err := os.ReadFile(path) if err != nil { return err } diff --git a/pkg/contexts/oci/repositories/docker/utils.go b/pkg/contexts/oci/repositories/docker/utils.go index 591999b34..cc6862a25 100644 --- a/pkg/contexts/oci/repositories/docker/utils.go +++ b/pkg/contexts/oci/repositories/docker/utils.go @@ -18,7 +18,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "regexp" "strings" "sync" @@ -131,7 +130,7 @@ func readAll(reader io.ReadCloser, err error) ([]byte, error) { } defer reader.Close() - data, err := ioutil.ReadAll(reader) + data, err := io.ReadAll(reader) if err != nil { return nil, err } diff --git a/pkg/contexts/oci/repositories/ocireg/utils.go b/pkg/contexts/oci/repositories/ocireg/utils.go index 3c6fb70d0..d50da38ba 100644 --- a/pkg/contexts/oci/repositories/ocireg/utils.go +++ b/pkg/contexts/oci/repositories/ocireg/utils.go @@ -18,7 +18,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "sync" "github.com/containerd/containerd/errdefs" @@ -96,7 +95,7 @@ func readAll(reader io.ReadCloser, err error) ([]byte, error) { } defer reader.Close() - data, err := ioutil.ReadAll(reader) + data, err := io.ReadAll(reader) if err != nil { return nil, err } diff --git a/pkg/contexts/ocm/accessmethods/github/method_test.go b/pkg/contexts/ocm/accessmethods/github/method_test.go index 99649d393..42adc6635 100644 --- a/pkg/contexts/ocm/accessmethods/github/method_test.go +++ b/pkg/contexts/ocm/accessmethods/github/method_test.go @@ -17,7 +17,7 @@ package github_test import ( "bytes" "fmt" - "io/ioutil" + "io" "net/http" "os" "path/filepath" @@ -69,7 +69,7 @@ func (m *mockDownloader) Download(link string) ([]byte, error) { } func Configure(ctx ocm.Context) { - data, err := ioutil.ReadFile(filepath.Join(os.Getenv("HOME"), ".ocmconfig")) + data, err := os.ReadFile(filepath.Join(os.Getenv("HOME"), ".ocmconfig")) if err != nil { return } @@ -98,7 +98,7 @@ var _ = Describe("Method", func() { return &http.Response{ StatusCode: 302, Status: http.StatusText(http.StatusFound), - Body: ioutil.NopCloser(bytes.NewBufferString(`{}`)), + Body: io.NopCloser(bytes.NewBufferString(`{}`)), // Must be set to non-nil value or it panics Header: http.Header{ "Location": []string{defaultLink}, diff --git a/pkg/docker/fetcher.go b/pkg/docker/fetcher.go index 4b2c10e9a..2e2a893a0 100644 --- a/pkg/docker/fetcher.go +++ b/pkg/docker/fetcher.go @@ -21,7 +21,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "net/url" "strings" @@ -201,7 +200,7 @@ func (r dockerFetcher) open(ctx context.Context, req *request, mediatype string, // Discard up to offset // Could use buffer pool here but this case should be rare - n, err := io.Copy(ioutil.Discard, io.LimitReader(resp.Body, offset)) + n, err := io.Copy(io.Discard, io.LimitReader(resp.Body, offset)) if err != nil { return nil, errors.Wrap(err, "failed to discard to offset") } diff --git a/pkg/docker/httpreadseeker.go b/pkg/docker/httpreadseeker.go index 58c866bcd..cf1c182eb 100644 --- a/pkg/docker/httpreadseeker.go +++ b/pkg/docker/httpreadseeker.go @@ -19,7 +19,6 @@ package docker import ( "bytes" "io" - "io/ioutil" "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/log" @@ -162,7 +161,7 @@ func (hrs *httpReadSeeker) reader() (io.Reader, error) { // as the length is already satisfied but we just return the empty // reader instead. - hrs.rc = ioutil.NopCloser(bytes.NewReader([]byte{})) + hrs.rc = io.NopCloser(bytes.NewReader([]byte{})) } return hrs.rc, nil diff --git a/pkg/docker/pusher.go b/pkg/docker/pusher.go index 97ed66a6a..593aa5d93 100644 --- a/pkg/docker/pusher.go +++ b/pkg/docker/pusher.go @@ -19,7 +19,6 @@ package docker import ( "context" "io" - "io/ioutil" "net/http" "net/url" "strings" @@ -263,7 +262,7 @@ func (p dockerPusher) push(ctx context.Context, desc ocispec.Descriptor, ref str pr, pw := io.Pipe() respC := make(chan response, 1) - body := ioutil.NopCloser(pr) + body := io.NopCloser(pr) req.body = func() (io.ReadCloser, error) { if body == nil { diff --git a/pkg/docker/resolver.go b/pkg/docker/resolver.go index 1be9e1d05..4af1d19db 100644 --- a/pkg/docker/resolver.go +++ b/pkg/docker/resolver.go @@ -20,7 +20,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "net/http" "net/url" "path" @@ -359,7 +358,7 @@ func (r *dockerResolver) Resolve(ctx context.Context, ref string) (string, ocisp return "", ocispec.Descriptor{}, err } } - } else if _, err := io.Copy(ioutil.Discard, &bodyReader); err != nil { + } else if _, err := io.Copy(io.Discard, &bodyReader); err != nil { return "", ocispec.Descriptor{}, err } size = bodyReader.bytesRead diff --git a/pkg/signing/handlers/rsa-signingservice/client.go b/pkg/signing/handlers/rsa-signingservice/client.go index 58c194977..de295bc54 100644 --- a/pkg/signing/handlers/rsa-signingservice/client.go +++ b/pkg/signing/handlers/rsa-signingservice/client.go @@ -20,7 +20,7 @@ import ( "encoding/pem" "errors" "fmt" - "io/ioutil" + "io" "net/http" "github.com/open-component-model/ocm/pkg/signing" @@ -68,7 +68,7 @@ func (signer *SigningServerSigner) Sign(algo string, digest string, issuer strin } defer res.Body.Close() - responseBodyBytes, err := ioutil.ReadAll(res.Body) + responseBodyBytes, err := io.ReadAll(res.Body) if err != nil { return nil, fmt.Errorf("failed reading response body: %w", err) } diff --git a/pkg/signing/x509_certificate.go b/pkg/signing/x509_certificate.go index 69df71008..28b3c8a14 100644 --- a/pkg/signing/x509_certificate.go +++ b/pkg/signing/x509_certificate.go @@ -20,7 +20,7 @@ import ( "encoding/pem" "errors" "fmt" - "io/ioutil" + "os" ) // CreateAndVerifyX509CertificateFromFiles creates and verifies a x509 certificate from certificate files. @@ -30,7 +30,7 @@ func CreateAndVerifyX509CertificateFromFiles(certPath, intermediateCAsCertsPath, var rootCACert []byte if rootCACertPath != "" { - rootCACert, err = ioutil.ReadFile(rootCACertPath) + rootCACert, err = os.ReadFile(rootCACertPath) if err != nil { return nil, fmt.Errorf("unable to read root CA certificate file: %w", err) } @@ -38,13 +38,13 @@ func CreateAndVerifyX509CertificateFromFiles(certPath, intermediateCAsCertsPath, var intermediateCAsCerts []byte if intermediateCAsCertsPath != "" { - intermediateCAsCerts, err = ioutil.ReadFile(intermediateCAsCertsPath) + intermediateCAsCerts, err = os.ReadFile(intermediateCAsCertsPath) if err != nil { return nil, fmt.Errorf("unable to read intermediate CAs certificates file: %w", err) } } - cert, err := ioutil.ReadFile(certPath) + cert, err := os.ReadFile(certPath) if err != nil { return nil, fmt.Errorf("unable to read certificate file: %w", err) } diff --git a/pkg/toi/drivers/docker/driver.go b/pkg/toi/drivers/docker/driver.go index 920b0c7d7..383f32c54 100644 --- a/pkg/toi/drivers/docker/driver.go +++ b/pkg/toi/drivers/docker/driver.go @@ -19,7 +19,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "os" unix_path "path" "strconv" @@ -174,7 +173,7 @@ func (d *Driver) initializeDockerCli() (command.Cli, error) { } if d.config[OptionQuiet] == "1" { - cli.Apply(command.WithCombinedStreams(ioutil.Discard)) + cli.Apply(command.WithCombinedStreams(io.Discard)) } d.dockerCli = cli @@ -383,7 +382,7 @@ func (d *Driver) fetchOutputs(ctx context.Context, container string, op *install name := strings.TrimPrefix(header.Name, "outputs/") outputName, shouldCapture := op.Outputs[name] if shouldCapture { - contents, err = ioutil.ReadAll(tarReader) + contents, err = io.ReadAll(tarReader) if err != nil { return opResult, fmt.Errorf("error while reading %q from outputs tar: %s", header.Name, err) } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index abbfc1c87..9f6fe0e77 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -22,9 +22,9 @@ import ( "errors" "fmt" "io" - "io/ioutil" "math/rand" "net/http" + "os" "reflect" "sort" "strings" @@ -176,7 +176,7 @@ func WriteFileToTARArchive(filename string, contentReader io.Reader, archiveWrit return errors.New("archiveWriter must not be nil") } - tempfile, err := ioutil.TempFile("", "") + tempfile, err := os.CreateTemp("", "") if err != nil { return fmt.Errorf("unable to create tempfile: %w", err) } @@ -230,9 +230,8 @@ func JoinIndentLines(orig []string, gap string, skipfirst ...bool) string { return s } - func StringMapKeys(m interface{}) []string { - if m==nil { + if m == nil { return nil } v := reflect.ValueOf(m) @@ -243,10 +242,11 @@ func StringMapKeys(m interface{}) []string { panic(fmt.Sprintf("map key of %T is no string", m)) } - keys:=[]string{} + keys := []string{} for _, k := range v.MapKeys() { - keys=append(keys,k.Interface().(string)) + keys = append(keys, k.Interface().(string)) } sort.Strings(keys) return keys -} \ No newline at end of file +} +