Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cli): move loftctl commands into devpod cli #1075

Merged
merged 1 commit into from
May 23, 2024
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
8 changes: 4 additions & 4 deletions cmd/pro/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"os"

"github.com/loft-sh/devpod/cmd/flags"
proflags "github.com/loft-sh/devpod/cmd/pro/flags"
providercmd "github.com/loft-sh/devpod/cmd/provider"
"github.com/loft-sh/devpod/pkg/config"
provider2 "github.com/loft-sh/devpod/pkg/provider"
Expand All @@ -16,19 +16,19 @@ import (

// DeleteCmd holds the delete cmd flags
type DeleteCmd struct {
*flags.GlobalFlags
*proflags.GlobalFlags

IgnoreNotFound bool
}

// NewDeleteCmd creates a new command
func NewDeleteCmd(flags *flags.GlobalFlags) *cobra.Command {
func NewDeleteCmd(flags *proflags.GlobalFlags) *cobra.Command {
cmd := &DeleteCmd{
GlobalFlags: flags,
}
deleteCmd := &cobra.Command{
Use: "delete",
Short: "Delete or logout from a Loft DevPod Pro",
Short: "Delete or logout from a DevPod Pro Instance",
RunE: func(_ *cobra.Command, args []string) error {
return cmd.Run(context.Background(), args)
},
Expand Down
23 changes: 23 additions & 0 deletions cmd/pro/flags/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package flags

import (
"github.com/loft-sh/devpod/cmd/flags"
"github.com/loft-sh/devpod/pkg/loft/client"
flag "github.com/spf13/pflag"
)

// GlobalFlags is the flags that contains the global flags
type GlobalFlags struct {
*flags.GlobalFlags

Config string
}

// SetGlobalFlags applies the global flags
func SetGlobalFlags(flags *flag.FlagSet) *GlobalFlags {
globalFlags := &GlobalFlags{}

flags.StringVar(&globalFlags.Config, "config", client.DefaultCacheConfig, "The config to use (will be created if it does not exist)")

return globalFlags
}
6 changes: 3 additions & 3 deletions cmd/pro/import_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

"github.com/loft-sh/devpod/cmd/flags"
proflags "github.com/loft-sh/devpod/cmd/pro/flags"
"github.com/loft-sh/devpod/pkg/config"
provider2 "github.com/loft-sh/devpod/pkg/provider"
"github.com/loft-sh/devpod/pkg/random"
Expand All @@ -15,7 +15,7 @@ import (
)

type ImportCmd struct {
*flags.GlobalFlags
*proflags.GlobalFlags

WorkspaceId string
WorkspaceUid string
Expand All @@ -26,7 +26,7 @@ type ImportCmd struct {
}

// NewImportCmd creates a new command
func NewImportCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
func NewImportCmd(globalFlags *proflags.GlobalFlags) *cobra.Command {
logger := log.GetInstance()
cmd := &ImportCmd{
GlobalFlags: globalFlags,
Expand Down
56 changes: 6 additions & 50 deletions cmd/pro/list.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package pro

import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"time"

"github.com/loft-sh/devpod/cmd/flags"
"github.com/loft-sh/devpod/pkg/binaries"
proflags "github.com/loft-sh/devpod/cmd/pro/flags"
"github.com/loft-sh/devpod/pkg/config"
"github.com/loft-sh/devpod/pkg/provider"
"github.com/loft-sh/devpod/pkg/workspace"
Expand All @@ -24,21 +18,21 @@ import (

// ListCmd holds the list cmd flags
type ListCmd struct {
flags.GlobalFlags
proflags.GlobalFlags

Output string
Login bool
}

// NewListCmd creates a new command
func NewListCmd(flags *flags.GlobalFlags) *cobra.Command {
func NewListCmd(flags *proflags.GlobalFlags) *cobra.Command {
cmd := &ListCmd{
GlobalFlags: *flags,
}
listCmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List available pro instances",
Short: "List available DevPod Pro instances",
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, args []string) error {
return cmd.Run(context.Background())
Expand Down Expand Up @@ -126,46 +120,8 @@ type proTableEntry struct {
}

func checkLogin(ctx context.Context, devPodConfig *config.Config, proInstance *provider.ProInstance) error {
providerConfig, err := provider.LoadProviderConfig(devPodConfig.DefaultContext, proInstance.Provider)
if err != nil {
return err
}

providerBinaries, err := binaries.GetBinaries(devPodConfig.DefaultContext, providerConfig)
if err != nil {
return fmt.Errorf("get provider binaries: %w", err)
} else if providerBinaries[LOFT_PROVIDER_BINARY] == "" {
return fmt.Errorf("provider is missing %s binary", LOFT_PROVIDER_BINARY)
}

providerDir, err := provider.GetProviderDir(devPodConfig.DefaultContext, providerConfig.Name)
if err != nil {
return err
}

args := []string{
"login",
"--log-output=raw",
}

extraEnv := []string{
"LOFT_SKIP_VERSION_CHECK=true",
"LOFT_CONFIG=" + filepath.Join(providerDir, "loft-config.json"),
}

stdout := &bytes.Buffer{}

// start the command
loginCmd := exec.CommandContext(ctx, providerBinaries[LOFT_PROVIDER_BINARY], args...)
loginCmd.Env = os.Environ()
loginCmd.Env = append(loginCmd.Env, extraEnv...)
loginCmd.Stdout = stdout
err = loginCmd.Run()
if err != nil {
return fmt.Errorf("run login command: %w", err)
}

if stdout.Len() > 0 && strings.Contains(stdout.String(), "Not logged in") {
// for every pro instance, check auth status by calling login
if err := login(ctx, devPodConfig, proInstance.Host, proInstance.Provider, "", log.Default); err != nil {
return fmt.Errorf("not logged into %s", proInstance.Host)
}

Expand Down
Loading
Loading