From b004f713d7221ee73a4e7e65828e5f79d0f4279a Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Thu, 27 Feb 2020 16:35:22 -0800 Subject: [PATCH] Add sys: module to expose platform and arch vars Resolves #916 --- pkg/eval/os/os.go | 29 +++++++++++++++++++++++++++++ pkg/eval/os/os_test.go | 18 ++++++++++++++++++ pkg/program/shell/runtime.go | 2 ++ website/ref/index.toml | 4 ++++ website/ref/os.md | 10 ++++++++++ 5 files changed, 63 insertions(+) create mode 100644 pkg/eval/os/os.go create mode 100644 pkg/eval/os/os_test.go create mode 100644 website/ref/os.md diff --git a/pkg/eval/os/os.go b/pkg/eval/os/os.go new file mode 100644 index 0000000000..8c458771eb --- /dev/null +++ b/pkg/eval/os/os.go @@ -0,0 +1,29 @@ +// Package `os` exposes variables and functions that deal with the specific +// platform being run on; such as the OS name and CPU architecture. +package os + +import ( + "runtime" + + "github.com/elves/elvish/pkg/eval" + "github.com/elves/elvish/pkg/eval/vars" +) + +//elvdoc:var arch +// +// The architecture of the platform; e.g., amd64, arm, ppc. +// This corresponds to Go's +// [`GOARCH`](https://pkg.go.dev/runtime?tab=doc#pkg-constants) constant. +// This is read-only. + +//elvdoc:var platform +// +// The name of the platform; e.g., darwin (macOS), linux, etc. +// This corresponds to Go's +// [`GOOS`](https://pkg.go.dev/runtime?tab=doc#pkg-constants) constant. +// This is read-only. + +var Ns = eval.Ns{ + "arch": vars.NewReadOnly(runtime.GOARCH), + "platform": vars.NewReadOnly(runtime.GOOS), +} diff --git a/pkg/eval/os/os_test.go b/pkg/eval/os/os_test.go new file mode 100644 index 0000000000..9a2a3f3dca --- /dev/null +++ b/pkg/eval/os/os_test.go @@ -0,0 +1,18 @@ +package os + +import ( + "runtime" + "testing" + + "github.com/elves/elvish/pkg/eval" +) + +var That = eval.That + +func TestOs(t *testing.T) { + setup := func(ev *eval.Evaler) { ev.Builtin.AddNs("os", Ns) } + eval.TestWithSetup(t, setup, + That(`put $os:arch`).Puts(runtime.GOARCH), + That(`put $os:platform`).Puts(runtime.GOOS), + ) +} diff --git a/pkg/program/shell/runtime.go b/pkg/program/shell/runtime.go index 6d182c9460..04798788fe 100644 --- a/pkg/program/shell/runtime.go +++ b/pkg/program/shell/runtime.go @@ -15,6 +15,7 @@ import ( "github.com/elves/elvish/pkg/eval/re" "github.com/elves/elvish/pkg/eval/store" "github.com/elves/elvish/pkg/eval/str" + elvish_os "github.com/elves/elvish/pkg/eval/os" daemonp "github.com/elves/elvish/pkg/program/daemon" ) @@ -74,6 +75,7 @@ func InitRuntime(binpath, sockpath, dbpath string) (*eval.Evaler, string) { ev.InstallModule("math", eval.MathNs) ev.InstallModule("re", re.Ns) ev.InstallModule("str", str.Ns) + ev.InstallModule("os", elvish_os.Ns) if sockpath != "" && dbpath != "" { spawner := &daemonp.Daemon{ BinPath: binpath, diff --git a/website/ref/index.toml b/website/ref/index.toml index 5b0dec4fe1..83a3cd3a50 100644 --- a/website/ref/index.toml +++ b/website/ref/index.toml @@ -10,6 +10,10 @@ title = "The Builtin Module" name = "edit" title = "The Editor Module (edit:)" +[[articles]] +name = "os" +title = "The Operating System Module (os:)" + [[articles]] name = "re" title = "The Regular Expression Module (re:)" diff --git a/website/ref/os.md b/website/ref/os.md new file mode 100644 index 0000000000..a4652625e6 --- /dev/null +++ b/website/ref/os.md @@ -0,0 +1,10 @@ + + +# Introduction + +The `os:` module provides platform specific constants. + +Function usages are given in the same format as in the +reference doc for the [builtin module](builtin.html). + +@elvdoc -ns os: -dir ../pkg/eval/os