Use path/filepath instead of path (#633)

This commit is contained in:
mattn 2018-09-12 07:41:30 +09:00 committed by Ludovic Fernandez
parent 035c27cdb7
commit 088c707d4c
5 changed files with 25 additions and 27 deletions

View file

@ -5,7 +5,7 @@ import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path/filepath"
"github.com/xenolf/lego/acme" "github.com/xenolf/lego/acme"
"github.com/xenolf/lego/log" "github.com/xenolf/lego/log"
@ -24,7 +24,7 @@ type Account struct {
func NewAccount(email string, conf *Configuration) *Account { func NewAccount(email string, conf *Configuration) *Account {
accKeysPath := conf.AccountKeysPath(email) accKeysPath := conf.AccountKeysPath(email)
// TODO: move to function in configuration? // TODO: move to function in configuration?
accKeyPath := accKeysPath + string(os.PathSeparator) + email + ".key" accKeyPath := filepath.Join(accKeysPath, email+".key")
if err := checkFolder(accKeysPath); err != nil { if err := checkFolder(accKeysPath); err != nil {
log.Fatalf("Could not check/create directory for account %s: %v", email, err) log.Fatalf("Could not check/create directory for account %s: %v", email, err)
} }
@ -46,7 +46,7 @@ func NewAccount(email string, conf *Configuration) *Account {
} }
} }
accountFile := path.Join(conf.AccountPath(email), "account.json") accountFile := filepath.Join(conf.AccountPath(email), "account.json")
if _, err := os.Stat(accountFile); os.IsNotExist(err) { if _, err := os.Stat(accountFile); os.IsNotExist(err) {
return &Account{Email: email, key: privKey, conf: conf} return &Account{Email: email, key: privKey, conf: conf}
} }
@ -127,7 +127,7 @@ func (a *Account) Save() error {
} }
return ioutil.WriteFile( return ioutil.WriteFile(
path.Join(a.conf.AccountPath(a.Email), "account.json"), filepath.Join(a.conf.AccountPath(a.Email), "account.json"),
jsonBytes, jsonBytes,
0600, 0600,
) )

4
cli.go
View file

@ -5,7 +5,7 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"path" "path/filepath"
"text/tabwriter" "text/tabwriter"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -29,7 +29,7 @@ func main() {
defaultPath := "" defaultPath := ""
cwd, err := os.Getwd() cwd, err := os.Getwd()
if err == nil { if err == nil {
defaultPath = path.Join(cwd, ".lego") defaultPath = filepath.Join(cwd, ".lego")
} }
app.Before = func(c *cli.Context) error { app.Before = func(c *cli.Context) error {

View file

@ -10,7 +10,6 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
@ -164,11 +163,11 @@ func saveCertRes(certRes *acme.CertificateResource, conf *Configuration) {
// We store the certificate, private key and metadata in different files // We store the certificate, private key and metadata in different files
// as web servers would not be able to work with a combined file. // as web servers would not be able to work with a combined file.
certOut := path.Join(conf.CertPath(), domainName+".crt") certOut := filepath.Join(conf.CertPath(), domainName+".crt")
privOut := path.Join(conf.CertPath(), domainName+".key") privOut := filepath.Join(conf.CertPath(), domainName+".key")
pemOut := path.Join(conf.CertPath(), domainName+".pem") pemOut := filepath.Join(conf.CertPath(), domainName+".pem")
metaOut := path.Join(conf.CertPath(), domainName+".json") metaOut := filepath.Join(conf.CertPath(), domainName+".json")
issuerOut := path.Join(conf.CertPath(), domainName+".issuer.crt") issuerOut := filepath.Join(conf.CertPath(), domainName+".issuer.crt")
err := checkFolder(filepath.Dir(certOut)) err := checkFolder(filepath.Dir(certOut))
if err != nil { if err != nil {
@ -379,7 +378,7 @@ func revoke(c *cli.Context) error {
for _, domain := range c.GlobalStringSlice("domains") { for _, domain := range c.GlobalStringSlice("domains") {
log.Printf("Trying to revoke certificate for domain %s", domain) log.Printf("Trying to revoke certificate for domain %s", domain)
certPath := path.Join(conf.CertPath(), domain+".crt") certPath := filepath.Join(conf.CertPath(), domain+".crt")
certBytes, err := ioutil.ReadFile(certPath) certBytes, err := ioutil.ReadFile(certPath)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -412,9 +411,9 @@ func renew(c *cli.Context) error {
// load the cert resource from files. // load the cert resource from files.
// We store the certificate, private key and metadata in different files // We store the certificate, private key and metadata in different files
// as web servers would not be able to work with a combined file. // as web servers would not be able to work with a combined file.
certPath := path.Join(conf.CertPath(), domain+".crt") certPath := filepath.Join(conf.CertPath(), domain+".crt")
privPath := path.Join(conf.CertPath(), domain+".key") privPath := filepath.Join(conf.CertPath(), domain+".key")
metaPath := path.Join(conf.CertPath(), domain+".json") metaPath := filepath.Join(conf.CertPath(), domain+".json")
certBytes, err := ioutil.ReadFile(certPath) certBytes, err := ioutil.ReadFile(certPath)
if err != nil { if err != nil {

View file

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"net/url" "net/url"
"os" "os"
"path" "path/filepath"
"strings" "strings"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -50,27 +50,26 @@ func (c *Configuration) ExcludedSolvers() (cc []acme.Challenge) {
// ServerPath returns the OS dependent path to the data for a specific CA // ServerPath returns the OS dependent path to the data for a specific CA
func (c *Configuration) ServerPath() string { func (c *Configuration) ServerPath() string {
srv, _ := url.Parse(c.context.GlobalString("server")) srv, _ := url.Parse(c.context.GlobalString("server"))
srvStr := strings.Replace(srv.Host, ":", "_", -1) return strings.NewReplacer(":", "_", "/", string(os.PathSeparator)).Replace(srv.Host)
return strings.Replace(srvStr, "/", string(os.PathSeparator), -1)
} }
// CertPath gets the path for certificates. // CertPath gets the path for certificates.
func (c *Configuration) CertPath() string { func (c *Configuration) CertPath() string {
return path.Join(c.context.GlobalString("path"), "certificates") return filepath.Join(c.context.GlobalString("path"), "certificates")
} }
// AccountsPath returns the OS dependent path to the // AccountsPath returns the OS dependent path to the
// local accounts for a specific CA // local accounts for a specific CA
func (c *Configuration) AccountsPath() string { func (c *Configuration) AccountsPath() string {
return path.Join(c.context.GlobalString("path"), "accounts", c.ServerPath()) return filepath.Join(c.context.GlobalString("path"), "accounts", c.ServerPath())
} }
// AccountPath returns the OS dependent path to a particular account // AccountPath returns the OS dependent path to a particular account
func (c *Configuration) AccountPath(acc string) string { func (c *Configuration) AccountPath(acc string) string {
return path.Join(c.AccountsPath(), acc) return filepath.Join(c.AccountsPath(), acc)
} }
// AccountKeysPath returns the OS dependent path to the keys of a particular account // AccountKeysPath returns the OS dependent path to the keys of a particular account
func (c *Configuration) AccountKeysPath(acc string) string { func (c *Configuration) AccountKeysPath(acc string) string {
return path.Join(c.AccountPath(acc), "keys") return filepath.Join(c.AccountPath(acc), "keys")
} }

View file

@ -5,7 +5,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path/filepath"
"github.com/xenolf/lego/acme" "github.com/xenolf/lego/acme"
) )
@ -32,8 +32,8 @@ func NewHTTPProvider(path string) (*HTTPProvider, error) {
func (w *HTTPProvider) Present(domain, token, keyAuth string) error { func (w *HTTPProvider) Present(domain, token, keyAuth string) error {
var err error var err error
challengeFilePath := path.Join(w.path, acme.HTTP01ChallengePath(token)) challengeFilePath := filepath.Join(w.path, acme.HTTP01ChallengePath(token))
err = os.MkdirAll(path.Dir(challengeFilePath), 0755) err = os.MkdirAll(filepath.Dir(challengeFilePath), 0755)
if err != nil { if err != nil {
return fmt.Errorf("could not create required directories in webroot for HTTP challenge -> %v", err) return fmt.Errorf("could not create required directories in webroot for HTTP challenge -> %v", err)
} }
@ -48,7 +48,7 @@ func (w *HTTPProvider) Present(domain, token, keyAuth string) error {
// CleanUp removes the file created for the challenge // CleanUp removes the file created for the challenge
func (w *HTTPProvider) CleanUp(domain, token, keyAuth string) error { func (w *HTTPProvider) CleanUp(domain, token, keyAuth string) error {
err := os.Remove(path.Join(w.path, acme.HTTP01ChallengePath(token))) err := os.Remove(filepath.Join(w.path, acme.HTTP01ChallengePath(token)))
if err != nil { if err != nil {
return fmt.Errorf("could not remove file in webroot after HTTP challenge -> %v", err) return fmt.Errorf("could not remove file in webroot after HTTP challenge -> %v", err)
} }