-
Notifications
You must be signed in to change notification settings - Fork 2
/
renamer.go
66 lines (54 loc) · 1.18 KB
/
renamer.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
package stackcli
import (
"fmt"
"os"
"regexp"
"strings"
)
type Renamer struct {
Vals map[string]string
FileName string
}
type Match struct {
Key string
Value string
}
func NewRenamer(project *Project, filename string) *Renamer {
return &Renamer{
FileName: filename,
Vals: map[string]string{
"project-name": project.ProjectName,
},
}
}
func (r *Renamer) execute() string {
matches := r.match(r.FileName)
return r.renameMatches(r.FileName, matches)
}
func (r *Renamer) renameMatches(origFileName string, matches []Match) string {
newFileName := origFileName
for _, match := range matches {
newFileName = strings.Replace(newFileName, match.Key, match.Value, -1)
}
err := os.Rename(origFileName, newFileName)
if err != nil {
panic(err)
}
return newFileName
}
func (r *Renamer) match(path string) []Match {
matches := []Match{}
for key, value := range r.Vals {
newKey := fmt.Sprintf("{{%s}}", key)
matchTest := fmt.Sprintf("\\{\\{%s\\}\\}", key)
isMatch, _ := regexp.MatchString(matchTest, path)
if isMatch {
matches = append(matches, Match{
Key: newKey,
Value: value,
})
}
}
return matches
}
func (r *Renamer) getMatch(path string) {}