Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,39 @@ package cmd

import (
"fmt"
"time"

"github.com/paastech-cloud/cli/pkg/auth"
"github.com/paastech-cloud/cli/internal/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var accountCmd = &cobra.Command{
GroupID: "account",
Use: "account",
Short: "Get infos about user account",
Long: "Get infos about user account",
RunE: func(cmd *cobra.Command, args []string) error {
status, err := auth.Status()
fmt.Println(status)
return err
jwt, err := config.ExtractJWTInfos()
if err != nil {
return err
}

fmt.Println("👤 You are logged in as: " + jwt.Username)
fmt.Println("🌐 Server: " + viper.GetString("server"))
timeDiff := jwt.ExpirationTime.Sub(time.Now())
if timeDiff > 0 {
fmt.Println(
"⌛ Current session expires in: " + fmt.Sprintf(
"%02dh%02dm%02ds",
int(timeDiff.Hours()),
int(timeDiff.Minutes())%60,
int(timeDiff.Seconds())%60,
),
)
} else {
fmt.Println("❌ Current session is expired. Please log back in.")
}
return nil
},
}

Expand Down
43 changes: 35 additions & 8 deletions cmd/login.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cmd

import (
"errors"
"fmt"
"strings"

"github.com/paastech-cloud/cli/internal/config"
"github.com/paastech-cloud/cli/pkg/auth"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
Expand All @@ -11,39 +14,63 @@ import (
var (
email string
password string
server string
)

var loginCmd = &cobra.Command{
GroupID: "account",
Use: "login",
Short: "Log in to PaaSTech.cloud",
Long: "Log in to PaaSTech.cloud",
Short: "Log in to PaaSTech",
RunE: func(cmd *cobra.Command, args []string) error {
email, _ := cmd.Flags().GetString("email")
password, _ := cmd.Flags().GetString("password")

// Ask for email and password if not present in flags
if len(email) == 0 || len(password) == 0 {
fmt.Print("Email: ")
fmt.Scan(&email)
fmt.Scanln(&email)

fmt.Print("Password: ")
p, _ := terminal.ReadPassword(0)
password = string(p)
fmt.Print("\n")
}
if len(email) == 0 || len(password) == 0 {
return errors.New("Email and password cannot be empty. Please try again.")
}

// Use PaaSTech API server by default
if server == "" {
server = "https://api.paastech.cloud"
} else {
server = strings.TrimSuffix(server, "/")
}

fmt.Printf("🔐 Logging in as %s...\n", email)
err := auth.Login(email, password)
if err == nil {
fmt.Println("✅ Login successful")
// Send login request
fmt.Printf("🔐 Logging in as %s\n", email)
jwt, err := auth.Login(server, email, password)
if err != nil {
return err
}
return err

// Load auth config
err = config.LoadAuthConfig()
if err != nil {
return err
}

// Save jwt in auth conf
config.SetAuth(server, jwt)
fmt.Println("✅ Login successful")

return nil
},
}

func init() {
rootCmd.AddCommand(loginCmd)

loginCmd.Flags().StringVarP(&server, "server", "", "", "Server URL")
loginCmd.Flags().StringVarP(&email, "email", "e", "", "Email")
loginCmd.Flags().StringVarP(&password, "password", "p", "", "Password")
loginCmd.MarkFlagsRequiredTogether("email", "password")
Expand Down
13 changes: 11 additions & 2 deletions cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"

"github.com/paastech-cloud/cli/pkg/auth"
"github.com/paastech-cloud/cli/internal/config"
"github.com/spf13/cobra"
)

Expand All @@ -13,8 +13,17 @@ var logoutCmd = &cobra.Command{
Short: "Log out from PaaSTech.cloud",
Long: "Log out from PaaSTech.cloud",
RunE: func(cmd *cobra.Command, args []string) error {
// Load auth config
err := config.LoadAuthConfig()
if err != nil {
return err
}

// Save empty jwt in auth conf
config.SetAuth("", "")
fmt.Println("🚪 Logging out")
return auth.Logout()

return nil
},
}

Expand Down
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module github.com/paastech-cloud/cli
go 1.20

require (
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
golang.org/x/crypto v0.10.0
)

Expand All @@ -13,18 +15,30 @@ require (
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.4.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/skeema/knownhosts v1.1.1 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/text v0.10.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
Expand Down
Loading