move list function to separate function
This commit is contained in:
parent
923c718e28
commit
4d280fc40f
1 changed files with 38 additions and 33 deletions
71
cmd/list.go
71
cmd/list.go
|
@ -4,55 +4,60 @@ 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/viper"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"codeberg.org/lauralani/ovh-apikey-manager/app"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// listCmd represents the list command
|
// listCmd represents the list command
|
||||||
var listCmd = &cobra.Command{
|
var listCmd = &cobra.Command{
|
||||||
Use: "list",
|
Use: "list",
|
||||||
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: runList,
|
||||||
client := app.GetOVHClient()
|
Args: cobra.NoArgs,
|
||||||
var applist []app.Application
|
Example: "ovh-apikey-manager delete",
|
||||||
|
}
|
||||||
|
|
||||||
var appresponselist []int
|
func runList(cmd *cobra.Command, args []string) {
|
||||||
err := client.Get("/me/api/application", &appresponselist)
|
client := app.GetOVHClient()
|
||||||
|
var applist []app.Application
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, appid := range appresponselist {
|
||||||
|
var application app.Application
|
||||||
|
|
||||||
|
err := client.Get("/me/api/application/"+strconv.Itoa(appid), &application)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error getting application list: %q\n", err)
|
log.Printf("Error getting application %v: %v", appid, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, appid := range appresponselist {
|
applist = append(applist, application)
|
||||||
var application app.Application
|
}
|
||||||
|
|
||||||
err := client.Get("/me/api/application/"+strconv.Itoa(appid), &application)
|
fmt.Println("List of current API-Keys:")
|
||||||
if err != nil {
|
fmt.Println("Format: ID: [status] name (description)")
|
||||||
log.Printf("Error getting application %v: %v", appid, err.Error())
|
fmt.Printf("\"*\" shows API key used by this application\n\n")
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
applist = append(applist, application)
|
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)
|
||||||
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() {
|
func init() {
|
||||||
|
|
Loading…
Reference in a new issue