Skip to content

Commit 2ef4ad3

Browse files
committed
feat(dist): improve error message if shell command fails
If release.go runs an external command, it reports a failure but does not mention the command that failed. This makes it harder to figure out what went wrong. Make release.go print the failed command. Also, make a helper function to shorten calling code.
1 parent 62e7968 commit 2ef4ad3

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

dist/release.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,14 @@ var Steps []Step = []Step{
121121
Step{
122122
Title: "Re-generate man pages",
123123
Run: func() {
124-
cmd := exec.Command("./docs/man/generate-man-pages")
125-
if err := cmd.Run(); err != nil {
126-
Stopf("failed to generate man pages: %v", err)
127-
}
124+
RunCommandOrStop("./docs/man/generate-man-pages")
128125
},
129126
},
130127

131128
Step{
132129
Title: "Re-generate Vim tags",
133130
Run: func() {
134-
cmd := exec.Command("./tools/generate-vim-tags")
135-
if err := cmd.Run(); err != nil {
136-
Stopf("failed to generate Vim tags: %v", err)
137-
}
131+
RunCommandOrStop("./tools/generate-vim-tags")
138132
},
139133
},
140134

@@ -201,31 +195,25 @@ var Steps []Step = []Step{
201195
Step{
202196
Title: "Create a Scoop manifest",
203197
Run: func() {
204-
cmd := exec.Command(
198+
RunCommandOrStop(
205199
"go", "run", "./dist/scoop/make-manifest.go", "-BaseURI",
206200
fmt.Sprintf("https://c.quick-lint-js.com/releases/%s/", ReleaseVersion),
207201
"-x86-ZIP", "signed-builds/manual/windows-x86.zip",
208202
"-x64-ZIP", "signed-builds/manual/windows.zip",
209203
"-Out", "signed-builds/scoop/quick-lint-js.json",
210204
)
211-
if err := cmd.Run(); err != nil {
212-
Stopf("failed to create Scoop manifest: %v", err)
213-
}
214205
},
215206
},
216207

217208
Step{
218209
Title: "Create a winget manifest",
219210
Run: func() {
220-
cmd := exec.Command(
211+
RunCommandOrStop(
221212
"go", "run", "./dist/winget/make-manifests.go", "-BaseURI",
222213
fmt.Sprintf("https://c.quick-lint-js.com/releases/%s/", ReleaseVersion),
223214
"-MSIX", "signed-builds/windows/quick-lint-js.msix",
224215
"-OutDir", "signed-builds/winget/",
225216
)
226-
if err := cmd.Run(); err != nil {
227-
Stopf("failed to create winget manifest: %v", err)
228-
}
229217
},
230218
},
231219

@@ -575,6 +563,26 @@ func UpdateReleaseVersions(options UpdateReleaseVersionsOptions) ([]byte, error)
575563
return newFileContent, nil
576564
}
577565

566+
func RunCommandOrStop(name string, arg ...string) {
567+
cmd := exec.Command(name, arg...)
568+
if err := cmd.Run(); err != nil {
569+
commandString := CommandToShell(cmd.Args)
570+
Stopf("failed to run command:\n$ %s\n%v", commandString, err)
571+
}
572+
}
573+
574+
// Escapes the command for POSIX-style shells.
575+
func CommandToShell(args []string) string {
576+
escape := func(arg string) string {
577+
return "'" + strings.ReplaceAll(arg, "'", "'\\''") + "'"
578+
}
579+
escapedArgs := make([]string, len(args))
580+
for i, arg := range args {
581+
escapedArgs[i] = escape(arg)
582+
}
583+
return strings.Join(escapedArgs, " ")
584+
}
585+
578586
func GetCurrentCommitHash() string {
579587
commitHash, gitErr := GetCurrentGitCommitHash()
580588
if gitErr == nil {

0 commit comments

Comments
 (0)