diff --git a/cmd/cosign/cli/generate/generate_key_pair.go b/cmd/cosign/cli/generate/generate_key_pair.go index 037e5a5e901..cfcd5c83270 100644 --- a/cmd/cosign/cli/generate/generate_key_pair.go +++ b/cmd/cosign/cli/generate/generate_key_pair.go @@ -18,16 +18,17 @@ package generate import ( "context" "crypto" - "errors" "fmt" "io" "os" "strings" + "github.com/pkg/errors" "github.com/sigstore/cosign/pkg/cosign/git" "github.com/sigstore/cosign/pkg/cosign/git/github" "github.com/sigstore/cosign/pkg/cosign/git/gitlab" + icos "github.com/sigstore/cosign/internal/pkg/cosign" "github.com/sigstore/cosign/pkg/cosign" "github.com/sigstore/cosign/pkg/cosign/kubernetes" "github.com/sigstore/sigstore/pkg/cryptoutils" @@ -85,9 +86,9 @@ func GenerateKeyPairCmd(ctx context.Context, kmsVal string, args []string) error return err } - fileExists, err := cosign.FileExists("cosign.key") + fileExists, err := icos.FileExists("cosign.key") if err != nil { - return err + return errors.Wrap(err, "error checking if cosign.key exists") } if fileExists { diff --git a/cmd/cosign/cli/importkeypair/import_key_pair.go b/cmd/cosign/cli/importkeypair/import_key_pair.go index 3a29898627d..4c8b1f203c4 100644 --- a/cmd/cosign/cli/importkeypair/import_key_pair.go +++ b/cmd/cosign/cli/importkeypair/import_key_pair.go @@ -21,6 +21,8 @@ import ( "io" "os" + "github.com/pkg/errors" + icos "github.com/sigstore/cosign/internal/pkg/cosign" "github.com/sigstore/cosign/pkg/cosign" ) @@ -36,9 +38,9 @@ func ImportKeyPairCmd(ctx context.Context, keyVal string, args []string) error { return err } - fileExists, err := cosign.FileExists("import-cosign.key\000x") + fileExists, err := icos.FileExists("import-cosign.key") if err != nil { - return err + return errors.Wrap(err, "error checking if import-cosign.key exists") } if fileExists { diff --git a/go.mod b/go.mod index 1e989650610..46e2f2875b5 100644 --- a/go.mod +++ b/go.mod @@ -52,6 +52,7 @@ require ( github.com/oklog/run v1.1.0 github.com/open-policy-agent/opa v0.43.0 github.com/pierrec/lz4 v2.6.1+incompatible + github.com/pkg/errors v0.9.1 github.com/ryanuber/go-glob v1.0.0 github.com/secure-systems-lab/go-securesystemslib v0.4.0 github.com/sigstore/fulcio v0.1.2-0.20220114150912-86a2036f9bc7 @@ -220,7 +221,6 @@ require ( github.com/opentracing/opentracing-go v1.2.0 // indirect github.com/pelletier/go-toml v1.9.5 // indirect github.com/pelletier/go-toml/v2 v2.0.1 // indirect - github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v1.12.2 // indirect github.com/prometheus/client_model v0.2.0 // indirect diff --git a/internal/pkg/cosign/common.go b/internal/pkg/cosign/common.go new file mode 100644 index 00000000000..74a9ae9b6d5 --- /dev/null +++ b/internal/pkg/cosign/common.go @@ -0,0 +1,28 @@ +// Copyright 2021 The Sigstore 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 cosign + +import "os" + +func FileExists(filename string) (bool, error) { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false, nil + } + if err != nil { + return false, err + } + return !info.IsDir(), nil +} diff --git a/pkg/cosign/common_test.go b/internal/pkg/cosign/common_test.go similarity index 100% rename from pkg/cosign/common_test.go rename to internal/pkg/cosign/common_test.go diff --git a/pkg/cosign/common.go b/pkg/cosign/common.go index 1915f81f645..0992843bd80 100644 --- a/pkg/cosign/common.go +++ b/pkg/cosign/common.go @@ -26,18 +26,6 @@ import ( "golang.org/x/term" ) -// TODO(jason): Move this to an internal package. -func FileExists(filename string) (bool, error) { - info, err := os.Stat(filename) - if os.IsNotExist(err) { - return false, nil - } - if err != nil { - return false, err - } - return !info.IsDir(), nil -} - // ConfirmPrompt prompts the user for confirmation for an action. Supports skipping // the confirmation prompt when skipConfirmation is set. // TODO(jason): Move this to an internal package.