Skip to content

Commit

Permalink
interp: Add Platform() method to OS interface
Browse files Browse the repository at this point in the history
Felt bad that main should have to know about it
  • Loading branch information
wader committed Jan 29, 2022
1 parent 94a863e commit ca68e6a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
4 changes: 1 addition & 3 deletions 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"
Expand All @@ -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)
}
7 changes: 7 additions & 0 deletions internal/script/script.go
Expand Up @@ -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{
Expand Down
12 changes: 10 additions & 2 deletions pkg/cli/cli.go
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"os/signal"
"path/filepath"
"runtime"

"github.com/wader/fq/pkg/interp"
"github.com/wader/fq/pkg/registry"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand All @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/fqtest/fqtest.go
Expand Up @@ -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()
Expand Down
13 changes: 10 additions & 3 deletions pkg/interp/interp.go
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ca68e6a

Please sign in to comment.