57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
/*
|
|
Copyright © 2023 Laura Kalb <dev@lauka.net>
|
|
*/
|
|
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
|
|
var listCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: "List all active API Keys",
|
|
Long: `Lists all of your currently active API Keys`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
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)
|
|
}
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(listCmd)
|
|
}
|