Skip to content

Commit

Permalink
Add sys: module to expose platform and arch vars
Browse files Browse the repository at this point in the history
Resolves #916
  • Loading branch information
krader1961 committed Feb 28, 2020
1 parent 7868b68 commit 15485ae
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 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))
}
17 changes: 17 additions & 0 deletions 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),
)
}
2 changes: 2 additions & 0 deletions pkg/program/shell/runtime.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions website/ref/index.toml
Expand Up @@ -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:)"
Expand Down
10 changes: 10 additions & 0 deletions website/ref/sys.md
@@ -0,0 +1,10 @@
<!-- toc -->

# 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

0 comments on commit 15485ae

Please sign in to comment.