From 8e742d655d0970844ba8c2d9563d2351aebbf179 Mon Sep 17 00:00:00 2001 From: lauralani Date: Sun, 11 Jun 2023 20:03:41 +0200 Subject: [PATCH] add delete and change error handling --- .gitignore | 3 ++- README.md | 33 ++++++++++++++++++++++++++++++++- cmd/delete.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ cmd/list.go | 7 +++++-- 4 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 cmd/delete.go diff --git a/.gitignore b/.gitignore index 650fbae..8f73b92 100644 --- a/.gitignore +++ b/.gitignore @@ -101,4 +101,5 @@ fabric.properties .idea/ .env -!examples/** \ No newline at end of file +!examples/** +/ovh-apikey-manager diff --git a/README.md b/README.md index e5aadba..843a23d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,35 @@ # ovh-apikey-manager [![status-badge](https://ci.codeberg.org/api/badges/lauralani/ovh-apikey-manager/status.svg)](https://ci.codeberg.org/lauralani/ovh-apikey-manager) -A tiny tool to manage your OVH api keys \ No newline at end of file +A tiny tool to manage your OVH api keys + +## Installation +### Pre-Built Binary +To get the binary just download the latest release for your OS/Arch from the +[release page](https://codeberg.org/lauralani/ovh-apikey-manager/releases) +and put the binary somewhere convenient. The application assumes its config +file to be in the same directory as the binary. + +### Compile from Source +Requirements: +- go1.20+ + +First, clone the repository to your local machine +```shell +git clone https://codeberg.org/lauralani/ovh-apikey-manager + +# or if you want a specific branch: +git clone https://codeberg.org/lauralani/ovh-apikey-manager -b x.x.x +``` + +Then compile the project with +```shell +go build +``` + +## Configuration +The application assumes the config file `.env` + +```shell + +``` \ No newline at end of file diff --git a/cmd/delete.go b/cmd/delete.go new file mode 100644 index 0000000..c182f6c --- /dev/null +++ b/cmd/delete.go @@ -0,0 +1,44 @@ +/* +Copyright © 2023 Laura Kalb +*/ +package cmd + +import ( + "codeberg.org/lauralani/ovh-apikey-manager/app" + "log" + "os" + "strconv" + + "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), + + Run: func(cmd *cobra.Command, args []string) { + client := app.GetOVHClient() + id := args[0] + + _, err := strconv.Atoi(id) + if err != nil { + log.Printf("Argument must be a valid number\n") + os.Exit(1) + } + + err = client.Delete("/me/api/application/"+id, nil) + if err != nil { + log.Printf("Error deleting application: %q\n", err) + os.Exit(1) + } + + log.Printf("Application with id %v was successfully deleted.\n", id) + }, +} + +func init() { + rootCmd.AddCommand(deleteCmd) +} diff --git a/cmd/list.go b/cmd/list.go index b669df5..0a2e177 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -9,6 +9,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" "log" + "os" "strconv" ) @@ -24,7 +25,8 @@ var listCmd = &cobra.Command{ var appresponselist []int err := client.Get("/me/api/application", &appresponselist) if err != nil { - log.Fatalf("Error getting application list: %v", err.Error()) + log.Printf("Error getting application list: %q\n", err) + os.Exit(1) } for _, appid := range appresponselist { @@ -32,7 +34,8 @@ var listCmd = &cobra.Command{ err := client.Get("/me/api/application/"+strconv.Itoa(appid), &application) if err != nil { - log.Fatalf("Error getting application %v: %v", appid, err.Error()) + log.Printf("Error getting application %v: %v", appid, err.Error()) + os.Exit(1) } applist = append(applist, application)