Skip to content

Commit 694ac30

Browse files
committed
ensure run in coder repo, use base path fs
1 parent ecc0a8e commit 694ac30

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

scripts/release/main.go

+37-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io/fs"
88
"os"
9+
"os/exec"
910
"path/filepath"
1011
"regexp"
1112
"slices"
@@ -30,8 +31,22 @@ const (
3031
)
3132

3233
func main() {
34+
// Pre-flight checks.
35+
toplevel, err := run("git", "rev-parse", "--show-toplevel")
36+
if err != nil {
37+
_, _ = fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
38+
_, _ = fmt.Fprintf(os.Stderr, "NOTE: This command must be run in the coder/coder repository.\n")
39+
os.Exit(1)
40+
}
41+
42+
if err = checkCoderRepo(toplevel); err != nil {
43+
_, _ = fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
44+
_, _ = fmt.Fprintf(os.Stderr, "NOTE: This command must be run in the coder/coder repository.\n")
45+
os.Exit(1)
46+
}
47+
3348
r := &releaseCommand{
34-
fs: afero.NewOsFs(),
49+
fs: afero.NewBasePathFs(afero.NewOsFs(), toplevel),
3550
logger: slog.Make(sloghuman.Sink(os.Stderr)).Leveled(slog.LevelInfo),
3651
}
3752

@@ -109,7 +124,7 @@ func main() {
109124
},
110125
}
111126

112-
err := cmd.Invoke().WithOS().Run()
127+
err = cmd.Invoke().WithOS().Run()
113128
if err != nil {
114129
if errors.Is(err, cliui.Canceled) {
115130
os.Exit(1)
@@ -119,6 +134,17 @@ func main() {
119134
}
120135
}
121136

137+
func checkCoderRepo(path string) error {
138+
remote, err := run("git", "-C", path, "remote", "get-url", "origin")
139+
if err != nil {
140+
return xerrors.Errorf("get remote failed: %w", err)
141+
}
142+
if !strings.Contains(remote, "github.com") || !strings.Contains(remote, "coder/coder") {
143+
return xerrors.Errorf("origin is not set to the coder/coder repository on github.com")
144+
}
145+
return nil
146+
}
147+
122148
type releaseCommand struct {
123149
fs afero.Fs
124150
logger slog.Logger
@@ -389,3 +415,12 @@ func (r *releaseCommand) autoversionFile(ctx context.Context, file, channel, ver
389415

390416
return nil
391417
}
418+
419+
func run(command string, args ...string) (string, error) {
420+
cmd := exec.Command(command, args...)
421+
out, err := cmd.CombinedOutput()
422+
if err != nil {
423+
return "", xerrors.Errorf("command failed: %q: %w\n%s", fmt.Sprintf("%s %s", command, strings.Join(args, " ")), err, out)
424+
}
425+
return strings.TrimSpace(string(out)), nil
426+
}

0 commit comments

Comments
 (0)