-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
123 lines (106 loc) · 3.21 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright (c) 2018 lærling
* For details see ./LICENSE
*/
package main
import (
"errors"
"os"
"strings"
)
// sortableStringSlice makes []string sortable. The sorting is case-insensitive.
type sortableStringSlice []string
// implementation of sort.Interface on stringSlice
func (s sortableStringSlice) Len() int {
return len(s)
}
// implementation of sort.Interface on stringSlice
func (s sortableStringSlice) Less(i, j int) bool {
return strings.ToLower(s[i]) < strings.ToLower(s[j])
}
// implementation of sort.Interface on stringSlice
func (s sortableStringSlice) Swap(i, j int) {
tmp := s[i]
s[i] = s[j]
s[j] = tmp
}
// getGithubUser returns the Github usernames for some of the most common Emacs
// distributions.
func getGithubUser(distroName string) (string, error) {
switch distroName {
case "doom-emacs":
return "hlissner", nil
case "emacs-live":
return "overtone", nil
case "prelude":
return "bbatsov", nil
case "spacemacs":
return "syl20bnr", nil
case "ohai-emacs":
return "bodil", nil
}
return "", errors.New("Could not find Github userName for repository '" + distroName + "'")
}
// makeRepoUrl takes a repository name or github name with repository
// name or URL and returns the fully qualified URL to use with git
// clone, as well as the name of the repository. For example:
// makeRepoUrl("prelude") == ("https://github.com/bbatnov/prelude", "prelude", nil)
// makeRepoUrl("bbatsov/prelude") == ("https://github.com/bbatsov/prelude", "RepoName", nil)
// makeRepoUrl("domain.tld/foo.git") == ("https://domain.tld/foo.git", "foo", nil)
func makeRepoUrl(distroUrlOrRepoName string) (string, string, error) {
distroUrl := distroUrlOrRepoName
// if distroUrl does not contain slash, get Github username
// the exact position of the slash is needed later
slashIndex := strings.Index(distroUrl, "/")
if slashIndex < 0 {
userName, err := getGithubUser(distroUrl)
if err != nil {
return "", "", err
}
distroUrl = userName + "/" + distroUrl
}
// at this point distroUrl can have the form foo/bar, or domain.tld/foo/bar
// if distroUrl does not contain dot before slash, assume Github
dotIndex := strings.Index(distroUrl, ".")
if dotIndex < 0 || dotIndex > slashIndex {
distroUrl = "github.com/" + distroUrl
}
// at this point distroUrl has the form domain.tld/foo/bar
// extract name of distro
distroName := distroUrl[strings.LastIndex(distroUrl, "/")+1:]
if strings.HasSuffix(distroName, ".git") {
distroName = distroName[:len(distroName)-4]
}
return distroUrl, distroName, nil
}
func directoryExists(dirName string) bool {
dir, err := os.Stat(dirName)
if err != nil {
if os.IsNotExist(err) {
return false
}
// fail only if stat failed but file is present
panic(err)
}
// check that it's really a directory, or a symlink pointing to a directory (or to a symlink pointing to... you get the idea)
if !dir.IsDir() {
return false
}
return true
}
func ensureDirectoryExists(dirName string) error {
if !directoryExists(dirName) {
if err := os.Mkdir(dirName, 0755); err != nil {
return err
}
}
return nil
}
func ensureDirectoryExistsNot(dirName string) error {
if directoryExists(dirName) {
if err := os.RemoveAll(dirName); err != nil {
return err
}
}
return nil
}