Skip to content

Commit

Permalink
Fixup for #1348.
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaq committed Jul 3, 2021
1 parent 3ba0397 commit 00c7205
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 51 deletions.
55 changes: 31 additions & 24 deletions pkg/buildinfo/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,45 +29,52 @@ var Reproducible = "false"
// Program is the buildinfo subprogram.
var Program prog.Program = program{}

type Buildinfo struct {
// Type contains all the build information fields.
type Type struct {
Version string `json:"version"`
Reproducible bool `json:"reproducible"`
GoVersion string `json:"goversion"`
}

func (Buildinfo) IsStructMap() {}
func (Type) IsStructMap() {}

// Value contains all the build information.
var Value = Type{
Version: Version + VersionSuffix,
Reproducible: Reproducible == "true",
GoVersion: runtime.Version(),
}

type program struct{}

func (program) ShouldRun(f *prog.Flags) bool { return f.Version || f.BuildInfo }

func GetBuildInfo() Buildinfo {
return Buildinfo{
Version: Version + VersionSuffix,
Reproducible: Reproducible == "true",
GoVersion: runtime.Version(),
}
}

func (program) Run(fds [3]*os.File, f *prog.Flags, _ []string) error {
bi := GetBuildInfo()
switch {
case f.JSON:
// Note: The only way this will be executed is if option -buildinfo is also present.
// See the ShouldRun() method above.
b, err := json.Marshal(bi)
if err != nil {
panic("unexpected buildinfo related error")
}
fmt.Fprintln(fds[1], string(b))
case f.BuildInfo:
fmt.Fprintln(fds[1], "Version:", bi.Version)
fmt.Fprintln(fds[1], "Go version:", bi.GoVersion)
fmt.Fprintln(fds[1], "Reproducible build:", bi.Reproducible)
if f.JSON {
fmt.Fprintln(fds[1], mustToJSON(Value))
} else {
fmt.Fprintln(fds[1], "Version:", Value.Version)
fmt.Fprintln(fds[1], "Go version:", Value.GoVersion)
fmt.Fprintln(fds[1], "Reproducible build:", Value.Reproducible)
}
case f.Version:
fmt.Fprintln(fds[1], bi.Version)
if f.JSON {
fmt.Fprintln(fds[1], mustToJSON(Value.Version))
} else {
fmt.Fprintln(fds[1], Value.Version)
}
default:
panic("unexpected buildinfo related error")
panic("should not run buildinfo")
}
return nil
}

func mustToJSON(v interface{}) string {
b, err := json.Marshal(v)
if err != nil {
panic(err)
}
return string(b)
}
33 changes: 13 additions & 20 deletions pkg/buildinfo/buildinfo_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package buildinfo

import (
"encoding/json"
"fmt"
"runtime"
"testing"

"src.elv.sh/pkg/prog"
Expand All @@ -16,8 +14,17 @@ func TestVersion(t *testing.T) {

prog.Run(f.Fds(), Elvish("-version"), Program)

bi := GetBuildInfo()
f.TestOut(t, 1, bi.Version+"\n")
f.TestOut(t, 1, Value.Version+"\n")
f.TestOut(t, 2, "")
}

func TestVersion_JSON(t *testing.T) {
f := Setup()
defer f.Cleanup()

prog.Run(f.Fds(), Elvish("-version", "-json"), Program)

f.TestOut(t, 1, mustToJSON(Value.Version)+"\n")
f.TestOut(t, 2, "")
}

Expand All @@ -27,11 +34,10 @@ func TestBuildInfo(t *testing.T) {

prog.Run(f.Fds(), Elvish("-buildinfo"), Program)

bi := GetBuildInfo()
f.TestOut(t, 1,
fmt.Sprintf(
"Version: %v\nGo version: %v\nReproducible build: %v\n",
bi.Version, bi.GoVersion, bi.Reproducible))
Value.Version, Value.GoVersion, Value.Reproducible))
f.TestOut(t, 2, "")
}

Expand All @@ -41,19 +47,6 @@ func TestBuildInfo_JSON(t *testing.T) {

prog.Run(f.Fds(), Elvish("-buildinfo", "-json"), Program)

f.TestOut(t, 1,
mustToJSON(Buildinfo{
Version: Version + VersionSuffix,
GoVersion: runtime.Version(),
Reproducible: Reproducible == "true",
})+"\n")
f.TestOut(t, 1, mustToJSON(Value)+"\n")
f.TestOut(t, 2, "")
}

func mustToJSON(v interface{}) string {
b, err := json.Marshal(v)
if err != nil {
panic(err)
}
return string(b)
}
6 changes: 2 additions & 4 deletions pkg/eval/builtin_ns.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,15 @@ import (
//
// @cf buildinfo

var bi = buildinfo.GetBuildInfo()

var builtinNs = NsBuilder{
"_": vars.NewBlackhole(),
"pid": vars.NewReadOnly(strconv.Itoa(syscall.Getpid())),
"ok": vars.NewReadOnly(OK),
"nil": vars.NewReadOnly(nil),
"true": vars.NewReadOnly(true),
"false": vars.NewReadOnly(false),
"buildinfo": vars.NewReadOnly(bi),
"version": vars.NewReadOnly(bi.Version),
"buildinfo": vars.NewReadOnly(buildinfo.Value),
"version": vars.NewReadOnly(buildinfo.Value.Version),
"paths": vars.NewEnvListVar("PATH"),
}

Expand Down
8 changes: 5 additions & 3 deletions website/ref/language.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,11 @@ that behave like maps with some restrictions.
A pseudo-map has a fixed set of keys whose values can be accessed by
[indexing](#indexing) like you would for a regular [map](#map). Similarly, you
can use commands like [`keys`](./builtin.html#keys) and
[`has-key`](./builtin.html#keys) on such objects. However, unlike a normal map
it is not possible to add new keys, remove existing keys, or even assign a new
value to an existing key. In other words, a pseudo-map is immutable.
[`has-key`](./builtin.html#keys) on such objects.

Unlike a normal map, it is currently not possible to create a modified version
of an existing pseudo-map: it is not possible to create a pseudo-map with new
keys, without existing keys, or with a different value for a given key.

The pseudo-map mechanism is often used for introspection. For example,
[exceptions](#exception), [user-defined functions](#function), and
Expand Down

0 comments on commit 00c7205

Please sign in to comment.