From e08840d6f5d769c78027552c715ca4b84d90b769 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Tue, 9 Jul 2024 09:50:34 +0200 Subject: [PATCH] add delete functionality (#425) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is slightly hacky. See https://stackoverflow.com/questions/52339336/removal-of-key-value-pair-from-viper-config-file > TLDR; This is dangerous and doesn't work. This method doesn't work in tricky ways. If you viper.Set a key at all for the path then the viper.Get will return the map from the override list, which then won't let you delete any keys in the config list. So if your app adds and removes things, you may find it fails to delete with this method. – [Will Brode](https://stackoverflow.com/users/1139197/will-brode) Commented May 11, 2023 at 7:29 In our specific use case, it seems to work just fine. --- README.md | 8 ++++++-- cmd/apps.go | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 95db1e1..aff1ee5 100644 --- a/README.md +++ b/README.md @@ -265,8 +265,12 @@ configured maximum Clisso will fallback to 3600 seconds. ### Deleting Apps -Deleting apps using the `clisso` command isn't currently supported. To delete an app, remove its -configuration from the config file. +For deleteing apps, use the following command: + + clisso apps delete my-app + +Deletion of an app will remove its configuration from the config file. You can also do it manually +by editing the config file. ### Obtaining Credentials diff --git a/cmd/apps.go b/cmd/apps.go index aa70dff..fb398b7 100644 --- a/cmd/apps.go +++ b/cmd/apps.go @@ -49,6 +49,7 @@ func init() { cmdAppsCreate.AddCommand(cmdAppsCreateOneLogin) cmdAppsCreate.AddCommand(cmdAppsCreateOkta) cmdApps.AddCommand(cmdAppsSelect) + cmdApps.AddCommand(cmdAppsDelete) } var cmdApps = &cobra.Command{ @@ -229,3 +230,27 @@ var cmdAppsSelect = &cobra.Command{ } }, } + +var cmdAppsDelete = &cobra.Command{ + Use: "delete [app name]", + Short: "Delete an app", + Long: "Delete an app from the config file.", + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + app := args[0] + + if exists := viper.Get("apps." + app); exists == nil { + log.Log.Fatalf("App '%s' doesn't exist", app) + } + + // Delete app + delete(viper.Get("apps").(map[string]interface{}), app) + + // Write config to file + err := viper.WriteConfig() + if err != nil { + log.Log.Fatalf("Error writing config: %v", err) + } + log.Log.Printf("App '%s' deleted from config file", app) + }, +}