-
Notifications
You must be signed in to change notification settings - Fork 386
/
web_cmds.go
89 lines (74 loc) · 2.17 KB
/
web_cmds.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
package subcmd
import (
"fmt"
"net/url"
"github.com/earthly/earthly/cloud"
"github.com/earthly/earthly/cmd/earthly/helper"
"github.com/pkg/browser"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)
type Web struct {
cli CLI
loginProvider string
}
func NewWeb(cli CLI) *Web {
return &Web{
cli: cli,
}
}
func (a *Web) Cmds() []*cli.Command {
return []*cli.Command{
{
Name: "web",
Usage: "*beta* Access the web UI via your default browser and print the url",
UsageText: "earthly web (--provider=github)",
Description: `*beta* Prints a url for entering the CI application and attempts to open your default browser with that url.
If the provider argument is given the CI application will automatically begin an OAuth flow with the given provider.
If you are logged into the CLI the url will contain a token used to link your OAuth credentials to your Earthly user.`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "provider",
EnvVars: []string{"EARTHLY_LOGIN_PROVIDER"},
Usage: "The provider to use when logging into the web ui",
Required: false,
Destination: &a.loginProvider,
},
},
Action: a.action,
},
}
}
func (a *Web) action(cliCtx *cli.Context) error {
urlToOpen, err := url.Parse(fmt.Sprintf("%s/login", a.cli.CIHost()))
if err != nil {
return errors.Wrapf(err, "failed to parse url")
}
query := urlToOpen.Query()
if a.loginProvider != "" {
query.Set("provider", a.loginProvider)
}
client, err := helper.NewCloudClient(a.cli)
if err != nil {
return errors.Wrap(err, "failed to initialize cloud client")
}
token, err := client.GetAuthToken(cliCtx.Context)
if err != nil {
if !errors.Is(err, cloud.ErrUnauthorized) {
return errors.Wrapf(err, "failed to get auth token")
}
a.cli.Console().VerbosePrintf("failed to get token %s", err.Error())
}
if token != "" {
query.Set("token", token)
}
urlToOpen.RawQuery = query.Encode()
urlString := urlToOpen.String()
err = browser.OpenURL(urlString)
if err != nil {
err := errors.Wrapf(err, "failed to open UI in browser")
a.cli.Console().Printf(err.Error())
}
a.cli.Console().Printf("Visit UI at %s", urlString)
return nil
}