From ca68e6a1e432cbf06075d9f801f38599d0bcfaeb Mon Sep 17 00:00:00 2001 From: Mattias Wadman Date: Sat, 29 Jan 2022 11:30:51 +0100 Subject: [PATCH] interp: Add Platform() method to OS interface Felt bad that main should have to know about it --- fq.go | 4 +--- internal/script/script.go | 7 +++++++ pkg/cli/cli.go | 12 ++++++++++-- pkg/fqtest/fqtest.go | 2 +- pkg/interp/interp.go | 13 ++++++++++--- 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/fq.go b/fq.go index 1b8be7e6..92ff48ec 100644 --- a/fq.go +++ b/fq.go @@ -1,8 +1,6 @@ package main import ( - "runtime" - _ "github.com/wader/fq/format/all" "github.com/wader/fq/format/registry" "github.com/wader/fq/pkg/cli" @@ -11,5 +9,5 @@ import ( const version = "0.0.4" func main() { - cli.Main(registry.Default, version, runtime.GOOS, runtime.GOARCH) + cli.Main(registry.Default, version) } diff --git a/internal/script/script.go b/internal/script/script.go index 6c50c352..6c69af5f 100644 --- a/internal/script/script.go +++ b/internal/script/script.go @@ -82,6 +82,13 @@ func (cr *CaseRun) getEnvInt(name string) int { return n } +func (cr *CaseRun) Platform() interp.Platform { + return interp.Platform{ + OS: "testos", + Arch: "testarch", + } +} + func (cr *CaseRun) Stdin() interp.Input { return CaseRunInput{ FileReader: interp.FileReader{ diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 3054368b..3b091556 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -11,6 +11,7 @@ import ( "os" "os/signal" "path/filepath" + "runtime" "github.com/wader/fq/pkg/interp" "github.com/wader/fq/pkg/registry" @@ -75,6 +76,13 @@ func newStandardOS() *stdOS { } } +func (stdOS) Platform() interp.Platform { + return interp.Platform{ + OS: runtime.GOOS, + Arch: runtime.GOARCH, + } +} + type fdTerminal uintptr func (fd fdTerminal) Size() (int, int) { @@ -224,7 +232,7 @@ func (o *stdOS) Close() error { return nil } -func Main(r *registry.Registry, version string, osStr string, archStr string) { +func Main(r *registry.Registry, version string) { os.Exit(func() int { defer maybeProfile()() maybeLogFile() @@ -238,7 +246,7 @@ func Main(r *registry.Registry, version string, osStr string, archStr string) { return 1 } - if err := i.Main(context.Background(), sos.Stdout(), version, osStr, archStr); err != nil { + if err := i.Main(context.Background(), sos.Stdout(), version); err != nil { if ex, ok := err.(interp.Exiter); ok { //nolint:errorlint return ex.ExitCode() } diff --git a/pkg/fqtest/fqtest.go b/pkg/fqtest/fqtest.go index 5b83755a..17e3b715 100644 --- a/pkg/fqtest/fqtest.go +++ b/pkg/fqtest/fqtest.go @@ -41,7 +41,7 @@ func TestPath(t *testing.T, registry *registry.Registry) { t.Fatal(err) } - err = i.Main(context.Background(), cr.Stdout(), "testversion", "testos", "testarch") + err = i.Main(context.Background(), cr.Stdout(), "testversion") if err != nil { if ex, ok := err.(interp.Exiter); ok { //nolint:errorlint cr.ActualExitCode = ex.ExitCode() diff --git a/pkg/interp/interp.go b/pkg/interp/interp.go index be82049b..a4493467 100644 --- a/pkg/interp/interp.go +++ b/pkg/interp/interp.go @@ -127,7 +127,13 @@ type Output interface { Terminal } +type Platform struct { + OS string + Arch string +} + type OS interface { + Platform() Platform Stdin() Input Stdout() Output Stderr() Output @@ -359,17 +365,18 @@ func (i *Interp) Stop() { i.interruptStack.Stop() } -func (i *Interp) Main(ctx context.Context, output Output, versionStr string, osStr string, archStr string) error { +func (i *Interp) Main(ctx context.Context, output Output, versionStr string) error { var args []interface{} for _, a := range i.os.Args() { args = append(args, a) } + platform := i.os.Platform() input := map[string]interface{}{ "args": args, "version": versionStr, - "os": osStr, - "arch": archStr, + "os": platform.OS, + "arch": platform.Arch, } iter, err := i.EvalFunc(ctx, input, "_main", nil, output)