From 9bc47d40a6d14e35210a6286d9320cd52666ea85 Mon Sep 17 00:00:00 2001 From: lauralani Date: Sun, 11 Jun 2023 19:29:56 +0200 Subject: [PATCH] add list keys --- .gitignore | 4 +++- app/app.go | 36 ++++++++++++++++++++++++++++++++++++ cmd/list.go | 39 ++++++++++++++++++++++++++++++++++++--- cmd/root.go | 13 +++++++++++-- examples/.env | 17 +++++++++++++++++ main.go | 2 +- 6 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 app/app.go create mode 100644 examples/.env diff --git a/.gitignore b/.gitignore index 7cd0227..650fbae 100644 --- a/.gitignore +++ b/.gitignore @@ -99,4 +99,6 @@ fabric.properties # Android studio 3.1+ serialized cache file .idea/caches/build_file_checksums.ser -.idea/ \ No newline at end of file +.idea/ +.env +!examples/** \ No newline at end of file diff --git a/app/app.go b/app/app.go new file mode 100644 index 0000000..ad51bc4 --- /dev/null +++ b/app/app.go @@ -0,0 +1,36 @@ +package app + +import ( + "github.com/ovh/go-ovh/ovh" + "github.com/spf13/viper" + "log" +) + +type ApplicationAllResponse struct { + Applications []int +} + +type Application struct { + ApplicationID int `json:"applicationId"` + ApplicationKey string `json:"applicationKey"` + Description string `json:"description"` + Name string `json:"name"` + Status string `json:"status"` +} + +func GetOVHClient() *ovh.Client { + if viper.GetString("OVH_APPLICATIONKEY") == "" || + viper.GetString("OVH_APPLICATIONSECRET") == "" || + viper.GetString("OVH_CONSUMERKEY") == "" || + viper.GetString("OVH_REGION") == "" { + log.Fatalln("We're missing a configuration variable in .env. Please recheck the config file.") + } + client, _ := ovh.NewClient( + viper.GetString("OVH_REGION"), + viper.GetString("OVH_APPLICATIONKEY"), + viper.GetString("OVH_APPLICATIONSECRET"), + viper.GetString("OVH_CONSUMERKEY"), + ) + + return client +} diff --git a/cmd/list.go b/cmd/list.go index 7d3aaf6..b669df5 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -1,12 +1,15 @@ /* -Copyright © 2023 NAME HERE +Copyright © 2023 Laura Kalb */ package cmd import ( + "codeberg.org/lauralani/ovh-apikey-manager/app" "fmt" - "github.com/spf13/cobra" + "github.com/spf13/viper" + "log" + "strconv" ) // listCmd represents the list command @@ -15,7 +18,37 @@ var listCmd = &cobra.Command{ Short: "List all active API Keys", Long: `Lists all of your currently active API Keys`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("list called") + client := app.GetOVHClient() + var applist []app.Application + + var appresponselist []int + err := client.Get("/me/api/application", &appresponselist) + if err != nil { + log.Fatalf("Error getting application list: %v", err.Error()) + } + + for _, appid := range appresponselist { + var application app.Application + + err := client.Get("/me/api/application/"+strconv.Itoa(appid), &application) + if err != nil { + log.Fatalf("Error getting application %v: %v", appid, err.Error()) + } + + applist = append(applist, application) + } + + fmt.Println("List of current API-Keys:") + fmt.Println("Format: ID: [status] name (description)") + fmt.Printf("\"*\" shows API key used by this application\n\n") + + for _, item := range applist { + iscurrentkey := " " + if item.ApplicationKey == viper.GetString("OVH_APPLICATIONKEY") { + iscurrentkey = "* " + } + fmt.Printf("%v%v: [%v] %v (%v)\n", iscurrentkey, item.ApplicationID, item.Status, item.Name, item.Description) + } }, } diff --git a/cmd/root.go b/cmd/root.go index 8439029..1f49365 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,9 +1,11 @@ /* -Copyright © 2023 NAME HERE +Copyright © 2023 Laura Kalb */ package cmd import ( + "github.com/spf13/viper" + "log" "os" "github.com/spf13/cobra" @@ -22,7 +24,14 @@ var rootCmd = &cobra.Command{ // 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() + viper.SetConfigFile(".env") // Search for a config file named .env + viper.AddConfigPath(".") // in the working dir of the app + err := viper.ReadInConfig() // Find and read the config file + if err != nil { // Handle errors reading the config file + log.Fatalln("Couldn't find config file at ./.env") + } + + err = rootCmd.Execute() if err != nil { os.Exit(1) } diff --git a/examples/.env b/examples/.env new file mode 100644 index 0000000..47d2498 --- /dev/null +++ b/examples/.env @@ -0,0 +1,17 @@ +# OVH API Region. Possible values: +# - Europe: ovh-eu +# - USA: ovh-us +# - Canada: ovh-ca +OVH_REGION=ovh-eu + +## OVH API Key details +## Yes, i know the irony that you have to have an API key to view API keys +## You can get a minimal permission API key by visiting +## https://api.ovh.com/createToken/?GET=/me/api/application&GET=/me/api/application/*&DELETE=/me/api/application/* +## Just login with your credentials and fill out the Name and then you have your API key + +OVH_APPLICATIONKEY= + +OVH_APPLICATIONSECRET= + +OVH_CONSUMERKEY= \ No newline at end of file diff --git a/main.go b/main.go index 9849fd4..f476e75 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,5 @@ /* -Copyright © 2023 NAME HERE +Copyright © 2023 2023 Laura Kalb */ package main