-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathcrawler.go
More file actions
83 lines (70 loc) · 2.2 KB
/
crawler.go
File metadata and controls
83 lines (70 loc) · 2.2 KB
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
package crawler
import (
"fmt"
"github.com/algolia/cli/api/dashboard"
"github.com/algolia/cli/pkg/auth"
"github.com/algolia/cli/pkg/cmdutil"
"github.com/algolia/cli/pkg/config"
"github.com/algolia/cli/pkg/iostreams"
"github.com/algolia/cli/pkg/validators"
"github.com/spf13/cobra"
)
type CrawlerOptions struct {
IO *iostreams.IOStreams
config config.IConfig
OAuthClientID func() string
NewDashboardClient func(clientID string) *dashboard.Client
GetValidToken func(client *dashboard.Client) (string, error)
}
func NewCrawlerCmd(f *cmdutil.Factory) *cobra.Command {
opts := &CrawlerOptions{
IO: f.IOStreams,
config: f.Config,
OAuthClientID: auth.OAuthClientID,
NewDashboardClient: func(clientID string) *dashboard.Client {
return dashboard.NewClient(clientID)
},
GetValidToken: auth.GetValidToken,
}
cmd := &cobra.Command{
Use: "crawler",
Short: "Load crawler auth details for the current profile",
Args: validators.NoArgs(),
RunE: func(cmd *cobra.Command, args []string) error {
return runCrawlerCmd(opts)
},
}
return cmd
}
func runCrawlerCmd(opts *CrawlerOptions) error {
cs := opts.IO.ColorScheme()
dashboardClient := opts.NewDashboardClient(opts.OAuthClientID())
accessToken, err := opts.GetValidToken(dashboardClient)
if err != nil {
return err
}
opts.IO.StartProgressIndicatorWithLabel("Fetching crawler information")
crawlerUserData, err := dashboardClient.GetCrawlerUser(accessToken)
opts.IO.StopProgressIndicator()
if err != nil {
return err
}
currentProfileName := opts.config.Profile().Name
if currentProfileName == "" {
defaultProfile := opts.config.Default()
if defaultProfile != nil {
currentProfileName = defaultProfile.Name
opts.config.Profile().Name = currentProfileName
}
}
if currentProfileName == "" {
return fmt.Errorf("no profile selected and no default profile configured")
}
if err = opts.config.SetCrawlerAuth(currentProfileName, crawlerUserData.ID, crawlerUserData.APIKey); err != nil {
return err
}
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.Out, "%s Crawler API auth credentials configured for profile: %s\n", cs.SuccessIcon(), currentProfileName)
}
return nil
}