-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(cli): move rebuild command from loftctl into devpod cli
- Loading branch information
1 parent
0805daf
commit 600477e
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |