mirror of https://github.com/coder/coder.git
ensure run in coder repo, use base path fs
This commit is contained in:
parent
ecc0a8eec9
commit
694ac3079c
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"slices"
|
"slices"
|
||||||
|
@ -30,8 +31,22 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// Pre-flight checks.
|
||||||
|
toplevel, err := run("git", "rev-parse", "--show-toplevel")
|
||||||
|
if err != nil {
|
||||||
|
_, _ = fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
|
||||||
|
_, _ = fmt.Fprintf(os.Stderr, "NOTE: This command must be run in the coder/coder repository.\n")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = checkCoderRepo(toplevel); err != nil {
|
||||||
|
_, _ = fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
|
||||||
|
_, _ = fmt.Fprintf(os.Stderr, "NOTE: This command must be run in the coder/coder repository.\n")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
r := &releaseCommand{
|
r := &releaseCommand{
|
||||||
fs: afero.NewOsFs(),
|
fs: afero.NewBasePathFs(afero.NewOsFs(), toplevel),
|
||||||
logger: slog.Make(sloghuman.Sink(os.Stderr)).Leveled(slog.LevelInfo),
|
logger: slog.Make(sloghuman.Sink(os.Stderr)).Leveled(slog.LevelInfo),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,7 +124,7 @@ func main() {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
err := cmd.Invoke().WithOS().Run()
|
err = cmd.Invoke().WithOS().Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, cliui.Canceled) {
|
if errors.Is(err, cliui.Canceled) {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -119,6 +134,17 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkCoderRepo(path string) error {
|
||||||
|
remote, err := run("git", "-C", path, "remote", "get-url", "origin")
|
||||||
|
if err != nil {
|
||||||
|
return xerrors.Errorf("get remote failed: %w", err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(remote, "github.com") || !strings.Contains(remote, "coder/coder") {
|
||||||
|
return xerrors.Errorf("origin is not set to the coder/coder repository on github.com")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type releaseCommand struct {
|
type releaseCommand struct {
|
||||||
fs afero.Fs
|
fs afero.Fs
|
||||||
logger slog.Logger
|
logger slog.Logger
|
||||||
|
@ -389,3 +415,12 @@ func (r *releaseCommand) autoversionFile(ctx context.Context, file, channel, ver
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func run(command string, args ...string) (string, error) {
|
||||||
|
cmd := exec.Command(command, args...)
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return "", xerrors.Errorf("command failed: %q: %w\n%s", fmt.Sprintf("%s %s", command, strings.Join(args, " ")), err, out)
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(string(out)), nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue