ovh-apikey-manager/cmd/list.go

66 lines
1.5 KiB
Go
Raw Normal View History

2023-06-11 17:01:59 +02:00
/*
2023-06-11 19:29:56 +02:00
Copyright © 2023 Laura Kalb <dev@lauka.net>
2023-06-11 17:01:59 +02:00
*/
package cmd
import (
"fmt"
2023-06-11 19:29:56 +02:00
"log"
2023-06-11 20:03:41 +02:00
"os"
2023-06-11 19:29:56 +02:00
"strconv"
"codeberg.org/lauralani/ovh-apikey-manager/app"
"github.com/spf13/cobra"
"github.com/spf13/viper"
2023-06-11 17:01:59 +02:00
)
// 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: runList,
Args: cobra.NoArgs,
Example: "ovh-apikey-manager delete",
}
func runList(cmd *cobra.Command, args []string) {
client := app.GetOVHClient()
var applist []app.Application
2023-06-11 19:29:56 +02:00
var appresponselist []int
err := client.Get("/me/api/application", &appresponselist)
if err != nil {
log.Printf("Error getting application list: %q\n", err)
os.Exit(1)
}
2023-06-11 19:29:56 +02:00
for _, appid := range appresponselist {
var application app.Application
2023-06-11 19:29:56 +02:00
err := client.Get("/me/api/application/"+strconv.Itoa(appid), &application)
if err != nil {
log.Printf("Error getting application %v: %v", appid, err.Error())
os.Exit(1)
2023-06-11 19:29:56 +02:00
}
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")
2023-06-11 19:29:56 +02:00
for _, item := range applist {
iscurrentkey := " "
if item.ApplicationKey == viper.GetString("OVH_APPLICATIONKEY") {
iscurrentkey = "* "
2023-06-11 19:29:56 +02:00
}
fmt.Printf("%v%v: [%v] %v (%v)\n", iscurrentkey, item.ApplicationID, item.Status, item.Name, item.Description)
}
2023-06-11 17:01:59 +02:00
}
func init() {
rootCmd.AddCommand(listCmd)
}