-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
cmd_preview.go
102 lines (90 loc) · 3.42 KB
/
cmd_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
package cli
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/telepresenceio/telepresence/rpc/v2/connector"
"github.com/telepresenceio/telepresence/rpc/v2/daemon"
"github.com/telepresenceio/telepresence/rpc/v2/manager"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/cliutil"
)
// addPreviewFlags mutates 'flags', adding flags to it such that the flags set the appropriate
// fields in the given 'spec'. If 'prefix' is given, long-flag names are prefixed with it.
func addPreviewFlags(prefix string, flags *pflag.FlagSet, spec *manager.PreviewSpec) {
flags.BoolVarP(&spec.DisplayBanner, prefix+"banner", "b", true, "Display banner on preview page")
}
func previewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "preview",
Args: OnlySubcommands,
Short: "Create or remove preview domains for existing intercepts",
RunE: RunSubcommands,
}
var createSpec manager.PreviewSpec
createCmd := &cobra.Command{
Use: "create [flags] <intercept_name>",
Args: cobra.ExactArgs(1),
Short: "Create a preview domain for an existing intercept",
RunE: func(cmd *cobra.Command, args []string) error {
if _, err := cliutil.EnsureLoggedIn(cmd.Context(), ""); err != nil {
return err
}
return withConnector(cmd, true, func(ctx context.Context, _ connector.ConnectorClient, connInfo *connector.ConnectInfo, _ daemon.DaemonClient) error {
return cliutil.WithManager(ctx, func(ctx context.Context, managerClient manager.ManagerClient) error {
if createSpec.Ingress == nil {
request := manager.GetInterceptRequest{Session: connInfo.SessionInfo, Name: args[0]}
// Throws rpc "not found" error if intercept has not yet been created
interceptInfo, err := managerClient.GetIntercept(ctx, &request)
if err != nil {
return err
}
ingress, err := selectIngress(ctx, cmd.InOrStdin(), cmd.OutOrStdout(), connInfo, interceptInfo.Spec.Agent, interceptInfo.Spec.Namespace)
if err != nil {
return err
}
createSpec.Ingress = ingress
}
intercept, err := managerClient.UpdateIntercept(ctx, &manager.UpdateInterceptRequest{
Session: connInfo.SessionInfo,
Name: args[0],
PreviewDomainAction: &manager.UpdateInterceptRequest_AddPreviewDomain{
AddPreviewDomain: &createSpec,
},
})
if err != nil {
return err
}
fmt.Println(DescribeIntercept(intercept, nil, false))
return nil
})
})
},
}
addPreviewFlags("", createCmd.Flags(), &createSpec)
removeCmd := &cobra.Command{
Use: "remove <intercept_name>",
Args: cobra.ExactArgs(1),
Short: "Remove a preview domain from an intercept",
RunE: func(cmd *cobra.Command, args []string) error {
return withConnector(cmd, true, func(ctx context.Context, _ connector.ConnectorClient, connInfo *connector.ConnectInfo, _ daemon.DaemonClient) error {
return cliutil.WithManager(ctx, func(ctx context.Context, managerClient manager.ManagerClient) error {
intercept, err := managerClient.UpdateIntercept(ctx, &manager.UpdateInterceptRequest{
Session: connInfo.SessionInfo,
Name: args[0],
PreviewDomainAction: &manager.UpdateInterceptRequest_RemovePreviewDomain{
RemovePreviewDomain: true,
},
})
if err != nil {
return err
}
fmt.Println(DescribeIntercept(intercept, nil, false))
return nil
})
})
},
}
cmd.AddCommand(createCmd, removeCmd)
return cmd
}