/
rebuild.go
91 lines (76 loc) · 2.13 KB
/
rebuild.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package devpod
import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"github.com/loft-sh/loftctl/v3/cmd/loftctl/flags"
"github.com/loft-sh/loftctl/v3/pkg/client"
devpodpkg "github.com/loft-sh/loftctl/v3/pkg/devpod"
"github.com/loft-sh/loftctl/v3/pkg/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",
Long: `
#######################################################
################# loft devpod rebuild ##################
#######################################################
`,
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.NewClientFromPath(cmd.Config)
if err != nil {
return err
}
workspace, err := devpodpkg.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 := devpodpkg.DialWorkspace(baseClient, workspace, "up", values)
if err != nil {
return err
}
// TODO: needs git credentials and configure logging
_, 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
}