-
Notifications
You must be signed in to change notification settings - Fork 786
/
get_preview.go
113 lines (98 loc) · 2.58 KB
/
get_preview.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cmd
import (
"fmt"
"io"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// GetPreviewOptions containers the CLI options
type GetPreviewOptions struct {
GetEnvOptions
Current bool
}
var (
getPreviewLong = templates.LongDesc(`
Display one or more environments.
`)
getPreviewExample = templates.Examples(`
# List all preview environments
jx get previews
# View the current preview environment URL
# inside a CI pipeline
jx get preview --current
`)
)
// NewCmdGetPreview creates the new command for: jx get env
func NewCmdGetPreview(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &GetPreviewOptions{
GetEnvOptions: GetEnvOptions{
GetOptions: GetOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
},
}
cmd := &cobra.Command{
Use: "previews",
Short: "Display one or more Preview Environments",
Aliases: []string{"preview"},
Long: getPreviewLong,
Example: getPreviewExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().BoolVarP(&options.Current, "current", "c", false, "Output the URL of the current Preview application the current pipeline just deployed")
options.addGetFlags(cmd)
return cmd
}
// Run implements this command
func (o *GetPreviewOptions) Run() error {
if o.Current {
return o.CurrentPreviewUrl()
}
o.PreviewOnly = true
return o.GetEnvOptions.Run()
}
func (o *GetPreviewOptions) CurrentPreviewUrl() error {
pipeline := o.getJobName()
if pipeline == "" {
return fmt.Errorf("No $JOB_NAME defined for the current pipeline job to use")
}
name := kube.ToValidName(pipeline)
client, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return err
}
apisClient, err := o.CreateApiExtensionsClient()
if err != nil {
return err
}
err = kube.RegisterEnvironmentCRD(apisClient)
if err != nil {
return err
}
envList, err := client.JenkinsV1().Environments(ns).List(metav1.ListOptions{})
if err != nil {
return err
}
for _, env := range envList.Items {
if env.Spec.Kind == v1.EnvironmentKindTypePreview && env.Name == name {
log.Info(env.Spec.PreviewGitSpec.ApplicationURL)
return nil
}
}
return fmt.Errorf("No Preview for name: %s", name)
}