-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fetch current email and project/context
- Loading branch information
1 parent
0843c9d
commit 547e775
Showing
6 changed files
with
773 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/coreos/go-oidc" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/oauth2/google" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
func getProjectFromKubernetesConfig() string { | ||
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( | ||
clientcmd.NewDefaultClientConfigLoadingRules(), | ||
&clientcmd.ConfigOverrides{}, | ||
).RawConfig() | ||
|
||
if err != nil { | ||
fmt.Println("Something went wrong") | ||
fmt.Println(err.Error()) | ||
} | ||
|
||
return config.Contexts[config.CurrentContext].Cluster | ||
} | ||
|
||
// authCmd represents the auth command | ||
var authCmd = &cobra.Command{ | ||
Use: "auth", | ||
Short: "Shows the current project and email", | ||
Long: `Fetches the current project and email using default credentials from Google. | ||
See https://cloud.google.com/docs/authentication/application-default-credentials`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ctx := context.Background() | ||
|
||
creds, err := google.FindDefaultCredentials(ctx) | ||
if err != nil { | ||
fmt.Println("Default credentials were not found. Try logging in to google cloud using the following command:") | ||
fmt.Println("gcloud auth application-default login") | ||
} | ||
|
||
provider, err := oidc.NewProvider(ctx, "https://accounts.google.com") | ||
if err != nil { | ||
fmt.Println("Something went wrong") | ||
fmt.Println(err.Error()) | ||
} | ||
|
||
user, err := provider.UserInfo(ctx, creds.TokenSource) | ||
if err != nil { | ||
fmt.Println("Could not find information regarding the user. Are you sure your application defaults are correct?") | ||
} | ||
|
||
fmt.Println("Current email: " + user.Email) | ||
fmt.Println("Current project/context: " + getProjectFromKubernetesConfig()) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(authCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var cfgFile string | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "skipper", | ||
Short: "A brief description of your application", | ||
Long: `A longer description that spans multiple lines and likely contains | ||
examples and usage of using your application. For example: | ||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
// Uncomment the following line if your bare application | ||
// has an action associated with it: | ||
// Run: func(cmd *cobra.Command, args []string) { }, | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
|
||
// Here you will define your flags and configuration settings. | ||
// Cobra supports persistent flags, which, if defined here, | ||
// will be global for your application. | ||
|
||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.skipper.yaml)") | ||
|
||
// Cobra also supports local flags, which will only run | ||
// when this action is called directly. | ||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} | ||
|
||
// initConfig reads in config file and ENV variables if set. | ||
func initConfig() { | ||
if cfgFile != "" { | ||
// Use config file from the flag. | ||
viper.SetConfigFile(cfgFile) | ||
} else { | ||
// Find home directory. | ||
home, err := os.UserHomeDir() | ||
cobra.CheckErr(err) | ||
|
||
// Search config in home directory with name ".skipper" (without extension). | ||
viper.AddConfigPath(home) | ||
viper.SetConfigType("yaml") | ||
viper.SetConfigName(".skipper") | ||
} | ||
|
||
viper.AutomaticEnv() // read in environment variables that match | ||
|
||
// If a config file is found, read it in. | ||
if err := viper.ReadInConfig(); err == nil { | ||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
module github.com/kartverket/skipper | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/coreos/go-oidc v2.2.1+incompatible | ||
github.com/spf13/cobra v1.6.1 | ||
github.com/spf13/viper v1.14.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/go-logr/logr v1.2.3 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/google/gofuzz v1.1.0 // indirect | ||
github.com/imdario/mergo v0.3.6 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/pquerna/cachecontrol v0.1.0 // indirect | ||
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect | ||
golang.org/x/term v0.3.0 // indirect | ||
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect | ||
gopkg.in/inf.v0 v0.9.1 // indirect | ||
gopkg.in/square/go-jose.v2 v2.6.0 // indirect | ||
k8s.io/apimachinery v0.26.0 // indirect | ||
k8s.io/klog/v2 v2.80.1 // indirect | ||
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d // indirect | ||
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect | ||
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect | ||
sigs.k8s.io/yaml v1.3.0 // indirect | ||
) | ||
|
||
require ( | ||
cloud.google.com/go/compute v1.13.0 // indirect | ||
cloud.google.com/go/compute/metadata v0.2.2 // indirect | ||
github.com/fsnotify/fsnotify v1.6.0 // indirect | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/hashicorp/hcl v1.0.0 // indirect | ||
github.com/inconshreveable/mousetrap v1.1.0 // indirect | ||
github.com/magiconair/properties v1.8.7 // indirect | ||
github.com/mitchellh/mapstructure v1.5.0 // indirect | ||
github.com/pelletier/go-toml v1.9.5 // indirect | ||
github.com/pelletier/go-toml/v2 v2.0.6 // indirect | ||
github.com/spf13/afero v1.9.3 // indirect | ||
github.com/spf13/cast v1.5.0 // indirect | ||
github.com/spf13/jwalterweatherman v1.1.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
github.com/subosito/gotenv v1.4.1 // indirect | ||
golang.org/x/net v0.3.1-0.20221206200815-1e63c2f08a10 // indirect | ||
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 | ||
golang.org/x/sys v0.3.0 // indirect | ||
golang.org/x/text v0.5.0 // indirect | ||
google.golang.org/appengine v1.6.7 // indirect | ||
google.golang.org/protobuf v1.28.1 // indirect | ||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect | ||
gopkg.in/ini.v1 v1.67.0 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
k8s.io/client-go v0.26.0 | ||
) |
Oops, something went wrong.