-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathroot.go
More file actions
257 lines (221 loc) · 6.8 KB
/
root.go
File metadata and controls
257 lines (221 loc) · 6.8 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright © 2016 Dropbox, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"golang.org/x/oauth2"
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox"
"github.com/dropbox/dropbox-sdk-go-unofficial/v6/dropbox/files"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)
const (
configFileName = "auth.json"
tokenPersonal = "personal"
tokenTeamAccess = "teamAccess"
tokenTeamManage = "teamManage"
)
func getEnv(key, fallback string) string {
value, exists := os.LookupEnv(key)
if !exists {
value = fallback
}
return value
}
var (
personalAppKey = "mvhz183vwqibe7q"
personalAppSecret = "q0kquhzgetjwcz1"
teamAccessAppKey = "zud1va492pnehkc"
teamAccessAppSecret = "p3ginm1gy0kmj54"
teamManageAppKey = "xxe04eai4wmlitv"
teamManageAppSecret = "t8ms714yun7nu5s"
)
// TokenMap maps domains to a map of commands to tokens.
// For each domain, we want to save different tokens depending on the
// command type: personal, team access and team manage
type TokenMap map[string]map[string]oauth2.Token
var config dropbox.Config
func oauthConfig(tokenType string, domain string) *oauth2.Config {
var appKey, appSecret string
switch tokenType {
case "personal":
appKey, appSecret = personalAppKey, personalAppSecret
case "teamAccess":
appKey, appSecret = teamAccessAppKey, teamAccessAppSecret
case "teamManage":
appKey, appSecret = teamManageAppKey, teamManageAppSecret
}
return &oauth2.Config{
ClientID: appKey,
ClientSecret: appSecret,
Endpoint: dropbox.OAuthEndpoint(domain),
}
}
func validatePath(p string) (path string, err error) {
path = p
if !strings.HasPrefix(path, "/") {
path = fmt.Sprintf("/%s", path)
}
path = strings.TrimSuffix(path, "/")
return
}
func makeRelocationArg(s string, d string) (arg *files.RelocationArg, err error) {
src, err := validatePath(s)
if err != nil {
return
}
dst, err := validatePath(d)
if err != nil {
return
}
arg = files.NewRelocationArg(src, dst)
return
}
func readTokens(filePath string) (TokenMap, error) {
b, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
var tokens TokenMap
if json.Unmarshal(b, &tokens) != nil {
return nil, err
}
return tokens, nil
}
func writeTokens(filePath string, tokens TokenMap) {
// Check if file exists
if _, err := os.Stat(filePath); os.IsNotExist(err) {
// Doesn't exist; lets create it
err = os.MkdirAll(filepath.Dir(filePath), 0700)
if err != nil {
return
}
}
// At this point, file must exist. Lets (over)write it.
b, err := json.Marshal(tokens)
if err != nil {
return
}
if err = ioutil.WriteFile(filePath, b, 0600); err != nil {
return
}
}
func tokenType(cmd *cobra.Command) string {
if cmd.Parent().Name() == "team" {
return tokenTeamManage
}
if asMember, _ := cmd.Flags().GetString("as-member"); asMember != "" {
return tokenTeamAccess
}
return tokenPersonal
}
func initDbx(cmd *cobra.Command, args []string) (err error) {
verbose, _ := cmd.Flags().GetBool("verbose")
asMember, _ := cmd.Flags().GetString("as-member")
domain, _ := cmd.Flags().GetString("domain")
dir, err := homedir.Dir()
if err != nil {
return
}
filePath := path.Join(dir, ".config", "dbxcli", configFileName)
tokType := tokenType(cmd)
conf := oauthConfig(tokType, domain)
tokenMap, err := readTokens(filePath)
if tokenMap == nil {
tokenMap = make(TokenMap)
}
if tokenMap[domain] == nil {
tokenMap[domain] = make(map[string]oauth2.Token)
}
tokens := tokenMap[domain]
if err != nil || tokens[tokType].AccessToken == "" {
fmt.Printf("1. Go to %v\n", conf.AuthCodeURL("state", oauth2.SetAuthURLParam("token_access_type", "offline")))
fmt.Printf("2. Click \"Allow\" (you might have to log in first).\n")
fmt.Printf("3. Copy the authorization code.\n")
fmt.Printf("Enter the authorization code here: ")
var code string
if _, err = fmt.Scan(&code); err != nil {
return
}
var token *oauth2.Token
ctx := context.Background()
token, err = conf.Exchange(ctx, code)
if err != nil {
return
}
tokens[tokType] = *token
writeTokens(filePath, tokenMap)
}
ctx := context.Background()
tmpToken := tokens[tokType]
refreshedToken, err := conf.TokenSource(ctx, &tmpToken).Token()
if err != nil {
return err
}
if refreshedToken.AccessToken != tmpToken.AccessToken || refreshedToken.Expiry != tmpToken.Expiry {
tokens[tokType] = *refreshedToken
writeTokens(filePath, tokenMap)
}
logLevel := dropbox.LogOff
if verbose {
logLevel = dropbox.LogInfo
}
config = dropbox.Config{
Token: tokens[tokType].AccessToken,
LogLevel: logLevel,
Logger: nil,
AsMemberID: asMember,
Domain: domain,
Client: nil,
HeaderGenerator: nil,
URLGenerator: nil,
}
return
}
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "dbxcli",
Short: "A command line tool for Dropbox users and team admins",
Long: `Use dbxcli to quickly interact with your Dropbox, upload/download files,
manage your team and more. It is easy, scriptable and works on all platforms!`,
SilenceUsage: true,
PersistentPreRunE: initDbx,
}
// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := RootCmd.Execute(); err != nil {
os.Exit(-1)
}
}
func init() {
RootCmd.PersistentFlags().BoolP("verbose", "v", false, "Enable verbose logging")
RootCmd.PersistentFlags().String("as-member", "", "Member ID to perform action as")
// This flag should only be used for testing. Marked hidden so it doesn't clutter usage etc.
RootCmd.PersistentFlags().String("domain", "", "Override default Dropbox domain, useful for testing")
RootCmd.PersistentFlags().MarkHidden("domain")
personalAppKey = getEnv("DROPBOX_PERSONAL_APP_KEY", personalAppKey)
personalAppSecret = getEnv("DROPBOX_PERSONAL_APP_SECRET", personalAppSecret)
teamAccessAppKey = getEnv("DROPBOX_TEAM_APP_KEY", teamAccessAppKey)
teamAccessAppSecret = getEnv("DROPBOX_TEAM_APP_SECRET", teamAccessAppSecret)
teamManageAppKey = getEnv("DROPBOX_MANAGE_APP_KEY", teamManageAppKey)
teamManageAppSecret = getEnv("DROPBOX_MANAGE_APP_SECRET", teamAccessAppSecret)
}