cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
cmd/cloudflared/path/path.go
30lines · modecode
| 1 | package path |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/url" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/cloudflare/cloudflared/cmd/cloudflared/config" |
| 11 | "github.com/mitchellh/go-homedir" |
| 12 | ) |
| 13 | |
| 14 | // GenerateFilePathFromURL will return a filepath for given access application url |
| 15 | func GenerateFilePathFromURL(url *url.URL, suffix string) (string, error) { |
| 16 | configPath, err := homedir.Expand(config.DefaultConfigDirs[0]) |
| 17 | if err != nil { |
| 18 | return "", err |
| 19 | } |
| 20 | ok, err := config.FileExists(configPath) |
| 21 | if !ok && err == nil { |
| 22 | // create config directory if doesn't already exist |
| 23 | err = os.Mkdir(configPath, 0700) |
| 24 | } |
| 25 | if err != nil { |
| 26 | return "", err |
| 27 | } |
| 28 | name := strings.Replace(fmt.Sprintf("%s%s-%s", url.Hostname(), url.EscapedPath(), suffix), "/", "-", -1) |
| 29 | return filepath.Join(configPath, name), nil |
| 30 | } |
| 31 | |