Skip to content

Commit

Permalink
chore(cli): move loftctl commands into devpod cli
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbreuninger committed May 22, 2024
1 parent 05dd69c commit 5361324
Show file tree
Hide file tree
Showing 3,690 changed files with 611,084 additions and 47,807 deletions.
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

0 comments on commit 5361324

Please sign in to comment.