2023-06-11 20:03:41 +02:00
|
|
|
/*
|
|
|
|
Copyright © 2023 Laura Kalb <dev@lauka.net>
|
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
|
2023-06-22 13:10:15 +02:00
|
|
|
"codeberg.org/lauralani/ovh-apikey-manager/app"
|
|
|
|
|
2023-06-11 20:03:41 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// deleteCmd represents the delete command
|
|
|
|
var deleteCmd = &cobra.Command{
|
2023-06-22 13:10:15 +02:00
|
|
|
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,
|
|
|
|
}
|
2023-06-11 20:03:41 +02:00
|
|
|
|
2023-06-22 13:10:15 +02:00
|
|
|
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)
|
|
|
|
}
|
2023-06-11 20:03:41 +02:00
|
|
|
|
2023-06-22 13:10:15 +02:00
|
|
|
client := app.GetOVHClient()
|
2023-06-11 20:03:41 +02:00
|
|
|
|
2023-06-22 13:10:15 +02:00
|
|
|
for _, id := range ids {
|
|
|
|
str := strconv.Itoa(id)
|
|
|
|
err = client.Delete("/me/api/application/"+str, nil)
|
2023-06-11 20:03:41 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error deleting application: %q\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-06-22 13:10:15 +02:00
|
|
|
log.Printf("Deleted Application %v\n", id)
|
|
|
|
}
|
|
|
|
}
|
2023-06-11 20:03:41 +02:00
|
|
|
|
2023-06-22 13:10:15 +02:00
|
|
|
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
|
2023-06-11 20:03:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(deleteCmd)
|
|
|
|
}
|