6
6
"fmt"
7
7
"io/fs"
8
8
"os"
9
+ "os/exec"
9
10
"path/filepath"
10
11
"regexp"
11
12
"slices"
@@ -30,8 +31,22 @@ const (
30
31
)
31
32
32
33
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
+
33
48
r := & releaseCommand {
34
- fs : afero .NewOsFs (),
49
+ fs : afero .NewBasePathFs ( afero . NewOsFs (), toplevel ),
35
50
logger : slog .Make (sloghuman .Sink (os .Stderr )).Leveled (slog .LevelInfo ),
36
51
}
37
52
@@ -109,7 +124,7 @@ func main() {
109
124
},
110
125
}
111
126
112
- err : = cmd .Invoke ().WithOS ().Run ()
127
+ err = cmd .Invoke ().WithOS ().Run ()
113
128
if err != nil {
114
129
if errors .Is (err , cliui .Canceled ) {
115
130
os .Exit (1 )
@@ -119,6 +134,17 @@ func main() {
119
134
}
120
135
}
121
136
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
+
122
148
type releaseCommand struct {
123
149
fs afero.Fs
124
150
logger slog.Logger
@@ -389,3 +415,12 @@ func (r *releaseCommand) autoversionFile(ctx context.Context, file, channel, ver
389
415
390
416
return nil
391
417
}
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