-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
rebuild.go
78 lines (63 loc) · 2.1 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
package codespace
import (
"context"
"fmt"
"github.com/cli/cli/v2/internal/codespaces"
"github.com/cli/cli/v2/internal/codespaces/api"
"github.com/cli/cli/v2/internal/codespaces/portforwarder"
"github.com/cli/cli/v2/internal/codespaces/rpc"
"github.com/spf13/cobra"
)
func newRebuildCmd(app *App) *cobra.Command {
var (
selector *CodespaceSelector
fullRebuild bool
)
rebuildCmd := &cobra.Command{
Use: "rebuild",
Short: "Rebuild a codespace",
Long: `Rebuilding recreates your codespace. Your code and any current changes will be
preserved. Your codespace will be rebuilt using your working directory's
dev container. A full rebuild also removes cached Docker images.`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return app.Rebuild(cmd.Context(), selector, fullRebuild)
},
}
selector = AddCodespaceSelector(rebuildCmd, app.apiClient)
rebuildCmd.Flags().BoolVar(&fullRebuild, "full", false, "perform a full rebuild")
return rebuildCmd
}
func (a *App) Rebuild(ctx context.Context, selector *CodespaceSelector, full bool) (err error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
codespace, err := selector.Select(ctx)
if err != nil {
return err
}
// There's no need to rebuild again because users can't modify their codespace while it rebuilds
if codespace.State == api.CodespaceStateRebuilding {
fmt.Fprintf(a.io.Out, "%s is already rebuilding\n", codespace.Name)
return nil
}
codespaceConnection, err := codespaces.GetCodespaceConnection(ctx, a, a.apiClient, codespace)
if err != nil {
return fmt.Errorf("error connecting to codespace: %w", err)
}
fwd, err := portforwarder.NewPortForwarder(ctx, codespaceConnection)
if err != nil {
return fmt.Errorf("failed to create port forwarder: %w", err)
}
defer safeClose(fwd, &err)
invoker, err := rpc.CreateInvoker(ctx, fwd)
if err != nil {
return err
}
defer safeClose(invoker, &err)
err = invoker.RebuildContainer(ctx, full)
if err != nil {
return fmt.Errorf("rebuilding codespace via session: %w", err)
}
fmt.Fprintf(a.io.Out, "%s is rebuilding\n", codespace.Name)
return nil
}