Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Commit

Permalink
Login Command Check (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunussandikci authored Nov 20, 2020
1 parent 8a91a39 commit c237c3c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/azurePeering.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var azurePeeringCmd = &cobra.Command{
var azurePeeringCreateCmd = &cobra.Command{
Use: "create",
Short: "This command creates Azure vNet Peering between your own vNet and your Enterprise Hazelcast cluster vNet.",
Example: "hzcloud azure-peering create --cluster-id=1 --project-id=2 --network-name=3",
Example: "hzcloud azure-peering create --cluster-id=1 --tenant-id=foo --subscription-id=bar --resource-group=baz --vnet=qux",
Run: func(cmd *cobra.Command, args []string) {
client := internal.NewClient()
indicator := util.NewLoadingIndicator("Azure Peering starting...", 100)
Expand Down
24 changes: 16 additions & 8 deletions cmd/login.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cmd

import (
"bufio"
"fmt"
"github.com/fatih/color"
"github.com/hazelcast/hazelcast-cloud-cli/internal"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
Expand All @@ -16,15 +16,23 @@ var loginCmd = &cobra.Command{
Aliases: []string{"login"},
Short: "This command logins you to Hazelcast Cloud with api-key and api-secret.",
RunE: func(cmd *cobra.Command, args []string) error {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Api Key: ")
apiKey, _ := reader.ReadString('\n')
fmt.Print("Api Secret: ")
fmt.Print("API Key: ")
apiKey, _ := terminal.ReadPassword(int(os.Stdin.Fd()))
fmt.Printf("\r\033[K")
fmt.Print("API Secret: ")
apiSecret, _ := terminal.ReadPassword(int(os.Stdin.Fd()))
configService := internal.NewConfigService()
configService.Set(internal.ApiKey, strings.TrimSpace(apiKey))
configService.Set(internal.ApiSecret, strings.TrimSpace(string(apiSecret)))
fmt.Printf("\r\033[K")
apiKeyString := strings.TrimSpace(string(apiKey))
apiSecretString := strings.TrimSpace(string(apiSecret))

loginResult, response, loginErr := internal.Login(apiKeyString, apiSecretString)
internal.Validate(loginResult, response, loginErr)
if loginErr == nil {
configService := internal.NewConfigService()
configService.Set(internal.ApiKey, apiKeyString)
configService.Set(internal.ApiSecret, apiSecretString)
color.Green("You have successfully logged into Hazelcast Cloud.")
}
return nil
},
}
Expand Down
21 changes: 9 additions & 12 deletions internal/hazelcastCloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ import (
"strings"
)

var hazelcastCloudClient *hazelcastcloud.Client

func NewClient() *hazelcastcloud.Client {
var apiKey = os.Getenv("HZ_CLOUD_API_KEY")
var apiSecret = os.Getenv("HZ_CLOUD_API_SECRET")
var apiUrl = os.Getenv("HZ_CLOUD_API_URL")
var client interface{}

if len(strings.TrimSpace(apiKey)) == 0 || len(strings.TrimSpace(apiSecret)) == 0 {
configService := NewConfigService()
Expand All @@ -29,25 +25,26 @@ func NewClient() *hazelcastcloud.Client {
os.Exit(1)
}

return Validate(Login(apiKey, apiSecret)).(*hazelcastcloud.Client)
}

func Login(apiKey string, apiSecret string) (*hazelcastcloud.Client, *hazelcastcloud.Response, error) {
var apiUrl = os.Getenv("HZ_CLOUD_API_URL")
if len(strings.TrimSpace(apiUrl)) != 0 {
client = Validate(hazelcastcloud.NewFromCredentials(apiKey, apiSecret, hazelcastcloud.OptionEndpoint(apiUrl)))
} else {
client = Validate(hazelcastcloud.NewFromCredentials(apiKey, apiSecret))
return hazelcastcloud.NewFromCredentials(apiKey, apiSecret, hazelcastcloud.OptionEndpoint(apiUrl))
}

hazelcastCloudClient = client.(*hazelcastcloud.Client)
return hazelcastCloudClient
return hazelcastcloud.NewFromCredentials(apiKey, apiSecret)
}

func Validate(a interface{}, b *hazelcastcloud.Response, c error) interface{} {
if c != nil {
if reflect.TypeOf(c) == reflect.TypeOf(&hazelcastcloud.ErrorResponse{}) {
color.Red("Message:%s CorrelationId:%s", c.(*hazelcastcloud.ErrorResponse).Message,
color.Red("Message:%s\nCorrelationId:%s", c.(*hazelcastcloud.ErrorResponse).Message,
c.(*hazelcastcloud.ErrorResponse).CorrelationId)
} else {
color.Red(c.Error())
}
os.Exit(1)
}
return a
}
}

0 comments on commit c237c3c

Please sign in to comment.