Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 'gno env' subcommand #1233

Merged
merged 28 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
485dd31
wip: add gnodev
gfanton Oct 11, 2023
08177f9
wip: move env to gnoenv package
gfanton Oct 11, 2023
b5ed456
fix: gnoroot integration naming
gfanton Oct 11, 2023
9f372d5
fix: move away homedir from tm2
gfanton Oct 11, 2023
b91abfd
fix: try to move HomeDir out of tm2
gfanton Oct 11, 2023
e706698
Merge remote-tracking branch 'origin/master' into feat/gno-env
gfanton Oct 18, 2023
609a30d
chore: lint comment
gfanton Oct 18, 2023
1142c08
chore: lint code
gfanton Oct 18, 2023
02eaefb
chore: lint
gfanton Oct 18, 2023
7234217
fix: test gnoenv
gfanton Oct 18, 2023
5258180
fix: gnoroot test
gfanton Oct 19, 2023
93792d8
Merge remote-tracking branch 'origin/master' into feat/gno-env
gfanton Oct 23, 2023
4cabaa8
chore: lint typo and comment
gfanton Oct 24, 2023
3bf298d
fix: add `gno env` test
gfanton Oct 24, 2023
c0a017d
chore: lint
gfanton Oct 24, 2023
8548d26
chore: lint comment
gfanton Oct 24, 2023
6bd0003
chore: cleanup test
gfanton Oct 25, 2023
ee2640c
Merge branch 'master' into feat/gno-env
gfanton Oct 25, 2023
807431e
Merge branch 'master' into feat/gno-env
gfanton Nov 6, 2023
d3341e0
Merge branch 'master' into feat/gno-env
gfanton Nov 6, 2023
fd81da3
Merge remote-tracking branch 'master' into feat/gno-env
gfanton Nov 7, 2023
54fb0ac
chore: fixup merge
gfanton Nov 7, 2023
93c3278
Merge branch 'master' into feat/gno-env
gfanton Nov 20, 2023
cc67ff4
fix: merge fixup
gfanton Nov 20, 2023
3bd97b5
chore: rename MustGuessRootDir to RootDir
gfanton Nov 20, 2023
f24d5ec
fix: use new root with config
gfanton Nov 20, 2023
bf522e9
Merge branch 'master' into feat/gno-env
gfanton Nov 20, 2023
e76b154
Merge branch 'master' into feat/gno-env
gfanton Nov 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 2 additions & 25 deletions gno.land/pkg/gnoland/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package gnoland

import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
"github.com/gnolang/gno/gnovm/pkg/gnoroot"
"github.com/gnolang/gno/tm2/pkg/amino"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
"github.com/gnolang/gno/tm2/pkg/crypto"
Expand Down Expand Up @@ -36,7 +35,7 @@ func NewAppOptions() *AppOptions {
return &AppOptions{
Logger: log.NewNopLogger(),
DB: dbm.NewMemDB(),
GnoRootDir: GuessGnoRootDir(),
GnoRootDir: gnoroot.MustGuessGnoRootDir(),
}
}

Expand Down Expand Up @@ -194,25 +193,3 @@ func EndBlocker(vmk vm.VMKeeperI) func(ctx sdk.Context, req abci.RequestEndBlock
return abci.ResponseEndBlock{}
}
}

func GuessGnoRootDir() string {
var rootdir string

// First try to get the root directory from the GNOROOT environment variable.
if rootdir = os.Getenv("GNOROOT"); rootdir != "" {
return filepath.Clean(rootdir)
}

if gobin, err := exec.LookPath("go"); err == nil {
// If GNOROOT is not set, try to guess the root directory using the `go list` command.
cmd := exec.Command(gobin, "list", "-m", "-mod=mod", "-f", "{{.Dir}}", "github.com/gnolang/gno")
out, err := cmd.CombinedOutput()
if err != nil {
panic(fmt.Errorf("invalid gno directory %q: %w", rootdir, err))
}

return strings.TrimSpace(string(out))
}

panic("no go binary available, unable to determine gno root-dir path")
}
4 changes: 2 additions & 2 deletions gno.land/pkg/integration/testing_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"testing"
"time"

"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/gnovm/pkg/gnoroot"
gfanton marked this conversation as resolved.
Show resolved Hide resolved
"github.com/gnolang/gno/tm2/pkg/bft/node"
"github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/commands"
Expand Down Expand Up @@ -51,7 +51,7 @@ func SetupGnolandTestScript(t *testing.T, txtarDir string) testscript.Params {

// `gnoRootDir` should point to the local location of the gno repository.
// It serves as the gno equivalent of GOROOT.
gnoRootDir := gnoland.GuessGnoRootDir()
gnoRootDir := gnoroot.MustGuessGnoRootDir()
gfanton marked this conversation as resolved.
Show resolved Hide resolved

// `gnoHomeDir` should be the local directory where gnokey stores keys.
gnoHomeDir := filepath.Join(tmpdir, "gno")
Expand Down
11 changes: 9 additions & 2 deletions gnovm/Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
GNOROOT_DIR ?= $(abspath $(lastword $(MAKEFILE_LIST))/../../)

LDFLAGS := -X github.com/gnolang/gno/gnovm/pkg/gnoroot._GNOROOT=$(GNOROOT_DIR)
gfanton marked this conversation as resolved.
Show resolved Hide resolved

.PHONY: help
help:
@echo "Available make commands:"
@cat Makefile | grep '^[a-z][^:]*:' | cut -d: -f1 | sort | sed 's/^/ /'

rundep=go run -modfile ../misc/devdeps/go.mod

# We can't use '-trimpath' yet as amino use absolute path from call stack
# to find some directory
GOBUILD_FLAGS := -ldflags "$(LDFLAGS)"
.PHONY: build
build:
go build -o build/gno ./cmd/gno
go build $(GOBUILD_FLAGS) -o build/gno ./cmd/gno

.PHONY: install
install:
go install ./cmd/gno
go install $(GOBUILD_FLAGS) ./cmd/gno

.PHONY: clean
clean:
Expand Down
3 changes: 2 additions & 1 deletion gnovm/cmd/gno/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/gnolang/gno/gnovm/pkg/doc"
"github.com/gnolang/gno/gnovm/pkg/gnomod"
"github.com/gnolang/gno/gnovm/pkg/gnoroot"
"github.com/gnolang/gno/tm2/pkg/commands"
)

Expand Down Expand Up @@ -77,7 +78,7 @@ func (c *docCfg) RegisterFlags(fs *flag.FlagSet) {
func execDoc(cfg *docCfg, args []string, io *commands.IO) error {
// guess opts.RootDir
if cfg.rootDir == "" {
cfg.rootDir = guessRootDir()
cfg.rootDir = gnoroot.MustGuessGnoRootDir()
}

wd, err := os.Getwd()
Expand Down
147 changes: 147 additions & 0 deletions gnovm/cmd/gno/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package main

import (
"context"
"flag"

"github.com/gnolang/gno/gnovm/pkg/gnoroot"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/crypto/keys/client"
)

type envCfg struct {
json bool
}

func newEnvCmd(io *commands.IO) *commands.Command {
c := &envCfg{}
return commands.NewCommand(
commands.Metadata{
Name: "env",
ShortUsage: "env [flags] <pkgsym>",
ShortHelp: "Env prints Gno environment information",
},
c,
func(_ context.Context, args []string) error {
return execEnv(c, args, io)
},

Check warning on line 27 in gnovm/cmd/gno/env.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/env.go#L26-L27

Added lines #L26 - L27 were not covered by tests
)
}

func (c *envCfg) RegisterFlags(fs *flag.FlagSet) {
// XXX: add flags
fs.BoolVar(
&c.json,
"json",
false,
"prints the environment in JSON format instead of as a shell script.",
)

// XXX: does those go orignal flags make sense here ?
// fs.BoolVar(
// &c.unset,
// "u",
// false,
// "unsets the default setting for the named environment variables",
// )

// fs.BoolVar(
// &c.warp,
// "w",
// false,
// "changes the default settings of the named environment variables to the given values",
// )
}

type envVar struct {
Key string
Value string
}

type envVars []envVar

func (vars *envVars) Set(key, value string) {
for _, env := range *vars {
if env.Key == key {
env.Value = value
}

Check warning on line 67 in gnovm/cmd/gno/env.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/env.go#L63-L67

Added lines #L63 - L67 were not covered by tests
}
*vars = append(*vars, envVar{Key: key, Value: value})

Check warning on line 69 in gnovm/cmd/gno/env.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/env.go#L69

Added line #L69 was not covered by tests
}

func (vars envVars) Get(key string) string {
for _, env := range vars {
if env.Key == key {
return env.Value
}

Check warning on line 76 in gnovm/cmd/gno/env.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/env.go#L72-L76

Added lines #L72 - L76 were not covered by tests
}
return ""

Check warning on line 78 in gnovm/cmd/gno/env.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/env.go#L78

Added line #L78 was not covered by tests
}

type envPrinter func(vars envVars, io *commands.IO)

func execEnv(cfg *envCfg, args []string, io *commands.IO) error {
gnorootVar, _ := gnoroot.GuessGnoRootDir()

envs := envVars{}

// GNOROOT:
// Should point to the local location of the GNO repository.
// It serves as the gno equivalent of `GOROOT`.
envs.Set("GNOROOT", gnorootVar)

// GNOHOME:
envs.Set("GNOHOME", client.HomeDir())

// Setup filters
filters := envVars{}
for _, arg := range args {
filters.Set(arg, envs.Get(arg))
}

Check warning on line 100 in gnovm/cmd/gno/env.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/env.go#L83-L100

Added lines #L83 - L100 were not covered by tests

// Setup printer
var printerEnv envPrinter
{
if cfg.json {
printerEnv = printJSON
} else {
printerEnv = getPrinterShell(len(args) == 0)
}

Check warning on line 109 in gnovm/cmd/gno/env.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/env.go#L103-L109

Added lines #L103 - L109 were not covered by tests
}

// Print environements
if len(filters) > 0 {
printerEnv(filters, io)
} else {
printerEnv(envs, io)
}

Check warning on line 117 in gnovm/cmd/gno/env.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/env.go#L113-L117

Added lines #L113 - L117 were not covered by tests

return nil

Check warning on line 119 in gnovm/cmd/gno/env.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/env.go#L119

Added line #L119 was not covered by tests
}

func getPrinterShell(printkeys bool) envPrinter {
return func(vars envVars, io *commands.IO) {
for _, env := range vars {
if printkeys {
io.Printf("%s=%q\n", env.Key, env.Value)
} else {
io.Printf("%s\n", env.Value)
}

Check warning on line 129 in gnovm/cmd/gno/env.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/env.go#L122-L129

Added lines #L122 - L129 were not covered by tests

}
}
}

func printJSON(vars envVars, io *commands.IO) {
io.Println("{")
for i, env := range vars {
io.Printf("\t%q: %q", env.Key, env.Value)
if i != len(vars)-1 {
io.Printf(",")
}

Check warning on line 141 in gnovm/cmd/gno/env.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/env.go#L135-L141

Added lines #L135 - L141 were not covered by tests

// Jump to next line
io.Printf("\n")

Check warning on line 144 in gnovm/cmd/gno/env.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/env.go#L144

Added line #L144 was not covered by tests
}
io.Println("}")

Check warning on line 146 in gnovm/cmd/gno/env.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/env.go#L146

Added line #L146 was not covered by tests
}
3 changes: 2 additions & 1 deletion gnovm/cmd/gno/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"

"github.com/gnolang/gno/gnovm/pkg/gnoroot"
"github.com/gnolang/gno/tm2/pkg/commands"
osm "github.com/gnolang/gno/tm2/pkg/os"
)
Expand Down Expand Up @@ -51,7 +52,7 @@ func execLint(cfg *lintCfg, args []string, io *commands.IO) error {
rootDir = cfg.rootDir
)
if rootDir == "" {
rootDir = guessRootDir()
rootDir = gnoroot.MustGuessGnoRootDir()
}

pkgPaths, err := gnoPackagesFromArgs(args)
Expand Down
1 change: 1 addition & 0 deletions gnovm/cmd/gno/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func newGnocliCmd(io *commands.IO) *commands.Command {
newCleanCmd(io),
newReplCmd(),
newDocCmd(io),
newEnvCmd(io),
// fmt -- gofmt
// graph
// vendor -- download deps from the chain in vendor/
Expand Down
3 changes: 2 additions & 1 deletion gnovm/cmd/gno/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"os"
"strings"

"github.com/gnolang/gno/gnovm/pkg/gnoroot"
"github.com/gnolang/gno/gnovm/pkg/repl"
"github.com/gnolang/gno/tm2/pkg/commands"
)
Expand Down Expand Up @@ -80,7 +81,7 @@
}

if cfg.rootDir == "" {
cfg.rootDir = guessRootDir()
cfg.rootDir = gnoroot.MustGuessGnoRootDir()

Check warning on line 84 in gnovm/cmd/gno/repl.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/repl.go#L84

Added line #L84 was not covered by tests
}

if !cfg.skipUsage {
Expand Down
3 changes: 2 additions & 1 deletion gnovm/cmd/gno/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/gnoroot"
"github.com/gnolang/gno/gnovm/tests"
"github.com/gnolang/gno/tm2/pkg/commands"
)
Expand Down Expand Up @@ -65,7 +66,7 @@ func execRun(cfg *runCfg, args []string, io *commands.IO) error {
}

if cfg.rootDir == "" {
cfg.rootDir = guessRootDir()
cfg.rootDir = gnoroot.MustGuessGnoRootDir()
}

stdin := io.In
Expand Down
3 changes: 2 additions & 1 deletion gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/gnomod"
"github.com/gnolang/gno/gnovm/pkg/gnoroot"
"github.com/gnolang/gno/gnovm/tests"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/errors"
Expand Down Expand Up @@ -179,7 +180,7 @@

// guess opts.RootDir
if cfg.rootDir == "" {
cfg.rootDir = guessRootDir()
cfg.rootDir = gnoroot.MustGuessGnoRootDir()

Check warning on line 183 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L183

Added line #L183 was not covered by tests
}

paths, err := gnoPackagesFromArgs(args)
Expand Down
5 changes: 3 additions & 2 deletions gnovm/cmd/gno/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"time"

gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/gnoroot"
)

func isGnoFile(f fs.DirEntry) bool {
Expand Down Expand Up @@ -109,7 +110,7 @@
return fmt.Sprintf("%.2fs", d.Seconds())
}

func guessRootDir() string {
func guesRootDir() string {

Check warning on line 113 in gnovm/cmd/gno/util.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/util.go#L113

Added line #L113 was not covered by tests
// try to get the root directory from the GNOROOT environment variable.
if rootdir := os.Getenv("GNOROOT"); rootdir != "" {
return filepath.Clean(rootdir)
Expand Down Expand Up @@ -158,7 +159,7 @@
if err != nil {
return "", err
}
pkgPath := strings.TrimPrefix(absPkgPath, guessRootDir())
pkgPath := strings.TrimPrefix(absPkgPath, gnoroot.MustGuessGnoRootDir())

Check warning on line 162 in gnovm/cmd/gno/util.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/util.go#L162

Added line #L162 was not covered by tests

return filepath.Join(absOutput, pkgPath), nil
}
Expand Down
Loading
Loading