go – Golang – set environment variable from inside COBRA command-ThrowExceptions

Exception or error:

In vanilla go, it seems pretty trivial to set an environment variable. The os library has a Setenv function that should get the job done. I’m running into trouble doing that inside of a cobra command. Is there a simple way that I’m missing, or is this functionality not supported at this point?

Example of what I’m trying to do:

package main

import (
    "fmt"
    "os"

    "github.com/spf13/cobra"
    "github.com/spf13/pflag"

    "k8s.io/cli-runtime/pkg/genericclioptions"
)

func main() {
    flags := pflag.NewFlagSet("ex-com", pflag.ExitOnError)
    pflag.CommandLine = flags

    root := newCmdEx(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr})
    if err := root.Execute(); err != nil {
        fmt.Fprintf(os.Stdout, "Error: %[1]s", err.Error())
        os.Exit(1)
    }
    os.Exit(0)
}

type ExOpOptions struct {
    configFlags genericclioptions.ConfigFlags
}

func NewExOpOptions(streams genericclioptions.IOStreams) *ExOpOptions {
    return &ExOpOptions{}
}

func newCmdEx(streams genericclioptions.IOStreams) *cobra.Command {
    o := NewExOpOptions(streams)

    cmd := &cobra.Command{
        RunE: func(c *cobra.Command, args []string) error {
            return o.Run()
        },
    }

    o.configFlags.AddFlags(cmd.Flags())

    return cmd
}

func (o *ExOpOptions) Run() error {
    return os.Setenv("THEBESTOFTIMES", "Sure it was.")
}

Example build and test script:

GO111MODULE="on" go build cmd/ex-com.go && \
   echo "First Print:" && \
   echo "$THEBESTOFTIMES" && \
   ./ex-com && \
   echo "Second Print:" && \
   echo "$THEBESTOFTIMES"

I would expect the output to look like this (since the environment variable is being set on line 48 of the go code):

go: finding k8s.io/cli-runtime/pkg/genericclioptions latest
go: finding k8s.io/cli-runtime/pkg latest
go: finding k8s.io/cli-runtime latest
First Print:

Second Print:
Sure it was.

But currently it looks like this:

go: finding k8s.io/cli-runtime/pkg/genericclioptions latest
go: finding k8s.io/cli-runtime/pkg latest
go: finding k8s.io/cli-runtime latest
First Print:

Second Print:

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *