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 Mar 10, 2020
1 parent c34464c commit b004f71
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
29 changes: 29 additions & 0 deletions 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),
}
18 changes: 18 additions & 0 deletions 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),
)
}
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"
elvish_os "github.com/elves/elvish/pkg/eval/os"
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("os", elvish_os.Ns)
if sockpath != "" && dbpath != "" {
spawner := &daemonp.Daemon{
BinPath: binpath,
Expand Down
4 changes: 4 additions & 0 deletions website/ref/index.toml
Expand Up @@ -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:)"
Expand Down
10 changes: 10 additions & 0 deletions website/ref/os.md
@@ -0,0 +1,10 @@
<!-- toc -->

# 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

0 comments on commit b004f71

Please sign in to comment.