From f72936df112b3e0d38e9e4176910d7f287fb91ba Mon Sep 17 00:00:00 2001 From: "Evers, Martijn" Date: Fri, 15 Mar 2024 15:09:22 +0100 Subject: [PATCH] Refactor loading current configuration --- cmd/apply-entry.go | 4 +--- cmd/apply-folder.go | 4 +--- cmd/create-entry.go | 4 +--- cmd/create-folder.go | 4 +--- cmd/delete-entry.go | 4 +--- cmd/delete-folder.go | 4 +--- cmd/get-accesslevels.go | 4 +--- cmd/get-entry.go | 4 +--- cmd/get-folder.go | 4 +--- cmd/get-folders.go | 4 +--- cmd/get-passwordstrength.go | 4 +--- cmd/get-rootfolder.go | 4 +--- cmd/get-serverinfo.go | 4 +--- cmd/patch-entry.go | 4 +--- cmd/patch-folder.go | 4 +--- cmd/search.go | 4 +--- pleasant/helpers.go | 4 ++++ 17 files changed, 20 insertions(+), 48 deletions(-) diff --git a/cmd/apply-entry.go b/cmd/apply-entry.go index e7f9846..39a7d42 100644 --- a/cmd/apply-entry.go +++ b/cmd/apply-entry.go @@ -20,7 +20,6 @@ import ( "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // applyEntryCmd represents the entry command @@ -55,8 +54,7 @@ pleasant-cli apply entry --path 'Root/Folder1/TestEntry' --data ' return } - baseUrl := viper.GetString("serverurl") - bearerToken := viper.GetString("bearertoken.accesstoken") + baseUrl, bearerToken := pleasant.LoadConfig() json, err := cmd.Flags().GetString("data") if err != nil { diff --git a/cmd/apply-folder.go b/cmd/apply-folder.go index 943246b..074a898 100644 --- a/cmd/apply-folder.go +++ b/cmd/apply-folder.go @@ -20,7 +20,6 @@ import ( "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // applyFolderCmd represents the folder command @@ -53,8 +52,7 @@ pleasant-cli apply folder --path 'Root/Folder1/TestFolder' --data ' return } - baseUrl := viper.GetString("serverurl") - bearerToken := viper.GetString("bearertoken.accesstoken") + baseUrl, bearerToken := pleasant.LoadConfig() json, err := cmd.Flags().GetString("data") if err != nil { diff --git a/cmd/create-entry.go b/cmd/create-entry.go index c46baf3..3e6e2c3 100644 --- a/cmd/create-entry.go +++ b/cmd/create-entry.go @@ -20,7 +20,6 @@ import ( "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // createEntryCmd represents the entry command @@ -63,8 +62,7 @@ pleasant-cli create entry --path 'Root/Folder1/TestEntry' --data ' return } - baseUrl := viper.GetString("serverurl") - bearerToken := viper.GetString("bearertoken.accesstoken") + baseUrl, bearerToken := pleasant.LoadConfig() json, err := cmd.Flags().GetString("data") if err != nil { diff --git a/cmd/create-folder.go b/cmd/create-folder.go index 943cd85..cb1dc78 100644 --- a/cmd/create-folder.go +++ b/cmd/create-folder.go @@ -20,7 +20,6 @@ import ( "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // createFolderCmd represents the folder command @@ -61,8 +60,7 @@ pleasant-cli create folder --path 'Root/Folder1/TestFolder' --data ' return } - baseUrl := viper.GetString("serverurl") - bearerToken := viper.GetString("bearertoken.accesstoken") + baseUrl, bearerToken := pleasant.LoadConfig() json, err := cmd.Flags().GetString("data") if err != nil { diff --git a/cmd/delete-entry.go b/cmd/delete-entry.go index 8ede221..ebf1b04 100644 --- a/cmd/delete-entry.go +++ b/cmd/delete-entry.go @@ -20,7 +20,6 @@ import ( "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // deleteEntryCmd represents the entry command @@ -44,8 +43,7 @@ pleasant-cli delete entry --id --delete --useraccess `, return } - baseUrl := viper.GetString("serverurl") - bearerToken := viper.GetString("bearertoken.accesstoken") + baseUrl, bearerToken := pleasant.LoadConfig() var identifier string diff --git a/cmd/delete-folder.go b/cmd/delete-folder.go index a720468..06d7695 100644 --- a/cmd/delete-folder.go +++ b/cmd/delete-folder.go @@ -20,7 +20,6 @@ import ( "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // deleteFolderCmd represents the entry command @@ -45,8 +44,7 @@ pleasant-cli delete folder --id --delete --useraccess `, return } - baseUrl := viper.GetString("serverurl") - bearerToken := viper.GetString("bearertoken.accesstoken") + baseUrl, bearerToken := pleasant.LoadConfig() var identifier string diff --git a/cmd/get-accesslevels.go b/cmd/get-accesslevels.go index fb0166f..b1273ec 100644 --- a/cmd/get-accesslevels.go +++ b/cmd/get-accesslevels.go @@ -20,7 +20,6 @@ import ( "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // getAccesslevelsCmd represents the accesslevels command @@ -36,8 +35,7 @@ pleasant-cli get accesslevels`, return } - baseUrl := viper.GetString("serverurl") - bearerToken := viper.GetString("bearertoken.accesstoken") + baseUrl, bearerToken := pleasant.LoadConfig() accesslevels, err := pleasant.GetJsonBody(baseUrl, pleasant.PathAccessLevels, bearerToken) if err != nil { diff --git a/cmd/get-entry.go b/cmd/get-entry.go index d81c5e6..4676653 100644 --- a/cmd/get-entry.go +++ b/cmd/get-entry.go @@ -20,7 +20,6 @@ import ( "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // getEntryCmd represents the entry command @@ -43,8 +42,7 @@ pleasant-cli get entry --path --attachments`, return } - baseUrl := viper.GetString("serverurl") - bearerToken := viper.GetString("bearertoken.accesstoken") + baseUrl, bearerToken := pleasant.LoadConfig() var identifier string diff --git a/cmd/get-folder.go b/cmd/get-folder.go index f39f023..ae0fd72 100644 --- a/cmd/get-folder.go +++ b/cmd/get-folder.go @@ -20,7 +20,6 @@ import ( "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // getFolderCmd represents the folder command @@ -38,8 +37,7 @@ pleasant-cli get folder --path `, return } - baseUrl := viper.GetString("serverurl") - bearerToken := viper.GetString("bearertoken.accesstoken") + baseUrl, bearerToken := pleasant.LoadConfig() var identifier string diff --git a/cmd/get-folders.go b/cmd/get-folders.go index 526de5e..61d01d9 100644 --- a/cmd/get-folders.go +++ b/cmd/get-folders.go @@ -20,7 +20,6 @@ import ( "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // getFoldersCmd represents the folders command @@ -37,8 +36,7 @@ pleasant-cli get folders`, return } - baseUrl := viper.GetString("serverurl") - bearerToken := viper.GetString("bearertoken.accesstoken") + baseUrl, bearerToken := pleasant.LoadConfig() folder, err := pleasant.GetJsonBody(baseUrl, pleasant.PathFolders, bearerToken) if err != nil { diff --git a/cmd/get-passwordstrength.go b/cmd/get-passwordstrength.go index 0d86c0e..330ba16 100644 --- a/cmd/get-passwordstrength.go +++ b/cmd/get-passwordstrength.go @@ -20,7 +20,6 @@ import ( "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // getPasswordStrengthCmd represents the passwordstrength command @@ -37,8 +36,7 @@ pleasant-cli get passwordstrength -p `, return } - baseUrl := viper.GetString("serverurl") - bearerToken := viper.GetString("bearertoken.accesstoken") + baseUrl, bearerToken := pleasant.LoadConfig() pw, err := cmd.Flags().GetString("password") if err != nil { diff --git a/cmd/get-rootfolder.go b/cmd/get-rootfolder.go index 1105b1a..ac2f37a 100644 --- a/cmd/get-rootfolder.go +++ b/cmd/get-rootfolder.go @@ -20,7 +20,6 @@ import ( "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // getRootfolderCmd represents the rootfolder command @@ -36,8 +35,7 @@ pleasant-cli get rootfolder`, return } - baseUrl := viper.GetString("serverurl") - bearerToken := viper.GetString("bearertoken.accesstoken") + baseUrl, bearerToken := pleasant.LoadConfig() rootFolderId, err := pleasant.GetJsonBody(baseUrl, pleasant.PathRootFolder, bearerToken) if err != nil { diff --git a/cmd/get-serverinfo.go b/cmd/get-serverinfo.go index 053575a..c6db5f9 100644 --- a/cmd/get-serverinfo.go +++ b/cmd/get-serverinfo.go @@ -20,7 +20,6 @@ import ( "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // getServerInfoCmd represents the serverinfo command @@ -37,8 +36,7 @@ pleasant-cli get serverinfo`, return } - baseUrl := viper.GetString("serverurl") - bearerToken := viper.GetString("bearertoken.accesstoken") + baseUrl, bearerToken := pleasant.LoadConfig() serverInfo, err := pleasant.GetJsonBody(baseUrl, pleasant.PathServerInfo, bearerToken) if err != nil { diff --git a/cmd/patch-entry.go b/cmd/patch-entry.go index 8069402..9da6fff 100644 --- a/cmd/patch-entry.go +++ b/cmd/patch-entry.go @@ -20,7 +20,6 @@ import ( "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // patchEntryCmd represents the entry command @@ -44,8 +43,7 @@ pleasant-cli patch entry --path 'Root/Folder1/TestEntry' --data ' return } - baseUrl := viper.GetString("serverurl") - bearerToken := viper.GetString("bearertoken.accesstoken") + baseUrl, bearerToken := pleasant.LoadConfig() json, err := cmd.Flags().GetString("data") if err != nil { diff --git a/cmd/patch-folder.go b/cmd/patch-folder.go index 3f2b86f..23f0473 100644 --- a/cmd/patch-folder.go +++ b/cmd/patch-folder.go @@ -20,7 +20,6 @@ import ( "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // patchFolderCmd represents the folder command @@ -44,8 +43,7 @@ pleasant-cli patch folder --path 'Root/Folder1/TestFolder' --data ' return } - baseUrl := viper.GetString("serverurl") - bearerToken := viper.GetString("bearertoken.accesstoken") + baseUrl, bearerToken := pleasant.LoadConfig() json, err := cmd.Flags().GetString("data") if err != nil { diff --git a/cmd/search.go b/cmd/search.go index ffe438c..4a751c9 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -20,7 +20,6 @@ import ( "github.com/marevers/pleasant-cli/pleasant" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // searchCmd represents the search command @@ -36,8 +35,7 @@ pleasant-cli search --query 'MyTestEntry'`, return } - baseUrl := viper.GetString("serverurl") - bearerToken := viper.GetString("bearertoken.accesstoken") + baseUrl, bearerToken := pleasant.LoadConfig() query, err := cmd.Flags().GetString("query") if err != nil { diff --git a/pleasant/helpers.go b/pleasant/helpers.go index 4554f76..dc707b9 100644 --- a/pleasant/helpers.go +++ b/pleasant/helpers.go @@ -146,6 +146,10 @@ func WriteTokenFile(file, accessToken string, expiresAt int64) error { return nil } +func LoadConfig() (string, string) { + return viper.GetString("serverurl"), viper.GetString("bearertoken.accesstoken") +} + func newHttpClient() *http.Client { return &http.Client{ Timeout: 20 * time.Second,