add list keys
This commit is contained in:
parent
f6d8fd4da9
commit
9bc47d40a6
6 changed files with 104 additions and 7 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -100,3 +100,5 @@ fabric.properties
|
||||||
.idea/caches/build_file_checksums.ser
|
.idea/caches/build_file_checksums.ser
|
||||||
|
|
||||||
.idea/
|
.idea/
|
||||||
|
.env
|
||||||
|
!examples/**
|
36
app/app.go
Normal file
36
app/app.go
Normal file
|
@ -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
|
||||||
|
}
|
39
cmd/list.go
39
cmd/list.go
|
@ -1,12 +1,15 @@
|
||||||
/*
|
/*
|
||||||
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
|
Copyright © 2023 Laura Kalb <dev@lauka.net>
|
||||||
*/
|
*/
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"codeberg.org/lauralani/ovh-apikey-manager/app"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// listCmd represents the list command
|
// listCmd represents the list command
|
||||||
|
@ -15,7 +18,37 @@ var listCmd = &cobra.Command{
|
||||||
Short: "List all active API Keys",
|
Short: "List all active API Keys",
|
||||||
Long: `Lists all of your currently active API Keys`,
|
Long: `Lists all of your currently active API Keys`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
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)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
13
cmd/root.go
13
cmd/root.go
|
@ -1,9 +1,11 @@
|
||||||
/*
|
/*
|
||||||
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
|
Copyright © 2023 Laura Kalb <dev@lauka.net>
|
||||||
*/
|
*/
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"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.
|
// 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.
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||||
func Execute() {
|
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 {
|
if err != nil {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
17
examples/.env
Normal file
17
examples/.env
Normal file
|
@ -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=
|
2
main.go
2
main.go
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
|
Copyright © 2023 2023 Laura Kalb <dev@lauka.net>
|
||||||
*/
|
*/
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue