make it possible for multiple apps to be deleted
This commit is contained in:
parent
de5dad2d56
commit
923c718e28
1 changed files with 34 additions and 16 deletions
|
@ -4,39 +4,57 @@ Copyright © 2023 Laura Kalb <dev@lauka.net>
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"codeberg.org/lauralani/ovh-apikey-manager/app"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"codeberg.org/lauralani/ovh-apikey-manager/app"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// deleteCmd represents the delete command
|
||||
var deleteCmd = &cobra.Command{
|
||||
Use: "delete",
|
||||
Short: "Delete API keys",
|
||||
Long: `Delete a single API key by ID`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
Use: "delete",
|
||||
Short: "Delete API keys",
|
||||
Long: `Delete one or multiple api keys by ID, separated by spaces`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Example: "ovh-apikey-manager delete 111111 22222 33333",
|
||||
Run: runDelete,
|
||||
}
|
||||
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
client := app.GetOVHClient()
|
||||
id := args[0]
|
||||
func runDelete(cmd *cobra.Command, args []string) {
|
||||
ids, err := getIntsfromDeleteArgs(args)
|
||||
if err != nil {
|
||||
log.Printf("Arguments must be valid numbers!\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
_, err := strconv.Atoi(id)
|
||||
if err != nil {
|
||||
log.Printf("Argument must be a valid number\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
client := app.GetOVHClient()
|
||||
|
||||
err = client.Delete("/me/api/application/"+id, nil)
|
||||
for _, id := range ids {
|
||||
str := strconv.Itoa(id)
|
||||
err = client.Delete("/me/api/application/"+str, nil)
|
||||
if err != nil {
|
||||
log.Printf("Error deleting application: %q\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Printf("Deleted Application %v\n", id)
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("Application with id %v was successfully deleted.\n", id)
|
||||
},
|
||||
func getIntsfromDeleteArgs(args []string) ([]int, error) {
|
||||
var ints []int
|
||||
for _, arg := range args {
|
||||
num, err := strconv.Atoi(arg)
|
||||
if err == nil {
|
||||
ints = append(ints, num)
|
||||
} else {
|
||||
var empty []int
|
||||
return empty, err
|
||||
}
|
||||
}
|
||||
return ints, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
|
Loading…
Reference in a new issue