pkg/environment: Remove exec related functions
This commit starts the migration to remove the environment package. It introduced an abstraction that is not being used, and removing it makes the code simpler to maintain. This commit removes all 'exec' related package.
This commit is contained in:
parent
066390e30d
commit
14bb7f1086
6 changed files with 32 additions and 25 deletions
|
|
@ -6,8 +6,6 @@ import (
|
|||
"io/fs"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/tkw1536/goprogram/stream"
|
||||
)
|
||||
|
||||
// Environment represents an environment that a program can run it.
|
||||
|
|
@ -43,10 +41,6 @@ type Environment interface {
|
|||
|
||||
Listen(network, address string) (net.Listener, error)
|
||||
DialContext(context context.Context, network, address string) (net.Conn, error)
|
||||
|
||||
Executable() (string, error)
|
||||
Exec(ctx context.Context, io stream.IOStream, workdir string, exe string, argv ...string) func() int
|
||||
LookPathAbs(name string) (string, error)
|
||||
}
|
||||
|
||||
type WritableFile interface {
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
package environment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"github.com/tkw1536/goprogram/stream"
|
||||
"github.com/tkw1536/pkglib/pools"
|
||||
)
|
||||
|
||||
|
|
@ -70,8 +68,3 @@ func ReadFile(env Environment, path string) ([]byte, error) {
|
|||
// return the buffer contents!
|
||||
return buffer.Bytes(), nil
|
||||
}
|
||||
|
||||
// MustExec is like Exec, except that it returns true if the command exited successfully, and else false.
|
||||
func MustExec(ctx context.Context, env Environment, io stream.IOStream, workdir string, exe string, argv ...string) bool {
|
||||
return env.Exec(ctx, io, workdir, exe, argv...)() == 0
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,81 +0,0 @@
|
|||
package environment
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/tkw1536/goprogram/stream"
|
||||
)
|
||||
|
||||
// Exec executes a system command with the specified input/output streams, working directory, and arguments.
|
||||
//
|
||||
// The command is started immediatly.
|
||||
// The returned function is guaranteed to be non-nil and returns an exit code.
|
||||
//
|
||||
// If the command executes, the returns the exit code as soon as the process executes.
|
||||
// If the command can not be executed, the returned function is [ExecCommandErrorFunc] and returns [ExecCommandError].
|
||||
func (*Native) Exec(ctx context.Context, io stream.IOStream, workdir string, exe string, argv ...string) func() int {
|
||||
// setup the command
|
||||
cmd := exec.Command(exe, argv...)
|
||||
cmd.Dir = workdir
|
||||
cmd.Stdin = io.Stdin
|
||||
cmd.Stdout = io.Stdout
|
||||
cmd.Stderr = io.Stderr
|
||||
|
||||
// context is already cancelled, don't run it!
|
||||
if err := ctx.Err(); err != nil {
|
||||
return ExecCommandErrorFunc
|
||||
}
|
||||
|
||||
// start the command, but if something happens, return nil
|
||||
err := cmd.Start()
|
||||
zerolog.Ctx(ctx).Debug().Str("exe", exe).Strs("argv", argv).Err(err).Msg("exec.Command.Start")
|
||||
if err != nil {
|
||||
return ExecCommandErrorFunc
|
||||
}
|
||||
|
||||
waitdone := make(chan struct{}) // closed once Wait() below returns
|
||||
alldone := make(chan struct{}) // closed once the kill goroutine exits
|
||||
go func() {
|
||||
defer close(alldone)
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
err := cmd.Process.Kill()
|
||||
zerolog.Ctx(ctx).Debug().Str("exe", exe).Strs("argv", argv).Err(err).Msg("exec.Command.Kill")
|
||||
case <-waitdone:
|
||||
}
|
||||
}()
|
||||
|
||||
// create a new command
|
||||
return func() int {
|
||||
defer func() {
|
||||
// wait for the goroutine to exit
|
||||
close(waitdone)
|
||||
<-alldone
|
||||
}()
|
||||
|
||||
err := cmd.Wait()
|
||||
zerolog.Ctx(ctx).Debug().Str("exe", exe).Strs("argv", argv).Err(err).Msg("exec.Command.Wait")
|
||||
|
||||
// non-zero exit
|
||||
if err, ok := err.(*exec.ExitError); ok {
|
||||
return err.ExitCode()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return ExecCommandError
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Native) LookPathAbs(file string) (string, error) {
|
||||
path, err := exec.LookPath(file)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return n.Abs(path)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue