From 9220ef9c36b1c5fa362b397fd3e6bdab2a72b94d Mon Sep 17 00:00:00 2001 From: "Evers, Martijn" Date: Thu, 13 Jun 2024 14:27:09 +0200 Subject: [PATCH] Add exit code 1 for unsuccesful commands --- cmd/apply-entry.go | 36 ++++++++++++------------------------ cmd/apply-folder.go | 36 ++++++++++++------------------------ cmd/config-cleartoken.go | 7 +++---- cmd/config-serverurl.go | 7 ++----- cmd/config-timeout.go | 7 ++----- cmd/create-entry.go | 33 +++++++++++---------------------- cmd/create-folder.go | 33 +++++++++++---------------------- cmd/delete-entry.go | 19 +++++++------------ cmd/delete-folder.go | 19 +++++++------------ cmd/get-accesslevels.go | 15 +++++---------- cmd/get-entry.go | 33 +++++++++++---------------------- cmd/get-folder.go | 24 ++++++++---------------- cmd/get-folders.go | 15 +++++---------- cmd/get-passwordstrength.go | 16 ++++++---------- cmd/get-rootfolder.go | 15 +++++---------- cmd/get-serverinfo.go | 15 +++++---------- cmd/login.go | 13 +++++-------- cmd/patch-entry.go | 22 ++++++++-------------- cmd/patch-folder.go | 22 ++++++++-------------- cmd/search.go | 16 ++++++---------- pleasant/errors.go | 1 + pleasant/helpers.go | 10 ++++++++++ 22 files changed, 150 insertions(+), 264 deletions(-) diff --git a/cmd/apply-entry.go b/cmd/apply-entry.go index 39a7d42..f294d9d 100644 --- a/cmd/apply-entry.go +++ b/cmd/apply-entry.go @@ -16,8 +16,6 @@ limitations under the License. package cmd import ( - "fmt" - "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" ) @@ -51,47 +49,41 @@ pleasant-cli apply entry --path 'Root/Folder1/TestEntry' --data ' }'`, Run: func(cmd *cobra.Command, args []string) { if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) { - return + pleasant.ExitFatal(pleasant.ErrPrereqNotMet) } baseUrl, bearerToken := pleasant.LoadConfig() json, err := cmd.Flags().GetString("data") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } input, err := pleasant.UnmarshalEntry(json) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if cmd.Flags().Changed("path") { resourcePath, err := cmd.Flags().GetString("path") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } pid, err := pleasant.GetParentIdByResourcePath(baseUrl, resourcePath, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if !pleasant.PathAndNameMatching(resourcePath, input.Name) { - fmt.Println("error: entry name from path and data do not match") - return + pleasant.ExitFatal("error: entry name from path and data do not match") } input.GroupId = pid j, err := pleasant.MarshalEntry(input) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } json = j @@ -99,8 +91,7 @@ pleasant-cli apply entry --path 'Root/Folder1/TestEntry' --data ' id, err := pleasant.DuplicateEntryId(baseUrl, json, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if id != "" { @@ -108,21 +99,18 @@ pleasant-cli apply entry --path 'Root/Folder1/TestEntry' --data ' _, err := pleasant.PatchJsonString(baseUrl, subPath, json, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println("Existing entry with id", id, "patched") - return + pleasant.Exit("Existing entry with id", id, "patched") } id, err = pleasant.PostJsonString(baseUrl, pleasant.PathEntry, json, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println(id) + pleasant.Exit(id) }, } diff --git a/cmd/apply-folder.go b/cmd/apply-folder.go index 074a898..6fe5219 100644 --- a/cmd/apply-folder.go +++ b/cmd/apply-folder.go @@ -16,8 +16,6 @@ limitations under the License. package cmd import ( - "fmt" - "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" ) @@ -49,47 +47,41 @@ pleasant-cli apply folder --path 'Root/Folder1/TestFolder' --data ' }'`, Run: func(cmd *cobra.Command, args []string) { if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) { - return + pleasant.ExitFatal(pleasant.ErrPrereqNotMet) } baseUrl, bearerToken := pleasant.LoadConfig() json, err := cmd.Flags().GetString("data") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } input, err := pleasant.UnmarshalFolder(json) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if cmd.Flags().Changed("path") { resourcePath, err := cmd.Flags().GetString("path") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } pid, err := pleasant.GetParentIdByResourcePath(baseUrl, resourcePath, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if !pleasant.PathAndNameMatching(resourcePath, input.Name) { - fmt.Println("error: folder name from path and data do not match") - return + pleasant.ExitFatal("error: folder name from path and data do not match") } input.ParentId = pid j, err := pleasant.MarshalFolder(input) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } json = j @@ -97,8 +89,7 @@ pleasant-cli apply folder --path 'Root/Folder1/TestFolder' --data ' id, err := pleasant.DuplicateFolderId(baseUrl, json, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if id != "" { @@ -106,21 +97,18 @@ pleasant-cli apply folder --path 'Root/Folder1/TestFolder' --data ' _, err := pleasant.PatchJsonString(baseUrl, subPath, json, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println("Existing folder with id", id, "patched") - return + pleasant.Exit("Existing folder with id", id, "patched") } id, err = pleasant.PostJsonString(baseUrl, pleasant.PathFolders, json, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println(id) + pleasant.Exit(id) }, } diff --git a/cmd/config-cleartoken.go b/cmd/config-cleartoken.go index fe6bb59..ed2318c 100644 --- a/cmd/config-cleartoken.go +++ b/cmd/config-cleartoken.go @@ -16,9 +16,9 @@ limitations under the License. package cmd import ( - "fmt" "os" + "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" ) @@ -34,11 +34,10 @@ pleasant-cli config cleartoken --token `, Run: func(cmd *cobra.Command, args []string) { err := os.Remove(tokenFile) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println("Token file deleted:", tokenFile) + pleasant.Exit("Token file deleted:", tokenFile) }, } diff --git a/cmd/config-serverurl.go b/cmd/config-serverurl.go index e48bc49..e299457 100644 --- a/cmd/config-serverurl.go +++ b/cmd/config-serverurl.go @@ -16,8 +16,6 @@ limitations under the License. package cmd import ( - "fmt" - "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" ) @@ -37,11 +35,10 @@ pleasant-cli config serverurl `, Run: func(cmd *cobra.Command, args []string) { err := pleasant.WriteConfigFile(cfgFile, "ServerUrl", args[0]) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println("Server URL saved to:", cfgFile) + pleasant.Exit("Server URL saved to:", cfgFile) }, } diff --git a/cmd/config-timeout.go b/cmd/config-timeout.go index ec62dc6..1a14337 100644 --- a/cmd/config-timeout.go +++ b/cmd/config-timeout.go @@ -16,8 +16,6 @@ limitations under the License. package cmd import ( - "fmt" - "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" ) @@ -35,11 +33,10 @@ pleasant-cli config timeout 30`, Run: func(cmd *cobra.Command, args []string) { err := pleasant.WriteConfigFile(cfgFile, "Timeout", args[0]) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println("Timeout saved to:", cfgFile) + pleasant.Exit("Timeout saved to:", cfgFile) }, } diff --git a/cmd/create-entry.go b/cmd/create-entry.go index 3e6e2c3..9004dd9 100644 --- a/cmd/create-entry.go +++ b/cmd/create-entry.go @@ -16,8 +16,6 @@ limitations under the License. package cmd import ( - "fmt" - "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" ) @@ -59,47 +57,41 @@ pleasant-cli create entry --path 'Root/Folder1/TestEntry' --data ' }'`, Run: func(cmd *cobra.Command, args []string) { if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) { - return + pleasant.ExitFatal(pleasant.ErrPrereqNotMet) } baseUrl, bearerToken := pleasant.LoadConfig() json, err := cmd.Flags().GetString("data") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if cmd.Flags().Changed("path") { resourcePath, err := cmd.Flags().GetString("path") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } input, err := pleasant.UnmarshalEntry(json) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } pid, err := pleasant.GetParentIdByResourcePath(baseUrl, resourcePath, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if !pleasant.PathAndNameMatching(resourcePath, input.Name) { - fmt.Println("error: entry name from path and data do not match") - return + pleasant.ExitFatal("error: entry name from path and data do not match") } input.GroupId = pid j, err := pleasant.MarshalEntry(input) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } json = j @@ -108,23 +100,20 @@ pleasant-cli create entry --path 'Root/Folder1/TestEntry' --data ' if cmd.Flags().Changed("no-duplicates") { exists, err := pleasant.DuplicateEntryExists(baseUrl, json, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if exists { - fmt.Println(pleasant.ErrDuplicateEntry) - return + pleasant.ExitFatal(pleasant.ErrDuplicateEntry) } } id, err := pleasant.PostJsonString(baseUrl, pleasant.PathEntry, json, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println(id) + pleasant.Exit(id) }, } diff --git a/cmd/create-folder.go b/cmd/create-folder.go index cb1dc78..aaab79d 100644 --- a/cmd/create-folder.go +++ b/cmd/create-folder.go @@ -16,8 +16,6 @@ limitations under the License. package cmd import ( - "fmt" - "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" ) @@ -57,47 +55,41 @@ pleasant-cli create folder --path 'Root/Folder1/TestFolder' --data ' }'`, Run: func(cmd *cobra.Command, args []string) { if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) { - return + pleasant.ExitFatal(pleasant.ErrPrereqNotMet) } baseUrl, bearerToken := pleasant.LoadConfig() json, err := cmd.Flags().GetString("data") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if cmd.Flags().Changed("path") { resourcePath, err := cmd.Flags().GetString("path") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } input, err := pleasant.UnmarshalFolder(json) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } pid, err := pleasant.GetParentIdByResourcePath(baseUrl, resourcePath, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if !pleasant.PathAndNameMatching(resourcePath, input.Name) { - fmt.Println("error: folder name from path and data do not match") - return + pleasant.ExitFatal("error: folder name from path and data do not match") } input.ParentId = pid j, err := pleasant.MarshalFolder(input) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } json = j @@ -106,23 +98,20 @@ pleasant-cli create folder --path 'Root/Folder1/TestFolder' --data ' if cmd.Flags().Changed("no-duplicates") { exists, err := pleasant.DuplicateFolderExists(baseUrl, json, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if exists { - fmt.Println(pleasant.ErrDuplicateFolder) - return + pleasant.ExitFatal(pleasant.ErrDuplicateFolder) } } id, err := pleasant.PostJsonString(baseUrl, pleasant.PathFolders, json, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println(id) + pleasant.Exit(id) }, } diff --git a/cmd/delete-entry.go b/cmd/delete-entry.go index ebf1b04..1d5f766 100644 --- a/cmd/delete-entry.go +++ b/cmd/delete-entry.go @@ -40,7 +40,7 @@ pleasant-cli delete entry --id --delete pleasant-cli delete entry --id --delete --useraccess `, Run: func(cmd *cobra.Command, args []string) { if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) { - return + pleasant.ExitFatal(pleasant.ErrPrereqNotMet) } baseUrl, bearerToken := pleasant.LoadConfig() @@ -50,22 +50,19 @@ pleasant-cli delete entry --id --delete --useraccess `, if cmd.Flags().Changed("path") { resourcePath, err := cmd.Flags().GetString("path") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } id, err := pleasant.GetIdByResourcePath(baseUrl, resourcePath, "entry", bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } identifier = id } else { id, err := cmd.Flags().GetString("id") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } identifier = id @@ -88,8 +85,7 @@ pleasant-cli delete entry --id --delete --useraccess `, if cmd.Flags().Changed("useraccess") { ua, err := cmd.Flags().GetString("useraccess") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } subPath = subPath + "/useraccess/" + ua @@ -101,11 +97,10 @@ pleasant-cli delete entry --id --delete --useraccess `, _, err := pleasant.DeleteJsonString(baseUrl, subPath, json, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println(msg) + pleasant.Exit(msg) }, } diff --git a/cmd/delete-folder.go b/cmd/delete-folder.go index 06d7695..ae8035a 100644 --- a/cmd/delete-folder.go +++ b/cmd/delete-folder.go @@ -41,7 +41,7 @@ pleasant-cli delete folder --id --delete pleasant-cli delete folder --id --delete --useraccess `, Run: func(cmd *cobra.Command, args []string) { if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) { - return + pleasant.ExitFatal(pleasant.ErrPrereqNotMet) } baseUrl, bearerToken := pleasant.LoadConfig() @@ -51,22 +51,19 @@ pleasant-cli delete folder --id --delete --useraccess `, if cmd.Flags().Changed("path") { resourcePath, err := cmd.Flags().GetString("path") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } id, err := pleasant.GetIdByResourcePath(baseUrl, resourcePath, "folder", bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } identifier = id } else { id, err := cmd.Flags().GetString("id") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } identifier = id @@ -89,8 +86,7 @@ pleasant-cli delete folder --id --delete --useraccess `, if cmd.Flags().Changed("useraccess") { ua, err := cmd.Flags().GetString("useraccess") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } subPath = subPath + "/useraccess/" + ua @@ -102,11 +98,10 @@ pleasant-cli delete folder --id --delete --useraccess `, _, err := pleasant.DeleteJsonString(baseUrl, subPath, json, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println(msg) + pleasant.Exit(msg) }, } diff --git a/cmd/get-accesslevels.go b/cmd/get-accesslevels.go index b1273ec..f4dfbc0 100644 --- a/cmd/get-accesslevels.go +++ b/cmd/get-accesslevels.go @@ -16,8 +16,6 @@ limitations under the License. package cmd import ( - "fmt" - "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" ) @@ -32,29 +30,26 @@ Example: pleasant-cli get accesslevels`, Run: func(cmd *cobra.Command, args []string) { if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) { - return + pleasant.ExitFatal(pleasant.ErrPrereqNotMet) } baseUrl, bearerToken := pleasant.LoadConfig() accesslevels, err := pleasant.GetJsonBody(baseUrl, pleasant.PathAccessLevels, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if cmd.Flags().Changed("pretty") { output, err := pleasant.PrettyPrintJson(accesslevels) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println(output) - return + pleasant.Exit(output) } - fmt.Println(accesslevels) + pleasant.Exit(accesslevels) }, } diff --git a/cmd/get-entry.go b/cmd/get-entry.go index 4676653..8659933 100644 --- a/cmd/get-entry.go +++ b/cmd/get-entry.go @@ -16,8 +16,6 @@ limitations under the License. package cmd import ( - "fmt" - "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" ) @@ -39,7 +37,7 @@ pleasant-cli get entry --id --password pleasant-cli get entry --path --attachments`, Run: func(cmd *cobra.Command, args []string) { if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) { - return + pleasant.ExitFatal(pleasant.ErrPrereqNotMet) } baseUrl, bearerToken := pleasant.LoadConfig() @@ -49,22 +47,19 @@ pleasant-cli get entry --path --attachments`, if cmd.Flags().Changed("path") { resourcePath, err := cmd.Flags().GetString("path") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } id, err := pleasant.GetIdByResourcePath(baseUrl, resourcePath, "entry", bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } identifier = id } else { id, err := cmd.Flags().GetString("id") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } identifier = id @@ -83,34 +78,28 @@ pleasant-cli get entry --path --attachments`, entry, err := pleasant.GetJsonBody(baseUrl, subPath, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } switch { case cmd.Flags().Changed("pretty"): output, err := pleasant.PrettyPrintJson(entry) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println(output) - return + pleasant.Exit(output) case cmd.Flags().Changed("username"): en, err := pleasant.UnmarshalEntry(entry) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println(en.Username) - return + pleasant.Exit(en.Username) case cmd.Flags().Changed("password"): - fmt.Println(pleasant.TrimDoubleQuotes(entry)) - return + pleasant.Exit(pleasant.TrimDoubleQuotes(entry)) default: - fmt.Println(entry) + pleasant.Exit(entry) } }, } diff --git a/cmd/get-folder.go b/cmd/get-folder.go index ae0fd72..15094d4 100644 --- a/cmd/get-folder.go +++ b/cmd/get-folder.go @@ -16,8 +16,6 @@ limitations under the License. package cmd import ( - "fmt" - "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" ) @@ -34,7 +32,7 @@ pleasant-cli get folder --id pleasant-cli get folder --path `, Run: func(cmd *cobra.Command, args []string) { if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) { - return + pleasant.ExitFatal(pleasant.ErrPrereqNotMet) } baseUrl, bearerToken := pleasant.LoadConfig() @@ -44,22 +42,19 @@ pleasant-cli get folder --path `, if cmd.Flags().Changed("path") { resourcePath, err := cmd.Flags().GetString("path") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } id, err := pleasant.GetIdByResourcePath(baseUrl, resourcePath, "folder", bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } identifier = id } else { id, err := cmd.Flags().GetString("id") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } identifier = id @@ -73,22 +68,19 @@ pleasant-cli get folder --path `, folder, err := pleasant.GetJsonBody(baseUrl, subPath, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if cmd.Flags().Changed("pretty") { output, err := pleasant.PrettyPrintJson(folder) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println(output) - return + pleasant.Exit(output) } - fmt.Println(folder) + pleasant.Exit(folder) }, } diff --git a/cmd/get-folders.go b/cmd/get-folders.go index 61d01d9..f00f7bf 100644 --- a/cmd/get-folders.go +++ b/cmd/get-folders.go @@ -16,8 +16,6 @@ limitations under the License. package cmd import ( - "fmt" - "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" ) @@ -33,29 +31,26 @@ Example: pleasant-cli get folders`, Run: func(cmd *cobra.Command, args []string) { if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) { - return + pleasant.ExitFatal(pleasant.ErrPrereqNotMet) } baseUrl, bearerToken := pleasant.LoadConfig() folder, err := pleasant.GetJsonBody(baseUrl, pleasant.PathFolders, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if cmd.Flags().Changed("pretty") { output, err := pleasant.PrettyPrintJson(folder) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println(output) - return + pleasant.Exit(output) } - fmt.Println(folder) + pleasant.Exit(folder) }, } diff --git a/cmd/get-passwordstrength.go b/cmd/get-passwordstrength.go index 330ba16..9ed910f 100644 --- a/cmd/get-passwordstrength.go +++ b/cmd/get-passwordstrength.go @@ -33,37 +33,33 @@ pleasant-cli get passwordstrength --password pleasant-cli get passwordstrength -p `, Run: func(cmd *cobra.Command, args []string) { if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) { - return + pleasant.ExitFatal(pleasant.ErrPrereqNotMet) } baseUrl, bearerToken := pleasant.LoadConfig() pw, err := cmd.Flags().GetString("password") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } json := fmt.Sprintf(`{"Password":"%v"}`, pw) pwStr, err := pleasant.PostJsonString(baseUrl, pleasant.PathPwStr, json, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if cmd.Flags().Changed("pretty") { output, err := pleasant.PrettyPrintJson(pwStr) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println(output) - return + pleasant.Exit(output) } - fmt.Println(pwStr) + pleasant.Exit(pwStr) }, } diff --git a/cmd/get-rootfolder.go b/cmd/get-rootfolder.go index ac2f37a..3df358b 100644 --- a/cmd/get-rootfolder.go +++ b/cmd/get-rootfolder.go @@ -16,8 +16,6 @@ limitations under the License. package cmd import ( - "fmt" - "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" ) @@ -32,29 +30,26 @@ Example: pleasant-cli get rootfolder`, Run: func(cmd *cobra.Command, args []string) { if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) { - return + pleasant.ExitFatal(pleasant.ErrPrereqNotMet) } baseUrl, bearerToken := pleasant.LoadConfig() rootFolderId, err := pleasant.GetJsonBody(baseUrl, pleasant.PathRootFolder, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if cmd.Flags().Changed("pretty") { output, err := pleasant.PrettyPrintJson(rootFolderId) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println(output) - return + pleasant.Exit(output) } - fmt.Println(rootFolderId) + pleasant.Exit(rootFolderId) }, } diff --git a/cmd/get-serverinfo.go b/cmd/get-serverinfo.go index c6db5f9..12b2784 100644 --- a/cmd/get-serverinfo.go +++ b/cmd/get-serverinfo.go @@ -16,8 +16,6 @@ limitations under the License. package cmd import ( - "fmt" - "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" ) @@ -33,29 +31,26 @@ Example: pleasant-cli get serverinfo`, Run: func(cmd *cobra.Command, args []string) { if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) { - return + pleasant.ExitFatal(pleasant.ErrPrereqNotMet) } baseUrl, bearerToken := pleasant.LoadConfig() serverInfo, err := pleasant.GetJsonBody(baseUrl, pleasant.PathServerInfo, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } if cmd.Flags().Changed("pretty") { output, err := pleasant.PrettyPrintJson(serverInfo) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println(output) - return + pleasant.Exit(output) } - fmt.Println(serverInfo) + pleasant.Exit(serverInfo) }, } diff --git a/cmd/login.go b/cmd/login.go index 93e9d05..073d3f5 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -38,7 +38,7 @@ pleasant-cli login pleasant-cli login --username --password `, Run: func(cmd *cobra.Command, args []string) { if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet()) { - return + pleasant.ExitFatal(pleasant.ErrPrereqNotMet) } var username string @@ -56,11 +56,9 @@ pleasant-cli login --username --password `, bearerToken, err := pleasant.GetBearerToken(viper.GetString("serverUrl"), username, password) if errors.Is(err, pleasant.ErrBadRequest) { - fmt.Println(pleasant.ErrInvalidCredentials) - return + pleasant.ExitFatal(pleasant.ErrInvalidCredentials) } else if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } t := time.Now() @@ -68,11 +66,10 @@ pleasant-cli login --username --password `, err = pleasant.WriteTokenFile(tokenFile, bearerToken.AccessToken, ea) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } - fmt.Println("Successfully logged in. Token saved to:", tokenFile, ", valid until", time.Unix(ea, 0)) + pleasant.Exit("Successfully logged in. Token saved to:", tokenFile, ", valid until", time.Unix(ea, 0)) }, } diff --git a/cmd/patch-entry.go b/cmd/patch-entry.go index 492a4f8..befe873 100644 --- a/cmd/patch-entry.go +++ b/cmd/patch-entry.go @@ -52,15 +52,14 @@ pleasant-cli patch entry --path 'Root/Folder1/TestEntry' --useraccess --data ' }'`, Run: func(cmd *cobra.Command, args []string) { if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) { - return + pleasant.ExitFatal(pleasant.ErrPrereqNotMet) } baseUrl, bearerToken := pleasant.LoadConfig() json, err := cmd.Flags().GetString("data") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } var identifier string @@ -68,22 +67,19 @@ pleasant-cli patch entry --path 'Root/Folder1/TestEntry' --useraccess --data ' if cmd.Flags().Changed("path") { resourcePath, err := cmd.Flags().GetString("path") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } id, err := pleasant.GetIdByResourcePath(baseUrl, resourcePath, "entry", bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } identifier = id } else { id, err := cmd.Flags().GetString("id") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } identifier = id @@ -100,20 +96,18 @@ pleasant-cli patch entry --path 'Root/Folder1/TestEntry' --useraccess --data ' _, err := pleasant.PostJsonString(baseUrl, subPath, json, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } } else { msg = fmt.Sprintf("Existing entry with id %v patched", identifier) _, err = pleasant.PatchJsonString(baseUrl, subPath, json, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } } - fmt.Println(msg) + pleasant.Exit(msg) }, } diff --git a/cmd/patch-folder.go b/cmd/patch-folder.go index 7c8663d..59c554e 100644 --- a/cmd/patch-folder.go +++ b/cmd/patch-folder.go @@ -52,15 +52,14 @@ pleasant-cli patch folder --path 'Root/Folder1/TestFolder' --useraccess --data ' }'`, Run: func(cmd *cobra.Command, args []string) { if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) { - return + pleasant.ExitFatal(pleasant.ErrPrereqNotMet) } baseUrl, bearerToken := pleasant.LoadConfig() json, err := cmd.Flags().GetString("data") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } var identifier string @@ -68,22 +67,19 @@ pleasant-cli patch folder --path 'Root/Folder1/TestFolder' --useraccess --data ' if cmd.Flags().Changed("path") { resourcePath, err := cmd.Flags().GetString("path") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } id, err := pleasant.GetIdByResourcePath(baseUrl, resourcePath, "folder", bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } identifier = id } else { id, err := cmd.Flags().GetString("id") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } identifier = id @@ -100,20 +96,18 @@ pleasant-cli patch folder --path 'Root/Folder1/TestFolder' --useraccess --data ' _, err := pleasant.PostJsonString(baseUrl, subPath, json, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } } else { msg = fmt.Sprintf("Existing folder with id %v patched", identifier) _, err = pleasant.PatchJsonString(baseUrl, subPath, json, bearerToken) if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } } - fmt.Println(msg) + pleasant.Exit(msg) }, } diff --git a/cmd/search.go b/cmd/search.go index 4a751c9..86afddc 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -16,8 +16,6 @@ limitations under the License. package cmd import ( - "fmt" - "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" ) @@ -32,33 +30,31 @@ Example: pleasant-cli search --query 'MyTestEntry'`, Run: func(cmd *cobra.Command, args []string) { if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) { - return + pleasant.ExitFatal(pleasant.ErrPrereqNotMet) } baseUrl, bearerToken := pleasant.LoadConfig() query, err := cmd.Flags().GetString("query") if err != nil { - fmt.Println(err) - return + pleasant.ExitFatal(err) } result, err := pleasant.PostSearch(baseUrl, query, bearerToken) if err != nil { - fmt.Println(err) + pleasant.ExitFatal(err) } if cmd.Flags().Changed("pretty") { output, err := pleasant.PrettyPrintJson(result) if err != nil { - fmt.Println(err) + pleasant.ExitFatal(err) } - fmt.Println(output) - return + pleasant.Exit(output) } - fmt.Println(result) + pleasant.Exit(result) }, } diff --git a/pleasant/errors.go b/pleasant/errors.go index 6355d16..c3a1c90 100644 --- a/pleasant/errors.go +++ b/pleasant/errors.go @@ -22,6 +22,7 @@ import ( ) var ( + ErrPrereqNotMet = errors.New("error: not all prerequisites met") ErrNotFound = errors.New("error: the requested resource was not found") ErrBadRequest = errors.New("error: bad request") ErrUnauthorized = errors.New("error: unauthorized, please log in again") diff --git a/pleasant/helpers.go b/pleasant/helpers.go index 13b8017..3029f48 100644 --- a/pleasant/helpers.go +++ b/pleasant/helpers.go @@ -431,3 +431,13 @@ func TrimDoubleQuotes(str string) string { return str } + +func Exit(msg ...any) { + fmt.Println(msg...) + os.Exit(0) +} + +func ExitFatal(msg ...any) { + fmt.Println(msg...) + os.Exit(1) +}