Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/preview/previewctl/cmd/get.go
2500 views
1
// Copyright (c) 2022 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
"fmt"
10
"time"
11
12
"github.com/sirupsen/logrus"
13
"github.com/spf13/cobra"
14
"k8s.io/client-go/util/homedir"
15
16
"github.com/gitpod-io/gitpod/previewctl/pkg/preview"
17
)
18
19
func newGetCmd(logger *logrus.Logger) *cobra.Command {
20
cmd := &cobra.Command{
21
Use: "get",
22
Short: "",
23
RunE: func(cmd *cobra.Command, args []string) error {
24
return nil
25
},
26
}
27
28
cmd.AddCommand(
29
newGetNameSubCmd(),
30
newGetActiveCmd(logger),
31
newGetUrlSubCmd(),
32
)
33
34
return cmd
35
}
36
37
func newGetNameSubCmd() *cobra.Command {
38
cmd := &cobra.Command{
39
Use: "name",
40
Short: "",
41
RunE: func(cmd *cobra.Command, args []string) error {
42
previewName, err := preview.GetName(branch)
43
if err != nil {
44
return err
45
}
46
47
fmt.Println(previewName)
48
49
return nil
50
},
51
}
52
53
return cmd
54
}
55
56
func newGetUrlSubCmd() *cobra.Command {
57
cmd := &cobra.Command{
58
Use: "url",
59
Short: "",
60
RunE: func(cmd *cobra.Command, args []string) error {
61
previewName, err := preview.GetName(branch)
62
if err != nil {
63
return err
64
}
65
66
previewUrl := fmt.Sprintf("https://%s.preview.gitpod-dev.com", previewName)
67
fmt.Println(previewUrl)
68
69
return nil
70
},
71
}
72
73
return cmd
74
}
75
76
func newGetActiveCmd(logger *logrus.Logger) *cobra.Command {
77
ctx := context.Background()
78
79
cmd := &cobra.Command{
80
Use: "active",
81
Short: "Checks if the preview environment is active",
82
RunE: func(cmd *cobra.Command, args []string) error {
83
p, err := preview.New(branch, logger)
84
if err != nil {
85
return err
86
}
87
88
logger.WithFields(logrus.Fields{
89
"preview": p.GetName(),
90
}).Info("Installing context")
91
92
err = p.InstallContext(ctx, &preview.InstallCtxOpts{
93
Retry: true,
94
RetryTimeout: 1 * time.Minute,
95
KubeSavePath: getKubeConfigPath(),
96
SSHPrivateKeyPath: fmt.Sprintf("%s/.ssh/vm_id_rsa", homedir.HomeDir()),
97
})
98
99
if err != nil {
100
return err
101
}
102
103
status, err := p.GetStatus(ctx)
104
if err != nil {
105
return err
106
}
107
108
fmt.Printf("Preview [%s] is [%t]. Reason: [%s]", p.GetName(), status.Active, status.Reason)
109
110
return nil
111
},
112
}
113
114
return cmd
115
}
116
117