add checkDir for keyring

ensureDir will create the dir, which for calls like List, Sign, Remove,
etc. should not occur. It should only perform the actions if something
exists there

Signed-off-by: Derek McQuay <derekmcquay@gmail.com>
This commit is contained in:
Derek McQuay 2018-02-27 19:39:14 -08:00
parent e043384c67
commit c4d67105f2
No known key found for this signature in database
GPG Key ID: 50CA472F40A5AFED
1 changed files with 20 additions and 12 deletions

View File

@ -78,8 +78,8 @@ func NewKeyPair(root, name, email string) error {
// ListKeys prints keyring information to w. // ListKeys prints keyring information to w.
func ListKeys(root string, w io.Writer) error { func ListKeys(root string, w io.Writer) error {
if err := ensureDir(root); err != nil { if err := checkDir(root); err != nil {
return errors.Wrap(err, "can't find or create pgp dir") return errors.Wrap(err, "can't find pgp dir")
} }
srn, prn := getNames(root) srn, prn := getNames(root)
secs, pubs, err := getELs(srn, prn) secs, pubs, err := getELs(srn, prn)
@ -105,8 +105,8 @@ func ListKeys(root string, w io.Writer) error {
// Export prints pubkey information associated with email to w. // Export prints pubkey information associated with email to w.
func Export(root string, w io.Writer, email string) error { func Export(root string, w io.Writer, email string) error {
if err := ensureDir(root); err != nil { if err := checkDir(root); err != nil {
return errors.Wrap(err, "can't find or create pgp dir") return errors.Wrap(err, "can't find pgp dir")
} }
srn, prn := getNames(root) srn, prn := getNames(root)
_, pubs, err := getELs(srn, prn) _, pubs, err := getELs(srn, prn)
@ -142,8 +142,8 @@ func Import(root string, w io.Reader) error {
return errors.Wrap(err, "reading keyring") return errors.Wrap(err, "reading keyring")
} }
if err := ensureDir(root); err != nil { if err := checkDir(root); err != nil {
return errors.Wrap(err, "can't find or create pgp dir") return errors.Wrap(err, "can't find pgp dir")
} }
srn, prn := getNames(root) srn, prn := getNames(root)
_, pubs, err := getELs(srn, prn) _, pubs, err := getELs(srn, prn)
@ -185,8 +185,8 @@ func Import(root string, w io.Reader) error {
// Sign takes an id and a reader and writes the signature for that id to sig. // Sign takes an id and a reader and writes the signature for that id to sig.
func Sign(root, id string, in io.Reader, sig io.Writer) error { func Sign(root, id string, in io.Reader, sig io.Writer) error {
if err := ensureDir(root); err != nil { if err := checkDir(root); err != nil {
return errors.Wrap(err, "can't find or create pgp dir") return errors.Wrap(err, "can't find pgp dir")
} }
srn, prn := getNames(root) srn, prn := getNames(root)
secs, _, err := getELs(srn, prn) secs, _, err := getELs(srn, prn)
@ -206,8 +206,8 @@ func Sign(root, id string, in io.Reader, sig io.Writer) error {
// Verify verifies a file's deatched signature. // Verify verifies a file's deatched signature.
func Verify(root string, file, sig io.Reader) error { func Verify(root string, file, sig io.Reader) error {
if err := ensureDir(root); err != nil { if err := checkDir(root); err != nil {
return errors.Wrap(err, "can't find or create pgp dir") return errors.Wrap(err, "can't find pgp dir")
} }
srn, prn := getNames(root) srn, prn := getNames(root)
_, pubs, err := getELs(srn, prn) _, pubs, err := getELs(srn, prn)
@ -225,8 +225,8 @@ func Verify(root string, file, sig io.Reader) error {
// It skips public keys that have matching secret keys, and does not effect // It skips public keys that have matching secret keys, and does not effect
// private keys. // private keys.
func Remove(root string, id string) error { func Remove(root string, id string) error {
if err := ensureDir(root); err != nil { if err := checkDir(root); err != nil {
return errors.Wrap(err, "can't find or create pgp dir") return errors.Wrap(err, "can't find pgp dir")
} }
srn, prn := getNames(root) srn, prn := getNames(root)
secs, pubs, err := getELs(srn, prn) secs, pubs, err := getELs(srn, prn)
@ -266,6 +266,14 @@ func pGPDir(root string) string {
return filepath.Join(root, "var", "lib", "pm", "pgp") return filepath.Join(root, "var", "lib", "pm", "pgp")
} }
func checkDir(root string) error {
d := pGPDir(root)
if !fs.Exists(d) {
return fmt.Errorf("pgp dir does not exist")
}
return nil
}
func ensureDir(root string) error { func ensureDir(root string) error {
d := pGPDir(root) d := pGPDir(root)
if !fs.Exists(d) { if !fs.Exists(d) {