Skip to content

Commit

Permalink
Refactor loading current configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
marevers committed Mar 15, 2024
1 parent de74c23 commit f72936d
Show file tree
Hide file tree
Showing 17 changed files with 20 additions and 48 deletions.
4 changes: 1 addition & 3 deletions cmd/apply-entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// applyEntryCmd represents the entry command
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions cmd/apply-folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// applyFolderCmd represents the folder command
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions cmd/create-entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// createEntryCmd represents the entry command
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions cmd/create-folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// createFolderCmd represents the folder command
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions cmd/delete-entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// deleteEntryCmd represents the entry command
Expand All @@ -44,8 +43,7 @@ pleasant-cli delete entry --id <id> --delete --useraccess <accessrowid>`,
return
}

baseUrl := viper.GetString("serverurl")
bearerToken := viper.GetString("bearertoken.accesstoken")
baseUrl, bearerToken := pleasant.LoadConfig()

var identifier string

Expand Down
4 changes: 1 addition & 3 deletions cmd/delete-folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// deleteFolderCmd represents the entry command
Expand All @@ -45,8 +44,7 @@ pleasant-cli delete folder --id <id> --delete --useraccess <accessrowid>`,
return
}

baseUrl := viper.GetString("serverurl")
bearerToken := viper.GetString("bearertoken.accesstoken")
baseUrl, bearerToken := pleasant.LoadConfig()

var identifier string

Expand Down
4 changes: 1 addition & 3 deletions cmd/get-accesslevels.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// getAccesslevelsCmd represents the accesslevels command
Expand All @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions cmd/get-entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// getEntryCmd represents the entry command
Expand All @@ -43,8 +42,7 @@ pleasant-cli get entry --path <path> --attachments`,
return
}

baseUrl := viper.GetString("serverurl")
bearerToken := viper.GetString("bearertoken.accesstoken")
baseUrl, bearerToken := pleasant.LoadConfig()

var identifier string

Expand Down
4 changes: 1 addition & 3 deletions cmd/get-folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// getFolderCmd represents the folder command
Expand All @@ -38,8 +37,7 @@ pleasant-cli get folder --path <path>`,
return
}

baseUrl := viper.GetString("serverurl")
bearerToken := viper.GetString("bearertoken.accesstoken")
baseUrl, bearerToken := pleasant.LoadConfig()

var identifier string

Expand Down
4 changes: 1 addition & 3 deletions cmd/get-folders.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// getFoldersCmd represents the folders command
Expand All @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions cmd/get-passwordstrength.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// getPasswordStrengthCmd represents the passwordstrength command
Expand All @@ -37,8 +36,7 @@ pleasant-cli get passwordstrength -p <PASSWORD>`,
return
}

baseUrl := viper.GetString("serverurl")
bearerToken := viper.GetString("bearertoken.accesstoken")
baseUrl, bearerToken := pleasant.LoadConfig()

pw, err := cmd.Flags().GetString("password")
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions cmd/get-rootfolder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// getRootfolderCmd represents the rootfolder command
Expand All @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions cmd/get-serverinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// getServerInfoCmd represents the serverinfo command
Expand All @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions cmd/patch-entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// patchEntryCmd represents the entry command
Expand All @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions cmd/patch-folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// patchFolderCmd represents the folder command
Expand All @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/marevers/pleasant-cli/pleasant"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// searchCmd represents the search command
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions pleasant/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f72936d

Please sign in to comment.