diff --git a/README.md b/README.md index 4eed2cda..34e4ee65 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,19 @@ 1. Setup your OpenID Connect provider, e.g. Google Identity Platform or Keycloak. 1. Setup your Kubernetes cluster. -1. Setup `kubectl` and `kubelogin`. +1. Setup your `kubectl`. + +``` +% kubelogin --help +2018/08/15 19:08:58 Usage: + kubelogin [OPTIONS] + +Application Options: + --kubeconfig= Path to the kubeconfig file. (default: ~/.kube/config) [$KUBECONFIG] + +Help Options: + -h, --help Show this help message +``` ## Getting Started with Google Account diff --git a/kubeconfig/kubeconfig.go b/kubeconfig/kubeconfig.go index c9afdf4d..b8d6d8ad 100644 --- a/kubeconfig/kubeconfig.go +++ b/kubeconfig/kubeconfig.go @@ -2,37 +2,11 @@ package kubeconfig import ( "fmt" - "os" - homedir "github.com/mitchellh/go-homedir" "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd/api" ) -const userKubeConfig = "~/.kube/config" - -// Find returns path to the kubeconfig file, -// that is given by env:KUBECONFIG or ~/.kube/config. -// This returns an error if it is not found or I/O error occurred. -func Find() (string, error) { - path := os.Getenv("KUBECONFIG") - if path == "" { - var err error - path, err = homedir.Expand(userKubeConfig) - if err != nil { - return "", fmt.Errorf("Could not expand %s: %s", userKubeConfig, err) - } - } - info, err := os.Stat(path) - if err != nil { - return "", fmt.Errorf("Could not stat %s: %s", userKubeConfig, err) - } - if info.IsDir() { - return "", fmt.Errorf("%s should be a file", userKubeConfig) - } - return path, nil -} - // Load loads the file and returns the Config. func Load(path string) (*api.Config, error) { config, err := clientcmd.LoadFromFile(path) diff --git a/main.go b/main.go index 419b3cd0..0db08747 100644 --- a/main.go +++ b/main.go @@ -2,16 +2,48 @@ package main import ( "context" + "fmt" "log" "github.com/int128/kubelogin/authn" "github.com/int128/kubelogin/kubeconfig" + flags "github.com/jessevdk/go-flags" + homedir "github.com/mitchellh/go-homedir" ) +type options struct { + KubeConfig string `long:"kubeconfig" default:"~/.kube/config" env:"KUBECONFIG" description:"Path to the kubeconfig file."` +} + +func (o *options) ExpandKubeConfig() (string, error) { + d, err := homedir.Expand(o.KubeConfig) + if err != nil { + return "", fmt.Errorf("Could not expand %s", o.KubeConfig) + } + return d, nil +} + +func parseOptions() (*options, error) { + var o options + parser := flags.NewParser(&o, flags.HelpFlag) + args, err := parser.Parse() + if err != nil { + return nil, err + } + if len(args) > 0 { + return nil, fmt.Errorf("Too many argument") + } + return &o, nil +} + func main() { - path, err := kubeconfig.Find() + opts, err := parseOptions() + if err != nil { + log.Fatal(err) + } + path, err := opts.ExpandKubeConfig() if err != nil { - log.Fatalf("Could not find kubeconfig: %s", err) + log.Fatal(err) } log.Printf("Reading %s", path) cfg, err := kubeconfig.Load(path)