Skip to content

Commit

Permalink
chore(cli): move rebuild command from loftctl into devpod cli
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbreuninger committed May 27, 2024
1 parent 0805daf commit 600477e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/pro/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func NewProCmd(flags *flags.GlobalFlags, streamLogger *log.StreamLogger) *cobra.
proCmd.AddCommand(NewDeleteCmd(globalFlags))
proCmd.AddCommand(NewImportCmd(globalFlags))
proCmd.AddCommand(NewStartCmd(globalFlags))
proCmd.AddCommand(NewRebuildCmd(globalFlags))
proCmd.AddCommand(reset.NewResetCmd(globalFlags))
proCmd.AddCommand(provider.NewProProviderCmd(globalFlags))
return proCmd
Expand Down
85 changes: 85 additions & 0 deletions cmd/pro/rebuild.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package pro

import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"

"github.com/loft-sh/devpod/cmd/pro/flags"
"github.com/loft-sh/devpod/cmd/pro/provider"
"github.com/loft-sh/devpod/pkg/loft/client"
"github.com/loft-sh/devpod/pkg/loft/remotecommand"
"github.com/loft-sh/log"
"github.com/spf13/cobra"
)

const AllWorkspaces = "all"

// RebuildCmd holds the cmd flags
type RebuildCmd struct {
*flags.GlobalFlags
Log log.Logger

Project string
}

// NewRebuildCmd creates a new command
func NewRebuildCmd(globalFlags *flags.GlobalFlags) *cobra.Command {
cmd := &RebuildCmd{
GlobalFlags: globalFlags,
Log: log.GetInstance(),
}
c := &cobra.Command{
Use: "rebuild",
Short: "Rebuild a workspace",
RunE: func(cobraCmd *cobra.Command, args []string) error {
log.Default.SetFormat(log.TextFormat)

return cmd.Run(cobraCmd.Context(), args)
},
}

c.Flags().StringVar(&cmd.Project, "project", "", "The project to use")
_ = c.MarkFlagRequired("project")

return c
}

func (cmd *RebuildCmd) Run(ctx context.Context, args []string) error {
if len(args) == 0 {
return fmt.Errorf("please provide a workspace name")
}
targetWorkspace := args[0]

baseClient, err := client.InitClientFromPath(ctx, cmd.Config)
if err != nil {
return err
}

workspace, err := provider.FindWorkspaceByName(ctx, baseClient, targetWorkspace, cmd.Project)
if err != nil {
return err
}

opts := struct {
Recreate bool `json:"recreate"`
}{Recreate: true}
rawOpts, err := json.Marshal(opts)
if err != nil {
return err
}
values := url.Values{"options": []string{string(rawOpts)}, "cliMode": []string{"true"}}
conn, err := provider.DialWorkspace(baseClient, workspace, "up", values)
if err != nil {
return err
}

_, err = remotecommand.ExecuteConn(ctx, conn, os.Stdin, os.Stdout, os.Stderr, cmd.Log.ErrorStreamOnly())
if err != nil {
return fmt.Errorf("error executing: %w", err)
}

return nil
}

0 comments on commit 600477e

Please sign in to comment.