Browse Source

jpictl: introduce `-e` for `jpictl env` to export variables

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/2/head
Alejandro Mery 1 year ago
parent
commit
6ee848e6ca
  1. 10
      cmd/jpictl/env.go
  2. 15
      pkg/zones/env.go

10
cmd/jpictl/env.go

@ -16,11 +16,19 @@ var envCmd = &cobra.Command{
return err
}
_, err = m.Env().WriteTo(os.Stdout)
_, err = m.Env(*envExport).WriteTo(os.Stdout)
return err
},
}
// Command Flags
var (
envExport *bool
)
func init() {
rootCmd.AddCommand(envCmd)
envExport = envCmd.PersistentFlags().BoolP("export", "e", false,
"export generated variables")
}

15
pkg/zones/env.go

@ -10,12 +10,15 @@ import (
// Env is a shell environment factory for this cluster
type Env struct {
ZoneIterator
export bool
}
// Env returns a shell environment factory
func (m *Zones) Env() *Env {
func (m *Zones) Env(export bool) *Env {
return &Env{
ZoneIterator: m,
export: export,
}
}
@ -63,7 +66,13 @@ func (m *Env) writeEnvVarFn(w io.Writer, fn func(*Env) string, name string, args
m.writeEnvVar(w, value, name, args...)
}
func (*Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
func (m *Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
var prefix string
if m.export {
prefix = "export "
}
if len(args) > 0 {
name = fmt.Sprintf(name, args...)
}
@ -71,7 +80,7 @@ func (*Env) writeEnvVar(w io.Writer, value string, name string, args ...any) {
if name != "" {
value = strings.TrimSpace(value)
_, _ = fmt.Fprintf(w, "%s=%q\n", name, value)
_, _ = fmt.Fprintf(w, "%s%s=%q\n", prefix, name, value)
}
}

Loading…
Cancel
Save