Skip to content

Commit

Permalink
Call beforeCommand() at switch as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhuan Oliveira committed May 7, 2022
1 parent 93be6f4 commit 979db6b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var editCmd = &cobra.Command{
Short: "Edit pages",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
wikiConfig, apiCredentials, hookOptions := beforeCommand()
wikiConfig, apiCredentials, hookOptions := beforeCommand(true)
pageName := args[0]

page, err := mw.GetPage(
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var exportCmd = &cobra.Command{
os.Exit(1)
}

wikiConfig, apiCredentials, hook := beforeCommand()
wikiConfig, apiCredentials, hook := beforeCommand(true)
exportTo := args[0]

exportCount := 0
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var importCmd = &cobra.Command{
Use: "import",
Short: "Import pages and images",
Run: func(cmd *cobra.Command, filePaths []string) {
wikiConfig, apiCredentials, hook := beforeCommand()
wikiConfig, apiCredentials, hook := beforeCommand(true)
userConfig, err := config.Get()
if err != nil {
panic(err)
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var switchCmd = &cobra.Command{
Short: "Switch between your available Wikis.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
beforeCommand(false)

configRoot, err := config.GetAll()
if err != nil {
panic(err)
Expand Down
13 changes: 8 additions & 5 deletions internal/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func handleErrorGettingApiCredentials(err error, user string, wikiAddress string
fmt.Println("Failed to get API Credentials.")
}

func beforeCommand() (*mw.Config, *mw.ApiCredentials, *mw.HookOptions) {
func beforeCommand(withApiCredentials bool) (*mw.Config, *mw.ApiCredentials, *mw.HookOptions) {
userConfig, err := config.Get()
if errors.Is(err, config.ErrConfigDoesNotExist) {
fmt.Println("You don't seem to have a configuration file. Try 'wikicmd config' to initialize a new configuration.")
Expand Down Expand Up @@ -57,11 +57,14 @@ func beforeCommand() (*mw.Config, *mw.ApiCredentials, *mw.HookOptions) {
Password: userConfig.Password,
}

apiCredentials, err := mw.GetApiCredentials(wikiConfig, hook)
if err != nil {
handleErrorGettingApiCredentials(err, userConfig.User, userConfig.Address)
apiCredentials := &mw.ApiCredentials{}
if withApiCredentials {
apiCredentials, err = mw.GetApiCredentials(wikiConfig, hook)
if err != nil {
handleErrorGettingApiCredentials(err, userConfig.User, userConfig.Address)

os.Exit(1)
os.Exit(1)
}
}

return wikiConfig, apiCredentials, hook
Expand Down

0 comments on commit 979db6b

Please sign in to comment.