-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
169 lines (157 loc) · 3.92 KB
/
main.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"text/template"
"gopkg.in/yaml.v3"
)
type Config struct {
BackupDir string `yaml:"backupDir"`
BaseDir string `yaml:"baseDir"`
Dirs struct {
Backend string `yaml:"backend"`
Frontend string `yaml:"frontend"`
Infrastructure string `yaml:"infrastructure"`
Playground string `yaml:"playground"`
}
Backup bool `yaml:"backup"`
}
func main() {
if err := run(); err != nil {
fmt.Println("ERROR:", err)
}
}
func run() error {
bytes, err := os.ReadFile("config.yaml")
if err != nil {
return fmt.Errorf("cannot read config file: %w", err)
}
c, err := parseConfig(bytes)
if err != nil {
return err
}
if c.Backup {
if err := backupFiles(c); err != nil {
return err
}
}
if err := setupEnvs(c); err != nil {
return err
}
if err := execTemplate(c); err != nil {
return err
}
if err := runDotbot(); err != nil {
return err
}
return success()
}
func backupFiles(c *Config) error {
bakDir := path.Join(c.BaseDir, c.BackupDir)
if err := os.Mkdir(bakDir, os.ModePerm); err != nil &&
!strings.Contains(err.Error(), "file exists") {
return err
}
root := path.Clean(path.Join(c.BaseDir, ".config"))
return filepath.WalkDir(
root,
func(p string, d fs.DirEntry, _ error) error {
if path.Clean(root) == path.Clean(p) || !d.IsDir() {
return nil
}
if d.Type() == fs.ModeSymlink {
return nil
}
if err := os.Rename(p, path.Join(bakDir, d.Name())); err != nil {
return err
}
return fs.SkipDir
})
}
func parseConfig(bytes []byte) (*Config, error) {
c := new(Config)
if err := yaml.Unmarshal(bytes, c); err != nil {
return nil, fmt.Errorf("cannot parse config: %w", err)
}
if c.BaseDir == "" {
return nil, errors.New("base directory needs to be configured, please set baseDir in config.yaml")
}
return c, nil
}
func setupEnvs(c *Config) error {
baseDir, err := filepath.Abs(c.BaseDir)
if err != nil {
return err
}
os.Setenv("BASE_DIR", baseDir)
sub := func(d string) string {
return path.Clean(path.Join(baseDir, d))
}
os.Setenv("DIRS_BACKEND", sub(c.Dirs.Backend))
os.Setenv("DIRS_FRONTEND", sub(c.Dirs.Frontend))
os.Setenv("DIRS_INFRASTRUCTURE", sub(c.Dirs.Infrastructure))
os.Setenv("DIRS_PLAYGROUND", sub(c.Dirs.Playground))
return nil
}
func execTemplate(c *Config) error {
type Vars struct{ Config }
vars := Vars{Config: *c}
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("cannot get current directory: %w", err)
}
filepath.WalkDir(path.Join(cwd, "config"), func(p string, d fs.DirEntry, _ error) error {
re := regexp.MustCompile(`(.*)\.(\w+)\.tmpl$`)
matches := re.FindStringSubmatch(d.Name())
if d.IsDir() || !strings.HasSuffix(d.Name(), ".tmpl") {
return nil
}
content, err := os.ReadFile(p)
if err != nil {
return fmt.Errorf("cannot read file: %w", err)
}
tmpl, err := template.New("dotfiles").Parse(string(content))
if err != nil {
return fmt.Errorf("cannot parse template: %w", err)
}
dir := path.Dir(p)
newFilename := matches[1] + "." + matches[2]
file, err := os.Create(path.Join(dir, newFilename))
if err != nil {
return fmt.Errorf("cannot open file: %w", err)
}
defer file.Close()
if err := tmpl.Execute(file, vars); err != nil {
return fmt.Errorf("cannot execute template: %w", err)
}
return os.Remove(p)
})
return nil
}
func runDotbot() error {
for _, bin := range []string{
"install",
"install_fonts",
"install_fisher",
} {
if err := os.Chmod(bin, os.ModePerm); err != nil {
return fmt.Errorf("cannot chmod %s: %w", bin, err)
}
}
cmd := exec.Command("./install")
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
return fmt.Errorf("cannot run install: %w", err)
}
return nil
}
func success() error {
return exec.Command("say", "-v", "ralph", "\"Your config is now managed by the almighty bean. All hail beans. All hail beans. All hail beans.\"").Run()
}