MCPcopy
hub / github.com/perkeep/perkeep / RunCommand

Method RunCommand

cmd/pk/googinit.go:57–113  ·  view source on GitHub ↗
(args []string)

Source from the content-addressed store, hash-verified

55}
56
57func (c *googinitCmd) RunCommand(args []string) error {
58 var (
59 err error
60 clientId string
61 clientSecret string
62 oauthConfig *oauth2.Config
63 )
64
65 if c.storageType != "drive" && c.storageType != "cloud" {
66 return cmdmain.UsageError("Invalid storage type: must be drive for Google Drive or cloud for Google Cloud Storage.")
67 }
68
69 clientId, clientSecret = getClientInfo()
70
71 switch c.storageType {
72 case "drive":
73 oauthConfig = &oauth2.Config{
74 Scopes: []string{drive.Scope},
75 Endpoint: google.Endpoint,
76 ClientID: clientId,
77 ClientSecret: clientSecret,
78 RedirectURL: oauthutil.TitleBarRedirectURL,
79 }
80 case "cloud":
81 oauthConfig = &oauth2.Config{
82 Scopes: []string{storage.ScopeReadWrite},
83 Endpoint: google.Endpoint,
84 ClientID: clientId,
85 ClientSecret: clientSecret,
86 RedirectURL: oauthutil.TitleBarRedirectURL,
87 }
88 }
89
90 token, err := oauth2.ReuseTokenSource(nil, &oauthutil.TokenSource{
91 Config: oauthConfig,
92 AuthCode: func() string {
93 fmt.Fprintf(cmdmain.Stdout, "Get auth code from:\n\n")
94 fmt.Fprintf(cmdmain.Stdout, "%v\n\n", oauthConfig.AuthCodeURL("", oauth2.AccessTypeOffline, oauth2.ApprovalForce))
95 return prompt("Enter auth code:")
96 },
97 }).Token()
98 if err != nil {
99 return fmt.Errorf("could not acquire token: %v", err)
100 }
101
102 fmt.Fprintf(cmdmain.Stdout, "\nYour Google auth object:\n\n")
103 enc := json.NewEncoder(cmdmain.Stdout)
104 authObj := map[string]string{
105 "client_id": clientId,
106 "client_secret": clientSecret,
107 "refresh_token": token.RefreshToken,
108 }
109 enc.Encode(authObj)
110 fmt.Fprint(cmdmain.Stdout, "\n\nFor server-config.json, your 'googlecloudstorage' value (update with your bucket name and path):\n\n")
111 fmt.Fprintf(cmdmain.Stdout, "%s:%s:%s:bucketName[/optional/dir]\n", clientId, clientSecret, token.RefreshToken)
112 return nil
113}
114

Callers

nothing calls this directly

Calls 3

UsageErrorTypeAlias · 0.92
getClientInfoFunction · 0.85
promptFunction · 0.85

Tested by

no test coverage detected