From 6f99f7717ae24277d85ac87136ad259413cee64d Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 11 Mar 2016 01:47:10 +0100 Subject: Refactor load --- cmd/acme/file.go | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'cmd/acme/file.go') diff --git a/cmd/acme/file.go b/cmd/acme/file.go index 7c1c6d9..f29345c 100644 --- a/cmd/acme/file.go +++ b/cmd/acme/file.go @@ -4,6 +4,7 @@ import ( "crypto" "crypto/tls" "crypto/x509" + "errors" "io" "os" "path" @@ -12,15 +13,12 @@ import ( "dim13.org/acme" ) +var ErrNotFound = errors.New("file not found") + func NewFile(fname string, mode os.FileMode) (io.WriteCloser, error) { err := os.Rename(fname, fname+".bak") if err != nil { - switch e := err.(type) { - case *os.LinkError: - if e.Err != syscall.ENOENT { - return nil, err - } - default: + if e, ok := err.(*os.LinkError); ok && e.Err != syscall.ENOENT { return nil, err } } @@ -61,14 +59,10 @@ func (d domain) Save(cert tls.Certificate) error { func (d domain) Load() (tls.Certificate, error) { crt, err := tls.LoadX509KeyPair(d.CrtFile, d.KeyFile) if err != nil { - switch e := err.(type) { - case *os.PathError: - if e.Err != syscall.ENOENT { - return tls.Certificate{}, err - } - default: - return tls.Certificate{}, nil + if e, ok := err.(*os.PathError); ok && e.Err == syscall.ENOENT { + err = ErrNotFound } + return tls.Certificate{}, err } crt.Leaf, err = x509.ParseCertificate(crt.Certificate[0]) return crt, err @@ -89,6 +83,9 @@ func (a account) Save(key crypto.PrivateKey) error { func (a account) Load() (crypto.PrivateKey, error) { fd, err := os.Open(a.KeyFile) if err != nil { + if e, ok := err.(*os.PathError); ok && e.Err == syscall.ENOENT { + err = ErrNotFound + } return nil, err } defer fd.Close() -- cgit v1.2.3