1
1
mirror of https://github.com/wader/fq.git synced 2024-11-22 07:16:49 +03:00

interp: Add Platform() method to OS interface

Felt bad that main should have to know about it
This commit is contained in:
Mattias Wadman 2022-01-29 11:30:51 +01:00
parent 94a863e69a
commit ca68e6a1e4
5 changed files with 29 additions and 9 deletions

4
fq.go
View File

@ -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)
}

View File

@ -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{

View File

@ -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()
}

View File

@ -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()

View File

@ -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)