-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.go
173 lines (143 loc) · 4.51 KB
/
update.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
170
171
172
173
package mage
import (
"context"
"fmt"
"os"
"sync"
"github.com/dosquad/mage/dyndep"
"github.com/dosquad/mage/helper"
"github.com/dosquad/mage/loga"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/na4ma4/go-permbits"
"github.com/princjef/mageutil/shellcmd"
)
const (
golangciLintConfigURL = "https://gist.githubusercontent.com/na4ma4/f165f6c9af35cda6b330efdcc07a9e26/raw/.golangci.yml"
)
// Update namespace is defined to group Update functions.
type Update mg.Namespace
// GoWorkspace create the go.work file if it is missing.
func (Update) GoWorkspace(ctx context.Context) error {
dyndep.CtxDeps(ctx, dyndep.Update)
goworkspaceFile := helper.MustGetGitTopLevel("go.work")
if _, err := os.Stat(goworkspaceFile); os.IsNotExist(err) {
return shellcmd.RunAll(
"go work init",
"go work use . ./magefiles",
)
}
return nil
}
// GolangciLint updates the .golangci.yml from the gist.
func (Update) GolangciLint(ctx context.Context) error {
dyndep.CtxDeps(ctx, dyndep.Update)
golangciLintFile := helper.MustGetGitTopLevel(".golangci.yml")
golangciLocalFile := helper.MustGetGitTopLevel(".golangci.local.yml")
golangciRemoteFile := helper.MustGetGitTopLevel(".golangci.remote.yml")
etag := helper.Must[helper.ETag](helper.ETagLoadConfig())
if !helper.FileExists(golangciLocalFile) {
loga.PrintDebug("Downloading file directly")
if err := helper.HTTPWriteFile(
golangciLintConfigURL,
golangciRemoteFile,
nil,
0,
); err != nil {
return fmt.Errorf("unable to retrieve HTTP source file: %w", err)
}
if helper.FileChanged(golangciLintFile, golangciRemoteFile) {
loga.PrintFileUpdate("Updating .golangci.yml from remote")
}
if err := helper.FileCopy(golangciRemoteFile, golangciLintFile, true); err != nil {
return fmt.Errorf("unable to copy file: %w", err)
}
return sh.Rm(golangciRemoteFile)
}
loga.PrintDebug("Downloading remote config to .golangci.remote.yml")
if err := helper.HTTPWriteFile(
golangciLintConfigURL,
golangciRemoteFile,
etag.GetItem(".golangci.yml"),
0,
); err != nil {
return fmt.Errorf("unable to retrieve HTTP source file: %w", err)
}
defer func() {
loga.PrintDebug("Removing remote config cache .golangci.remote.yml")
_ = os.Remove(golangciRemoteFile)
}()
var yamlData []byte
{
var err error
loga.PrintDebug("Merging .golangci.remote.yml and .golangci.local.yml")
yamlData, err = helper.MergeYaml(golangciRemoteFile, golangciLocalFile)
if err != nil {
return fmt.Errorf("unable to merge remote and local config: %w", err)
}
}
loga.PrintDebug("Writing merged config to .golangci.yml")
if err := os.WriteFile(golangciLintFile, yamlData, permbits.MustString("ug=rw,o=r")); err != nil {
return fmt.Errorf("unable to write golangci ling config: %w", err)
}
return nil
}
// GitIgnore updates the .gitignore from a set list.
func (Update) GitIgnore(ctx context.Context) error {
dyndep.CtxDeps(ctx, dyndep.Update)
gitignoreFile := helper.MustGetGitTopLevel(".gitignore")
once := sync.Once{}
for _, path := range []string{
"/.makefiles",
"/artifacts",
"/dist",
} {
if !helper.FileLineExists(gitignoreFile, path) {
once.Do(func() {
if rn, err := helper.FileLastRune(gitignoreFile); err == nil && rn != '\n' {
_ = helper.FileAppendLine(gitignoreFile, 0, "")
}
})
loga.PrintFileUpdate("Adding path to .gitignore: %s", path)
if err := helper.FileAppendLine(gitignoreFile, 0, path); err != nil {
return err
}
}
}
return nil
}
// DockerIgnore writes the .dockerignore file if it does not exist.
func (Update) DockerIgnore(ctx context.Context) error {
dyndep.CtxDeps(ctx, dyndep.Update)
var dcfg *helper.DockerConfig
{
var err error
dcfg, err = helper.DockerLoadConfig()
helper.PanicIfError(err, "unable to load docker config")
}
dockerignoreFile := helper.MustGetGitTopLevel(".dockerignore")
once := sync.Once{}
for _, line := range dcfg.Ignore {
if !helper.FileLineExists(dockerignoreFile, line) {
once.Do(func() {
if rn, err := helper.FileLastRune(dockerignoreFile); err == nil && rn != '\n' {
_ = helper.FileAppendLine(dockerignoreFile, 0, "")
}
})
loga.PrintFileUpdate("Adding path to .dockerignore: %s", line)
if err := helper.FileAppendLine(dockerignoreFile, 0, line); err != nil {
return err
}
}
}
return nil
}
func UpdateE(ctx context.Context) error {
dyndep.CtxDeps(ctx, dyndep.Update)
mg.CtxDeps(ctx, Update.GolangciLint)
mg.CtxDeps(ctx, Update.GitIgnore)
if helper.FileExists("Dockerfile") {
mg.CtxDeps(ctx, Update.DockerIgnore)
}
return nil
}