From 58e6c6b858c41322d7e8f889d96a29a9bec99889 Mon Sep 17 00:00:00 2001 From: TharinduWijewardane Date: Fri, 11 Oct 2019 16:32:16 +0530 Subject: [PATCH] Validate whether the user is logged in first --- internal/pkg/client/config.go | 2 +- internal/pkg/client/helper.go | 6 ++++++ internal/pkg/cmd/application/create.go | 4 ++++ internal/pkg/cmd/application/list.go | 4 ++++ internal/pkg/cmd/auth/connect.go | 6 ++++++ 5 files changed, 21 insertions(+), 1 deletion(-) diff --git a/internal/pkg/client/config.go b/internal/pkg/client/config.go index 6881a4a..cba83f9 100644 --- a/internal/pkg/client/config.go +++ b/internal/pkg/client/config.go @@ -21,5 +21,5 @@ var EnvConfigs = map[string]string{ } var UserConfigs = map[string]string{ - AccessToken: "login.oauth2.accessToken", + AccessToken: "", } diff --git a/internal/pkg/client/helper.go b/internal/pkg/client/helper.go index 011c9b7..b853a9d 100644 --- a/internal/pkg/client/helper.go +++ b/internal/pkg/client/helper.go @@ -46,3 +46,9 @@ func NewRequest(cliContext runtime.CliContext, method, path string, body io.Read return req, err } + +func IsUserLoggedIn(cliContext runtime.UserConfigHolder) bool { + + token := cliContext.UserConfig().GetString(AccessToken) + return token != "" +} diff --git a/internal/pkg/cmd/application/create.go b/internal/pkg/cmd/application/create.go index 398d9ed..3e80552 100644 --- a/internal/pkg/cmd/application/create.go +++ b/internal/pkg/cmd/application/create.go @@ -42,6 +42,10 @@ func NewCreateCommand(cliContext runtime.CliContext) *cobra.Command { func runCreateAppCommand(cliContext runtime.CliContext) func(cmd *cobra.Command, args []string) { return func(cmd *cobra.Command, args []string) { + if !client.IsUserLoggedIn(cliContext) { + common.ExitWithErrorMessage(cliContext.Out(), "Please login first") + } + description, _ := cmd.Flags().GetString("description") app := Application{args[0], description} diff --git a/internal/pkg/cmd/application/list.go b/internal/pkg/cmd/application/list.go index 2a9c0df..7cd0a0d 100644 --- a/internal/pkg/cmd/application/list.go +++ b/internal/pkg/cmd/application/list.go @@ -41,6 +41,10 @@ func NewListCommand(cliContext runtime.CliContext) *cobra.Command { func runListAppCommand(cliContext runtime.CliContext) func(cmd *cobra.Command, args []string) { return func(cmd *cobra.Command, args []string) { + if !client.IsUserLoggedIn(cliContext) { + common.ExitWithErrorMessage(cliContext.Out(), "Please login first") + } + listApps(cliContext) } } diff --git a/internal/pkg/cmd/auth/connect.go b/internal/pkg/cmd/auth/connect.go index 0bb586b..8d03f95 100644 --- a/internal/pkg/cmd/auth/connect.go +++ b/internal/pkg/cmd/auth/connect.go @@ -12,6 +12,7 @@ package auth import ( "fmt" "github.com/spf13/cobra" + "github.com/wso2/choreo-cli/internal/pkg/client" "github.com/wso2/choreo-cli/internal/pkg/cmd/common" "github.com/wso2/choreo-cli/internal/pkg/cmd/runtime" "github.com/wso2/choreo-cli/internal/pkg/source/github" @@ -36,6 +37,11 @@ func runConnectCommand(cliContext runtime.CliContext) func(cmd *cobra.Command, a consoleWriter := cliContext.Out() return func(cmd *cobra.Command, args []string) { + + if !client.IsUserLoggedIn(cliContext) { + common.ExitWithErrorMessage(consoleWriter, "Please login first") + } + if strings.ToLower(args[0]) == sourceProviderGithub { if github.PerformGithubAuthorization(cliContext) { common.PrintInfo(consoleWriter, "GitHub authorization successful.")