diff --git a/pkg/eval/sys/sys.go b/pkg/eval/sys/sys.go new file mode 100644 index 0000000000..34a5291d2f --- /dev/null +++ b/pkg/eval/sys/sys.go @@ -0,0 +1,25 @@ +// Package sys exposes variables and functions that deal with the specific +// system being run on. Such as the OS name and CPU architecture. +package sys + +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 is read-only. + +//elvdoc:var platform +// +// The name of the platform; e.g., darwin (macOS), linux. This is read-only. + +var Ns = eval.NewNs() + +func init() { + Ns.Add("arch", vars.NewReadOnly(runtime.GOARCH)) + Ns.Add("platform", vars.NewReadOnly(runtime.GOOS)) +} diff --git a/pkg/eval/sys/sys_test.go b/pkg/eval/sys/sys_test.go new file mode 100644 index 0000000000..cb1106ec5d --- /dev/null +++ b/pkg/eval/sys/sys_test.go @@ -0,0 +1,17 @@ +package sys + +import ( + "runtime" + "testing" + + "github.com/elves/elvish/pkg/eval" +) + +func TestMath(t *testing.T) { + setup := func(ev *eval.Evaler) { ev.Builtin.AddNs("sys", Ns) } + That := eval.That + eval.TestWithSetup(t, setup, + That(`put $sys:arch`).Puts(runtime.GOARCH), + That(`put $sys:platform`).Puts(runtime.GOOS), + ) +} diff --git a/pkg/program/shell/runtime.go b/pkg/program/shell/runtime.go index 6d182c9460..9572b58f8b 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" + "github.com/elves/elvish/pkg/eval/sys" 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("sys", sys.Ns) if sockpath != "" && dbpath != "" { spawner := &daemonp.Daemon{ BinPath: binpath, diff --git a/website/ref/index.toml b/website/ref/index.toml index 5b0dec4fe1..725a20bd05 100644 --- a/website/ref/index.toml +++ b/website/ref/index.toml @@ -14,6 +14,10 @@ title = "The Editor Module (edit:)" name = "re" title = "The Regular Expression Module (re:)" +[[articles]] +name = "sys" +title = "The System Module (sys:)" + [[articles]] name = "epm" title = "The Elvish Package Manager (epm:)" diff --git a/website/ref/sys.md b/website/ref/sys.md new file mode 100644 index 0000000000..e356482fb9 --- /dev/null +++ b/website/ref/sys.md @@ -0,0 +1,10 @@ + + +# Introduction + +The `sys:` module provides platform specific functions and constants. + +Function usages are given in the same format as in the +reference doc for the [builtin module](builtin.html). + +@elvdoc -ns sys: -dir ../pkg/eval/sys