Skip to content

Commit

Permalink
Adding update command, using colored output for increased visibility,…
Browse files Browse the repository at this point in the history
… and auth now checks existing token first
  • Loading branch information
hunoz committed May 27, 2023
1 parent 5e5a4d1 commit 86ea5af
Show file tree
Hide file tree
Showing 19 changed files with 233 additions and 91 deletions.
29 changes: 5 additions & 24 deletions cmd/auth/auth.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package auth

import (
"fmt"
"os"
"strings"

"github.com/manifoldco/promptui"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gtech.dev/spark/cognito"
Expand All @@ -23,39 +22,21 @@ var AuthCmd = &cobra.Command{
config.CheckIfCognitoIsInitialized()
if config, e := config.GetCognitoConfig(); e != nil {
if strings.Contains(e.Error(), "Invalid region") {
fmt.Println("Spark has not been initialized. Please run 'spark init' to initialize Spark.")
color.Red("Spark has not been initialized. Please run 'spark init' to initialize Spark.")
} else {
fmt.Printf("Error getting config: %s\n", e.Error())
color.Red("Error getting config: %v", e.Error())
}
os.Exit(1)
} else {
configuration = config
}
passwordValidator := cognito.CheckIfValidPassword
usernamePrompt := promptui.Prompt{
Label: "Username",
}
username, err := usernamePrompt.Run()
if err != nil {
fmt.Printf("Error: %s\n", err)
}

passwordPrompt := promptui.Prompt{
Label: "Password",
Mask: '*',
Validate: passwordValidator,
}
password, err := passwordPrompt.Run()
if err != nil {
fmt.Printf("Error: %s\n", err)
}

cognitoClient := cognito.New(configuration)

cognitoClient.InitiateAuth(username, password, viper.GetBool(FlagKey.Force))
cognitoClient.InitiateAuth(viper.GetBool(FlagKey.Force))
},
}

func init() {
AuthCmd.Flags().Bool(FlagKey.Force, false, "Force update session, even if expiration time is > 6 hours")
AuthCmd.Flags().BoolP(FlagKey.Force, string(FlagKey.Force[0]), false, "Force update session, even if expiration time is > 6 hours")
}
4 changes: 2 additions & 2 deletions cmd/change-password/change-password.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package changepassword

import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/spf13/cobra"
"gtech.dev/spark/cognito"
"gtech.dev/spark/config"
Expand All @@ -16,7 +16,7 @@ var ChangePasswordCmd = &cobra.Command{
var configuration *config.CognitoConfig
config.CheckIfCognitoIsInitialized()
if config, e := config.GetCognitoConfig(); e != nil {
fmt.Printf("Error getting config: %s\n", e.Error())
color.Red("Error getting config: %v", e.Error())
os.Exit(1)
} else {
configuration = config
Expand Down
4 changes: 2 additions & 2 deletions cmd/first-sign-in/perform-first-sign-in.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package firstsignin

import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/spf13/cobra"
"gtech.dev/spark/cognito"
"gtech.dev/spark/config"
Expand All @@ -16,7 +16,7 @@ var FirstSignInCmd = &cobra.Command{
var configuration *config.CognitoConfig
config.CheckIfCognitoIsInitialized()
if config, e := config.GetCognitoConfig(); e != nil {
fmt.Printf("Error getting config: %s\n", e.Error())
color.Red("Error getting config: %v", e.Error())
os.Exit(1)
} else {
configuration = config
Expand Down
29 changes: 19 additions & 10 deletions cmd/init/init.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package init

import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -18,9 +18,11 @@ func getClientIdFromAllOptions() string {
Label: "Client ID",
}
clientId, _ = clientIdPrompt.Run()
} else {
clientId = clientIdFromCli
}
if clientId == "" {
fmt.Println("Client ID cannot be empty")
color.Red("Client ID cannot be empty")
os.Exit(1)
}
return clientId
Expand All @@ -31,13 +33,18 @@ func getRegionFromAllOptions() string {
regionFromCli := viper.GetString(FlagKey.Region)
if regionFromCli == "" {
regionPrompt := promptui.Prompt{
Label: "Region",
Label: "Region",
Validate: config.IsValidAwsRegion,
}
region, _ = regionPrompt.Run()
} else {
region = regionFromCli
}
if region == "" {
fmt.Println("Region cannot be empty")
color.Red("Region cannot be empty")
os.Exit(1)
} else if err := config.IsValidAwsRegion(region); err != nil {
color.Red(err.Error())
}
return region
}
Expand All @@ -50,9 +57,11 @@ func getPoolIdFromAllOptions() string {
Label: "Pool ID",
}
poolId, _ = poolIdPrompt.Run()
} else {
poolId = poolIdFromCli
}
if poolId == "" {
fmt.Println("Pool ID cannot be empty")
color.Red("Pool ID cannot be empty")
os.Exit(1)
}
return poolId
Expand All @@ -79,7 +88,7 @@ var InitCmd = &cobra.Command{
Region: region,
PoolId: poolId,
}); err != nil {
fmt.Printf("Error updating config: %v\n", err.Error())
color.Red("Error updating config: %v", err.Error())
os.Exit(1)
}
} else {
Expand All @@ -92,21 +101,21 @@ var InitCmd = &cobra.Command{
poolId = getPoolIdFromAllOptions()
} else {
if configuration.ClientId != "" && !overwrite {
fmt.Println("Client ID already configured")
color.Cyan("Client ID already configured")
clientId = configuration.ClientId
} else if configuration.ClientId == "" {
clientId = getClientIdFromAllOptions()
}

if configuration.Region != "" && !overwrite {
fmt.Println("Region already configured")
color.Cyan("Region already configured")
region = configuration.Region
} else if configuration.Region == "" {
region = getRegionFromAllOptions()
}

if configuration.PoolId != "" && !overwrite {
fmt.Println("Pool ID already configured")
color.Cyan("Pool ID already configured")
poolId = configuration.PoolId
} else if configuration.PoolId == "" {
poolId = getPoolIdFromAllOptions()
Expand All @@ -122,7 +131,7 @@ var InitCmd = &cobra.Command{
Session: configuration.Session,
Expires: configuration.Expires,
}); err != nil {
fmt.Printf("Error updating config: %v\n", err.Error())
color.Red("Error updating config: %v", err.Error())
os.Exit(1)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/register-totp/register-totp.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package registertotp

import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/spf13/cobra"
"gtech.dev/spark/cognito"
"gtech.dev/spark/config"
Expand All @@ -17,7 +17,7 @@ var RegisterTotpCmd = &cobra.Command{
var configuration *config.CognitoConfig
config.CheckIfCognitoIsInitialized()
if config, e := config.GetCognitoConfig(); e != nil {
fmt.Printf("Error getting config: %s\n", e.Error())
color.Red("Error getting config: %v", e.Error())
os.Exit(1)
} else {
configuration = config
Expand Down
4 changes: 2 additions & 2 deletions cmd/reset-password/reset-password.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package resetpassword

import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/spf13/cobra"
"gtech.dev/spark/cognito"
"gtech.dev/spark/config"
Expand All @@ -17,7 +17,7 @@ var ResetPasswordCmd = &cobra.Command{
var configuration *config.CognitoConfig
config.CheckIfCognitoIsInitialized()
if config, e := config.GetCognitoConfig(); e != nil {
fmt.Printf("Error getting config: %s\n", e.Error())
color.Red("Error getting config: %v", e.Error())
os.Exit(1)
} else {
configuration = config
Expand Down
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
cmdInit "gtech.dev/spark/cmd/init"
registertotp "gtech.dev/spark/cmd/register-totp"
resetpassword "gtech.dev/spark/cmd/reset-password"
"gtech.dev/spark/cmd/update"
)

var cmdVersion = "1.0.1"

var RootCmd = &cobra.Command{
Use: "spark",
Short: "Utilities for interacting with Cognito",
Expand All @@ -24,19 +23,20 @@ var RootCmd = &cobra.Command{
}

if version {
fmt.Println(cmdVersion)
fmt.Println(update.CmdVersion)
} else {
cmd.Help()
}
},
}

func init() {
RootCmd.Flags().Bool("version", false, "Current version of Spark")
RootCmd.Flags().BoolP("version", "v", false, "Current version of Spark")
RootCmd.AddCommand(auth.AuthCmd)
RootCmd.AddCommand(changepassword.ChangePasswordCmd)
RootCmd.AddCommand(registertotp.RegisterTotpCmd)
RootCmd.AddCommand(resetpassword.ResetPasswordCmd)
RootCmd.AddCommand(firstsignin.FirstSignInCmd)
RootCmd.AddCommand(cmdInit.InitCmd)
RootCmd.AddCommand(update.UpdateCmd)
}
89 changes: 89 additions & 0 deletions cmd/update/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package update

import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"runtime"

"github.com/spf13/cobra"

"github.com/fatih/color"
)

var CmdVersion = "1.0.2"

type Release struct {
Url string `json:"url,omitempty"`
AssetsUrl string `json:"assets_url,omitempty"`
UploadUrl string `json:"upload_url,omitempty"`
TagName string `json:"tag_name,omitempty"`
}

func CmdIsLatestVersion() (*string, bool) {
response, err := http.Get("https://api.github.com/repos/hunoz/SparkCli/releases/latest")
if err != nil {
color.Red("Error fetching latest release: %v", err.Error())
os.Exit(1)
}

defer response.Body.Close()

body, err := io.ReadAll(response.Body)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}

release := Release{}
err = json.Unmarshal(body, &release)
if err != nil {
color.Red("Error reading latest release: %v", err.Error())
os.Exit(1)
}

if release.TagName == CmdVersion || release.TagName < CmdVersion {
return &release.TagName, true
}

return &release.TagName, false
}

var UpdateCmd = &cobra.Command{
Use: "update",
Short: "Update CLI if an update is available",
Run: func(cmd *cobra.Command, args []string) {
latestVersion, isLatestVersion := CmdIsLatestVersion()
if isLatestVersion {
color.Green("Spark is already running at the latest version")
os.Exit(0)
}

path, _ := os.Executable()
out, err := os.Create(path)
if err != nil {
color.Red("Error creating file at %v: %v", path, err.Error())
os.Exit(1)
}

// Here we need to get the asset for the current architecture
assetFilename := fmt.Sprintf("spark-%v-%v", runtime.GOOS, runtime.GOARCH)
downloadUrl := fmt.Sprintf("https://github.com/hunoz/SparkCli/releases/download/%v/%v", latestVersion, assetFilename)

response, err := http.Get(downloadUrl)
if err != nil {
color.Red("Error downloading latest version: %v", err.Error())
os.Exit(1)
}

defer response.Body.Close()

_, err = io.Copy(out, response.Body)
if err != nil {
color.Red("Error updating to latest version: %v", err.Error())
}

color.Green("Successfully updated Spark to version %v", latestVersion)
},
}
Loading

0 comments on commit 86ea5af

Please sign in to comment.