Skip to content

Commit

Permalink
scripts: make helpers log output directable
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Gutekanst <[email protected]>
  • Loading branch information
emidoots committed Dec 28, 2022
1 parent 16940d4 commit 1f2044b
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 34 deletions.
2 changes: 1 addition & 1 deletion internal/wrench/scripts/env_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ func envFileEnsureLine(line string) error {
}
}

err = AppendToFile(file, "\n"+line+"\n")()
err = AppendToFile(file, "\n"+line+"\n")(os.Stderr)
return errors.Wrap(err, "appending to "+file)
}
4 changes: 2 additions & 2 deletions internal/wrench/scripts/install_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func init() {
archiveFilePath := "golang." + extension
_ = os.RemoveAll(archiveFilePath)
defer os.RemoveAll(archiveFilePath)
err = DownloadFile(url, archiveFilePath)()
err = DownloadFile(url, archiveFilePath)(os.Stderr)
if err != nil {
return errors.Wrap(err, "DownloadFile")
}
Expand All @@ -66,7 +66,7 @@ func init() {
}

// Extract the Go archive
err = ExtractArchive(archiveFilePath, ".")()
err = ExtractArchive(archiveFilePath, ".")(os.Stderr)
if err != nil {
return errors.Wrap(err, "ExtractArchive")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/wrench/scripts/install_zig.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func init() {
archiveFilePath := "zig." + extension
_ = os.RemoveAll(archiveFilePath)
defer os.RemoveAll(archiveFilePath)
err = DownloadFile(url, archiveFilePath)()
err = DownloadFile(url, archiveFilePath)(os.Stderr)
if err != nil {
return errors.Wrap(err, "DownloadFile")
}
Expand Down Expand Up @@ -71,7 +71,7 @@ func init() {
}

// // Extract the Go archive
// err = ExtractArchive(archiveFilePath, ".")()
// err = ExtractArchive(archiveFilePath, ".")(os.Stderr)
// if err != nil {
// return errors.Wrap(err, "ExtractArchive")
// }
Expand Down
4 changes: 3 additions & 1 deletion internal/wrench/scripts/rebuild.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package scripts

import "os"

func init() {
Scripts = append(Scripts, Script{
Command: "rebuild",
Expand All @@ -10,7 +12,7 @@ func init() {
Exec("wrench script install-go"),
Exec("wrench script rebuild-only"),
Exec("wrench svc restart").IgnoreError(),
)()
)(os.Stderr)
},
})
}
13 changes: 7 additions & 6 deletions internal/wrench/scripts/rebuild_only.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scripts

import (
"fmt"
"io"
"os"
"runtime"
"strings"
Expand All @@ -20,7 +21,7 @@ func init() {
Exec("git clone https://github.com/hexops/wrench").IgnoreError(),
Exec("git fetch", WorkDir("wrench")),
Exec("git reset --hard origin/main", WorkDir("wrench")),
)(); err != nil {
)(os.Stderr); err != nil {
return err
}

Expand All @@ -45,7 +46,7 @@ func init() {

return Sequence(
ExecArgs("go", []string{"build", "-ldflags", ldFlags, "-o", "bin/wrench", "."}, WorkDir("wrench")),
func() error {
func(w io.Writer) error {
exePath, err := os.Executable()
if err != nil {
return errors.Wrap(err, "Executable")
Expand All @@ -60,9 +61,9 @@ func init() {
// Rename wrench.exe -> wrench-old.exe
// Rename new build -> wrench.exe
exe2Path := strings.TrimSuffix(exePath, ".exe") + "-old.exe"
fmt.Printf("$ rm -f %s\n", exe2Path)
fmt.Fprintf(w, "$ rm -f %s\n", exe2Path)
_ = os.Remove(exe2Path)
fmt.Printf("$ mv %s %s\n", exePath, exe2Path)
fmt.Fprintf(w, "$ mv %s %s\n", exePath, exe2Path)
err = os.Rename(exePath, exe2Path)
if err != nil {
return errors.Wrap(err, "Rename")
Expand All @@ -72,14 +73,14 @@ func init() {
return errors.Wrap(err, "Remove")
}
}
fmt.Printf("$ mv %s %s\n", newBinary, exePath)
fmt.Fprintf(w, "$ mv %s %s\n", newBinary, exePath)
err = os.Rename("wrench/bin/wrench", exePath)
if err != nil {
return errors.Wrap(err, "Rename")
}
return nil
},
)()
)(os.Stderr)
},
})
}
38 changes: 19 additions & 19 deletions internal/wrench/scripts/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Env(key, value string) CmdOption {
}
}

func newCmd(name string, args []string, opt ...CmdOption) *exec.Cmd {
func newCmd(w io.Writer, name string, args []string, opt ...CmdOption) *exec.Cmd {
cmd := exec.Command(name, args...)
for _, opt := range opt {
opt(cmd)
Expand All @@ -55,15 +55,15 @@ func newCmd(name string, args []string, opt ...CmdOption) *exec.Cmd {
prefix = fmt.Sprintf("%s %s ", prefix, envKeyValue)
}
}
fmt.Fprintf(os.Stderr, "$ %s%s\n", prefix, strings.Join(append([]string{name}, args...), " "))
fmt.Fprintf(w, "$ %s%s\n", prefix, strings.Join(append([]string{name}, args...), " "))
return cmd
}

func ExecArgs(name string, args []string, opt ...CmdOption) Cmd {
return func() error {
cmd := newCmd(name, args, opt...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return func(w io.Writer) error {
cmd := newCmd(w, name, args, opt...)
cmd.Stderr = w
cmd.Stdout = w
if err := cmd.Run(); err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
return fmt.Errorf("'%s': error: exit code: %v", name, exitError.ExitCode())
Expand All @@ -82,7 +82,7 @@ func Exec(cmdLine string, opt ...CmdOption) Cmd {

func OutputArgs(name string, args []string, opt ...CmdOption) (string, error) {
var buf bytes.Buffer
cmd := newCmd(name, args, opt...)
cmd := newCmd(&buf, name, args, opt...)
cmd.Stderr = &buf
cmd.Stdout = &buf
if err := cmd.Run(); err != nil {
Expand All @@ -100,21 +100,21 @@ func Output(cmdLine string, opt ...CmdOption) (string, error) {
return OutputArgs(name, args, opt...)
}

type Cmd func() error
type Cmd func(w io.Writer) error

func (cmd Cmd) IgnoreError() Cmd {
return func() error {
if err := cmd(); err != nil {
fmt.Fprintf(os.Stderr, "ignoring error: %s\n", err)
return func(w io.Writer) error {
if err := cmd(w); err != nil {
fmt.Fprintf(w, "ignoring error: %s\n", err)
}
return nil
}
}

func Sequence(cmds ...Cmd) Cmd {
return func() error {
return func(w io.Writer) error {
for _, cmd := range cmds {
if err := cmd(); err != nil {
if err := cmd(w); err != nil {
return err
}
}
Expand All @@ -123,8 +123,8 @@ func Sequence(cmds ...Cmd) Cmd {
}

func DownloadFile(url string, filepath string) Cmd {
return func() error {
fmt.Fprintf(os.Stderr, "DownloadFile: %s > %s\n", url, filepath)
return func(w io.Writer) error {
fmt.Fprintf(w, "DownloadFile: %s > %s\n", url, filepath)
out, err := os.Create(filepath)
if err != nil {
return errors.Wrap(err, "Create")
Expand All @@ -150,8 +150,8 @@ func DownloadFile(url string, filepath string) Cmd {
}

func ExtractArchive(archiveFilePath, dst string) Cmd {
return func() error {
fmt.Fprintf(os.Stderr, "ExtractArchive: %s > %s\n", archiveFilePath, dst)
return func(w io.Writer) error {
fmt.Fprintf(w, "ExtractArchive: %s > %s\n", archiveFilePath, dst)
ctx := context.Background()
handler := func(ctx context.Context, fi archiver.File) error {
dstPath := filepath.Join(dst, fi.NameInArchive)
Expand Down Expand Up @@ -211,8 +211,8 @@ func ExtractArchive(archiveFilePath, dst string) Cmd {
}

func AppendToFile(file, format string, v ...any) Cmd {
return func() error {
fmt.Fprintf(os.Stderr, "AppendToFile: %s >> %s\n", fmt.Sprintf(format, v...), file)
return func(w io.Writer) error {
fmt.Fprintf(w, "AppendToFile: %s >> %s\n", fmt.Sprintf(format, v...), file)
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return errors.Wrap(err, "OpenFile")
Expand Down
4 changes: 2 additions & 2 deletions internal/wrench/scripts/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func init() {
}

if runtime.GOOS == "windows" {
return ExecArgs("powershell.exe", []string{tmpFile.Name()})()
return ExecArgs("powershell.exe", []string{tmpFile.Name()})(os.Stderr)
}
return ExecArgs("sh", []string{tmpFile.Name()})()
return ExecArgs("sh", []string{tmpFile.Name()})(os.Stderr)
},
})
}
Expand Down
3 changes: 2 additions & 1 deletion service_restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"os"
"runtime"

"github.com/hexops/cmder"
Expand Down Expand Up @@ -31,7 +32,7 @@ Examples:
if runtime.GOOS == "darwin" {
// kickstart -k is a better / safer approacher to restarting on macOS.
// https://github.com/kardianos/service/issues/358
if err := scripts.Exec(`launchctl kickstart -k system/wrench`)(); err != nil {
if err := scripts.Exec(`launchctl kickstart -k system/wrench`)(os.Stderr); err != nil {
return errors.Wrap(err, "restart")
}
return nil
Expand Down

0 comments on commit 1f2044b

Please sign in to comment.