Skip to content

Instantly share code, notes, and snippets.

@dhensen
Last active September 16, 2017 09:08
Show Gist options
  • Save dhensen/ab04e0804df655974064e461b6dfa3c0 to your computer and use it in GitHub Desktop.
Save dhensen/ab04e0804df655974064e461b6dfa3c0 to your computer and use it in GitHub Desktop.
Default ingress-class can not be overridden
package main
import (
"flag"
"fmt"
"os"
"github.com/spf13/pflag"
)
func main() {
flags := pflag.NewFlagSet("", pflag.ExitOnError)
ingressClass := flags.String("ingress-class", "",
`Name of the ingress class to route through this controller.`)
flags.AddGoFlagSet(flag.CommandLine)
ingressClassFoo := flags.Lookup("ingress-class")
// Set default value before Parse
if ingressClassFoo != nil {
ingressClassFoo.Value.Set("haproxy")
ingressClassFoo.DefValue = "haproxy"
}
// Perform Parse
flags.Parse(os.Args)
// change flag value after Parse
ic, _ := flags.GetString("ingress-class")
if ic == "" {
flags.Set("ingress-class", "haproxy")
}
fmt.Printf("Ingress class: %s\n", *ingressClass)
}
//$ go run main.go
//Ingress class: haproxy
//$ go run main.go --ingress-class=Foo
//Ingress class: Foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment