-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtools.go
86 lines (68 loc) · 1.61 KB
/
tools.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
//go:build tools
// +build tools
package main
import (
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
"github.com/spf13/cobra/doc"
"github.com/satisfactorymodding/ficsit-cli/cmd"
_ "github.com/Khan/genqlient/generate"
)
//go:generate go run github.com/Khan/genqlient
//go:generate go run -tags tools tools.go
func main() {
var err error
_ = os.RemoveAll("./docs/")
if err = os.Mkdir("./docs/", 0o777); err != nil {
panic(err)
}
err = doc.GenMarkdownTree(cmd.RootCmd, "./docs/")
if err != nil {
panic(err)
}
// replace user dir information with generic username
baseCacheDir, err := os.UserCacheDir()
if err != nil {
panic(err)
}
var baseLocalDir string
switch runtime.GOOS {
case "windows":
baseLocalDir = os.Getenv("APPDATA")
case "linux":
baseLocalDir = filepath.Join(os.Getenv("HOME"), ".local", "share")
case "darwin":
baseLocalDir = filepath.Join(os.Getenv("HOME"), "Library", "Application Support")
default:
panic("unsupported platform: " + runtime.GOOS)
}
docFiles, err := os.ReadDir("./docs/")
if err != nil {
panic(err)
}
user, err := user.Current()
if err != nil {
panic(err)
}
for _, f := range docFiles {
fPath := "./docs/" + f.Name()
oldContents, err := os.ReadFile(fPath)
if err != nil {
panic(err)
}
newContents := strings.ReplaceAll(
string(oldContents),
baseCacheDir,
strings.ReplaceAll(baseCacheDir, user.Username, "{{Username}}"),
)
newContents = strings.ReplaceAll(
newContents,
baseLocalDir,
strings.ReplaceAll(baseLocalDir, user.Username, "{{Username}}"),
)
os.WriteFile(fPath, []byte(newContents), 0o777)
}
}