Add support for provisioning and rebuilding via interface

This commit is contained in:
Tom 2023-07-09 11:18:14 +02:00
parent f5c5999f44
commit ddb4bb3546
76 changed files with 1306 additions and 625 deletions

View file

@ -2,11 +2,35 @@ package barrel
import (
"context"
"fmt"
"github.com/alessio/shellescape"
"github.com/tkw1536/goprogram/exit"
"github.com/tkw1536/pkglib/stream"
)
// Shell executes a shell command inside the instance.
func (barrel *Barrel) Shell(ctx context.Context, io stream.IOStream, argv ...string) func() int {
return barrel.Stack().Exec(ctx, io, "barrel", "/bin/sh", append([]string{"/user_shell.sh"}, argv...)...)
type ExitError int
func (ee ExitError) Error() string {
return fmt.Sprintf("Exited with code %d", int(ee))
}
func (ee ExitError) Code() exit.ExitCode {
return exit.ExitCode(ee)
}
// Shell executes a shell with the given command line arguments inside the container.
// If an error occurs, it is of type ExitError.
func (barrel *Barrel) Shell(ctx context.Context, io stream.IOStream, argv ...string) error {
code := barrel.Stack().Exec(ctx, io, "barrel", "/bin/sh", append([]string{"/user_shell.sh"}, argv...)...)()
if code != 0 {
return ExitError(code)
}
return nil
}
// ShellScript quotes the given command and executes it as a shell script inside the container.
func (barrel *Barrel) ShellScript(ctx context.Context, io stream.IOStream, commands ...string) error {
command := shellescape.QuoteCommand(commands)
return barrel.Shell(ctx, io, "-c", command)
}