Skip to content

Commit

Permalink
Introduce flags
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 committed Aug 15, 2018
1 parent ea711f9 commit 4bf7788
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 0 additions & 26 deletions kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 34 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4bf7788

Please sign in to comment.