github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/cmd/gno/env.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  
     7  	"github.com/gnolang/gno/gnovm/pkg/gnoenv"
     8  	"github.com/gnolang/gno/tm2/pkg/commands"
     9  )
    10  
    11  type envCfg struct {
    12  	json bool
    13  }
    14  
    15  func newEnvCmd(io commands.IO) *commands.Command {
    16  	c := &envCfg{}
    17  	return commands.NewCommand(
    18  		commands.Metadata{
    19  			Name:       "env",
    20  			ShortUsage: "env [flags] <pkgsym>",
    21  			ShortHelp:  "`env` prints Gno environment information",
    22  		},
    23  		c,
    24  		func(_ context.Context, args []string) error {
    25  			return execEnv(c, args, io)
    26  		},
    27  	)
    28  }
    29  
    30  func (c *envCfg) RegisterFlags(fs *flag.FlagSet) {
    31  	fs.BoolVar(
    32  		&c.json,
    33  		"json",
    34  		false,
    35  		"prints the environment in JSON format instead of as a shell script.",
    36  	)
    37  }
    38  
    39  type envVar struct {
    40  	Key   string
    41  	Value string
    42  }
    43  
    44  func findEnv(env []envVar, name string) string {
    45  	for _, e := range env {
    46  		if e.Key == name {
    47  			return e.Value
    48  		}
    49  	}
    50  	return ""
    51  }
    52  
    53  type envPrinter func(vars []envVar, io commands.IO)
    54  
    55  func execEnv(cfg *envCfg, args []string, io commands.IO) error {
    56  	envs := []envVar{
    57  		// GNOROOT Should point to the local location of the GNO repository.
    58  		// It serves as the gno equivalent of `GOROOT`.
    59  		{Key: "GNOROOT", Value: gnoenv.RootDir()},
    60  		// GNOHOME Should point to the user local configuration.
    61  		// The most common place for this should be $HOME/gno.
    62  		{Key: "GNOHOME", Value: gnoenv.HomeDir()},
    63  	}
    64  
    65  	// Setup filters
    66  	filters := make([]envVar, len(args))
    67  	for i, arg := range args {
    68  		filters[i] = envVar{Key: arg, Value: findEnv(envs, arg)}
    69  	}
    70  
    71  	// Setup printer
    72  	var printerEnv envPrinter
    73  	if cfg.json {
    74  		printerEnv = printJSON
    75  	} else {
    76  		printerEnv = getPrinterShell(len(args) == 0)
    77  	}
    78  
    79  	// Print environements
    80  	if len(filters) > 0 {
    81  		printerEnv(filters, io)
    82  	} else {
    83  		printerEnv(envs, io)
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  func getPrinterShell(printkeys bool) envPrinter {
    90  	return func(vars []envVar, io commands.IO) {
    91  		for _, env := range vars {
    92  			if printkeys {
    93  				io.Printf("%s=%q\n", env.Key, env.Value)
    94  			} else {
    95  				io.Printf("%s\n", env.Value)
    96  			}
    97  		}
    98  	}
    99  }
   100  
   101  func printJSON(vars []envVar, io commands.IO) {
   102  	io.Println("{")
   103  	for i, env := range vars {
   104  		io.Printf("\t%q: %q", env.Key, env.Value)
   105  		if i != len(vars)-1 {
   106  			io.Printf(",")
   107  		}
   108  
   109  		// Jump to next line
   110  		io.Printf("\n")
   111  	}
   112  	io.Println("}")
   113  }