Skip to content

Commit

Permalink
Added logout command
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Kosiewski <[email protected]>
  • Loading branch information
Thomas Kosiewski committed Sep 21, 2023
1 parent fff3cf4 commit 6904aed
Show file tree
Hide file tree
Showing 26 changed files with 317 additions and 368 deletions.
3 changes: 2 additions & 1 deletion cmd/vclusterctl/cmd/login/notpro.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ package login

import (
"github.com/loft-sh/vcluster/cmd/vclusterctl/flags"
"github.com/loft-sh/vcluster/pkg/constants"
"github.com/spf13/cobra"
)

func NewLoginCmd(*flags.GlobalFlags) (*cobra.Command, error) {
return nil, nil
return nil, constants.ErrOnlyInPro
}
13 changes: 8 additions & 5 deletions cmd/vclusterctl/cmd/login/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ func NewLoginCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
loftctlGlobalFlags := &loftctlflags.GlobalFlags{
Silent: globalFlags.Silent,
Debug: globalFlags.Debug,
Config: globalFlags.Config,
LogOutput: globalFlags.LogOutput,
}

var err error
loftctlGlobalFlags.Config, err = pro.GetConfigFilePath()
if err != nil {
return nil, fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
if globalFlags.Config != "" {
loftctlGlobalFlags.Config = globalFlags.Config
} else {
var err error
loftctlGlobalFlags.Config, err = pro.GetLoftConfigFilePath()
if err != nil {
return nil, fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
}
}

loginCmd := loftctl.NewLoginCmd(loftctlGlobalFlags)
Expand Down
14 changes: 14 additions & 0 deletions cmd/vclusterctl/cmd/logout/notpro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build !pro
// +build !pro

package logout

import (
"github.com/loft-sh/vcluster/cmd/vclusterctl/flags"
"github.com/loft-sh/vcluster/pkg/constants"
"github.com/spf13/cobra"
)

func NewLogoutCmd(*flags.GlobalFlags) (*cobra.Command, error) {
return nil, constants.ErrOnlyInPro
}
45 changes: 45 additions & 0 deletions cmd/vclusterctl/cmd/logout/pro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build pro
// +build pro

package logout

import (
"fmt"

loftctl "github.com/loft-sh/loftctl/v3/cmd/loftctl/cmd"
loftctlflags "github.com/loft-sh/loftctl/v3/cmd/loftctl/flags"
"github.com/loft-sh/vcluster/cmd/vclusterctl/flags"
"github.com/loft-sh/vcluster/pkg/pro"
"github.com/spf13/cobra"
)

func NewLogoutCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
loftctlGlobalFlags := &loftctlflags.GlobalFlags{
Silent: globalFlags.Silent,
Debug: globalFlags.Debug,
LogOutput: globalFlags.LogOutput,
}

if globalFlags.Config != "" {
loftctlGlobalFlags.Config = globalFlags.Config
} else {
var err error
loftctlGlobalFlags.Config, err = pro.GetLoftConfigFilePath()
if err != nil {
return nil, fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
}
}

logoutCmd := loftctl.NewLogoutCmd(loftctlGlobalFlags)

logoutCmd.Use = "logout"
logoutCmd.Long = `########################################################
Log out of vCluster.Pro
Example:
vcluster logout
########################################################
`

return logoutCmd, nil
}
3 changes: 2 additions & 1 deletion cmd/vclusterctl/cmd/pro/notpro.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ package pro

import (
"github.com/loft-sh/vcluster/cmd/vclusterctl/flags"
"github.com/loft-sh/vcluster/pkg/constants"
"github.com/spf13/cobra"
)

func NewProCmd(*flags.GlobalFlags) (*cobra.Command, error) {
return nil, nil
return nil, constants.ErrOnlyInPro
}
2 changes: 1 addition & 1 deletion cmd/vclusterctl/cmd/pro/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewProCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) {
func NewStartCmd(loftctlGlobalFlags *loftctlflags.GlobalFlags) (*cobra.Command, error) {
starCmd := loftctl.NewStartCmd(loftctlGlobalFlags)

configPath, err := pro.GetConfigFilePath()
configPath, err := pro.GetLoftConfigFilePath()
if err != nil {
return nil, fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
}
Expand Down
23 changes: 22 additions & 1 deletion cmd/vclusterctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package cmd

import (
"context"
"errors"
"fmt"
"os"

"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/get"
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/login"
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/logout"
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/pro"
"github.com/loft-sh/vcluster/cmd/vclusterctl/cmd/telemetry"
"github.com/loft-sh/vcluster/cmd/vclusterctl/flags"
"github.com/loft-sh/vcluster/cmd/vclusterctl/log"
"github.com/loft-sh/vcluster/pkg/constants"
"github.com/loft-sh/vcluster/pkg/upgrade"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -86,13 +90,30 @@ func BuildRoot(log log.Logger) (*cobra.Command, error) {

// add pro commands
proCmd, err := pro.NewProCmd(globalFlags)
if err != nil {
if err != nil && !errors.Is(err, constants.ErrOnlyInPro) {
return nil, fmt.Errorf("failed to create pro command: %w", err)
}
if proCmd != nil {
rootCmd.AddCommand(proCmd)
}

loginCmd, err := login.NewLoginCmd(globalFlags)
if err != nil && !errors.Is(err, constants.ErrOnlyInPro) {
return nil, fmt.Errorf("failed to create login command: %w", err)
}
if loginCmd != nil {
rootCmd.AddCommand(loginCmd)
}

logoutCmd, err := logout.NewLogoutCmd(globalFlags)
if err != nil && !errors.Is(err, constants.ErrOnlyInPro) {
return nil, fmt.Errorf("failed to create logout command: %w", err)
}
if logoutCmd != nil {
rootCmd.AddCommand(logoutCmd)
}

// add completion command
err = rootCmd.RegisterFlagCompletionFunc("namespace", newNamespaceCompletionFunc(rootCmd.Context()))
if err != nil {
return rootCmd, fmt.Errorf("failed to register completion for namespace: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/hashicorp/golang-lru/v2 v2.0.2
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.2.0
github.com/loft-sh/loftctl/v3 v3.0.0-20230921095454-4a09cdaacca3
github.com/loft-sh/loftctl/v3 v3.0.0-20230921143437-669b265e3ecf
github.com/loft-sh/utils v0.0.25
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d
github.com/mitchellh/go-homedir v1.1.0
Expand Down Expand Up @@ -76,7 +76,7 @@ require (
github.com/google/gnostic-models v0.6.8 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/loft-sh/agentapi/v3 v3.3.0-ci.1.0.20230921083523-e1d74f6f8fd1 // indirect
github.com/loft-sh/api/v3 v3.3.0-alpha.21 // indirect
github.com/loft-sh/api/v3 v3.0.0-20230921143328-114580f85fdd // indirect
github.com/loft-sh/apiserver v0.0.0-20230628051307-f26967fbb40f // indirect
github.com/loft-sh/external-types v0.0.2-0.20230301201552-ec939da949b4 // indirect
github.com/loft-sh/jspolicy v0.1.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -550,16 +550,16 @@ github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhn
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
github.com/loft-sh/agentapi/v3 v3.3.0-ci.1.0.20230921083523-e1d74f6f8fd1 h1:XE+JbvcExJd45Oh3Jm2d62uPmDP2luPf0mQ3NZZMMuY=
github.com/loft-sh/agentapi/v3 v3.3.0-ci.1.0.20230921083523-e1d74f6f8fd1/go.mod h1:8jmVWFBcLjwh6Tv2KPVxkIjUJpVxUMROcbYYVYxR1pI=
github.com/loft-sh/api/v3 v3.3.0-alpha.21 h1:akI1V79u/aPePeQue6UsAff67pvS6ok0/NxbmND8wHw=
github.com/loft-sh/api/v3 v3.3.0-alpha.21/go.mod h1:eyXGuTGTpJmWAlmXM1QQMHwFc6t9jhj+imrrPdj9syA=
github.com/loft-sh/api/v3 v3.0.0-20230921143328-114580f85fdd h1:8UQwV6TK1QAHlsdrL8we59IKrPH1z8nSO6+mI2lziXs=
github.com/loft-sh/api/v3 v3.0.0-20230921143328-114580f85fdd/go.mod h1:eIbt+Ze6F2fW651JE3VTlFsool+S/hX603aFG5s/AZg=
github.com/loft-sh/apiserver v0.0.0-20230628051307-f26967fbb40f h1:eeLBYlrGOvzJK3h7JYdBGpRvdjzD/sTCVPL2lIQsXlg=
github.com/loft-sh/apiserver v0.0.0-20230628051307-f26967fbb40f/go.mod h1:Mlbl/QTA48cbKpQcmYMA56TQ+Z6Nna7t7ocbCoCmf5w=
github.com/loft-sh/external-types v0.0.2-0.20230301201552-ec939da949b4 h1:eSuBR+6j7MWmab6AU8WGd4EYQiRCZKupxThUxqf38C8=
github.com/loft-sh/external-types v0.0.2-0.20230301201552-ec939da949b4/go.mod h1:qnerIOkD67CmTkV8HsXQit6BfB7Qiw45bSUy1oefIXU=
github.com/loft-sh/jspolicy v0.1.0 h1:FNAWR6tRX5NRWxAf9RI/86NRH83NXYbobLHSZyMND74=
github.com/loft-sh/jspolicy v0.1.0/go.mod h1:4Zi38iEB0JvhnkrNHPpoueSUWQ1OlHMNB9JHTGEsPO0=
github.com/loft-sh/loftctl/v3 v3.0.0-20230921095454-4a09cdaacca3 h1:NVjFE4y/QD2Ol3tkdhTnv+iV8baktqGmxodpr2vZVQo=
github.com/loft-sh/loftctl/v3 v3.0.0-20230921095454-4a09cdaacca3/go.mod h1:iN3Q75vfQDNJrJLKK7YmPVYJhNGPZcV7J0fbsrosnQA=
github.com/loft-sh/loftctl/v3 v3.0.0-20230921143437-669b265e3ecf h1:SwIal5P1coM01bw3OYz0PLokpu4iTaYOMlPH1WfWBjk=
github.com/loft-sh/loftctl/v3 v3.0.0-20230921143437-669b265e3ecf/go.mod h1:I+dMG4zKZWOn+CcteN0cyPGOclHlqqmXBC6THcUwj60=
github.com/loft-sh/log v0.0.0-20230824104949-bd516c25712a h1:/gqqjKpcHEdFXIX41lx1Y/FBqT/72gbPpf7sa20tyM8=
github.com/loft-sh/log v0.0.0-20230824104949-bd516c25712a/go.mod h1:YImeRjXH34Yf5E79T7UHBQpDZl9fIaaFRgyZ/bkY+UQ=
github.com/loft-sh/utils v0.0.25 h1:JbbRJfXO1Rd34fQcaoDSmwyPBEzsrLwBSR21C90hHuk=
Expand Down
7 changes: 7 additions & 0 deletions pkg/constants/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package constants

import "errors"

var (
ErrOnlyInPro = errors.New("this command is only available in vcluster pro")
)
21 changes: 15 additions & 6 deletions pkg/pro/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"path/filepath"
"time"

"github.com/loft-sh/loftctl/v3/pkg/client"
"github.com/loft-sh/vcluster/pkg/util/cliconfig"
homedir "github.com/mitchellh/go-homedir"
)

const (
VclusterProFolder = "pro"

LoftctlConfigFileName = "creds.json"
)

var (
Expand All @@ -25,8 +26,6 @@ var (

// CLIConfig is the config of the CLI
type CLIConfig struct {
*client.Config `json:",inline"`

LatestVersion string `json:"latestVersion,omitempty"`
LatestCheckAt time.Time `json:"latestCheck,omitempty"`
}
Expand All @@ -36,8 +35,18 @@ func getDefaultCLIConfig() *CLIConfig {
return &CLIConfig{}
}

// GetLoftConfigFilePath returns the path to the loft config file
func GetLoftConfigFilePath() (string, error) {
home, err := homedir.Dir()
if err != nil {
return "", fmt.Errorf("failed to open vcluster pro configuration file from, unable to detect $HOME directory, falling back to default configuration, following error occurred: %w", err)
}

return filepath.Join(home, cliconfig.VclusterFolder, VclusterProFolder, LoftctlConfigFileName), nil
}

// getConfigFilePath returns the path to the config file
func GetConfigFilePath() (string, error) {
func getConfigFilePath() (string, error) {
home, err := homedir.Dir()
if err != nil {
return "", fmt.Errorf("failed to open vcluster pro configuration file from, unable to detect $HOME directory, falling back to default configuration, following error occurred: %w", err)
Expand All @@ -48,7 +57,7 @@ func GetConfigFilePath() (string, error) {

// GetConfig returns the config from the config file
func GetConfig() (*CLIConfig, error) {
path, err := GetConfigFilePath()
path, err := getConfigFilePath()
if err != nil {
return getDefaultCLIConfig(), fmt.Errorf("failed to get vcluster pro configuration file path: %w", err)
}
Expand Down Expand Up @@ -80,7 +89,7 @@ func GetConfig() (*CLIConfig, error) {

// WriteConfig writes the given config to the config file
func WriteConfig(c *CLIConfig) error {
path, err := GetConfigFilePath()
path, err := getConfigFilePath()
if err != nil {
return fmt.Errorf("failed to get vcluster configuration file path: %w", err)
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions vendor/github.com/loft-sh/api/v3/pkg/product/strings.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6904aed

Please sign in to comment.