Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/cmd/snapshot.go
2498 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"context"
9
"errors"
10
"fmt"
11
"time"
12
13
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
14
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
15
"github.com/sourcegraph/jsonrpc2"
16
"github.com/spf13/cobra"
17
)
18
19
const (
20
ErrorCodeSnapshotNotFound = 404
21
ErrorCodeSnapshotError = 630
22
)
23
24
// snapshotCmd represents the snapshotCmd command
25
var snapshotCmd = &cobra.Command{
26
Use: "snapshot",
27
Short: "Take a snapshot of the current workspace",
28
Args: cobra.ArbitraryArgs,
29
RunE: func(cmd *cobra.Command, args []string) error {
30
ctx, cancel := context.WithCancel(cmd.Context())
31
defer cancel()
32
33
wsInfo, err := gitpod.GetWSInfo(ctx)
34
if err != nil {
35
return err
36
}
37
client, err := gitpod.ConnectToServer(ctx, wsInfo, []string{
38
"function:takeSnapshot",
39
"function:waitForSnapshot",
40
"resource:workspace::" + wsInfo.WorkspaceId + "::get/update",
41
})
42
if err != nil {
43
return err
44
}
45
defer client.Close()
46
snapshotId, err := client.TakeSnapshot(ctx, &protocol.TakeSnapshotOptions{
47
WorkspaceID: wsInfo.WorkspaceId,
48
DontWait: true,
49
})
50
if err != nil {
51
return err
52
}
53
for ctx.Err() == nil {
54
err = client.WaitForSnapshot(ctx, snapshotId)
55
if err != nil {
56
var responseErr *jsonrpc2.Error
57
if errors.As(err, &responseErr) && (responseErr.Code == ErrorCodeSnapshotNotFound || responseErr.Code == ErrorCodeSnapshotError) {
58
return err
59
}
60
time.Sleep(time.Second * 3)
61
} else {
62
break
63
}
64
}
65
url := fmt.Sprintf("%s/#snapshot/%s", wsInfo.GitpodHost, snapshotId)
66
fmt.Println(url)
67
return nil
68
},
69
}
70
71
func init() {
72
rootCmd.AddCommand(snapshotCmd)
73
}
74
75