Skip to content

Commit 2f4fabf

Browse files
committed
version 1
0 parents  commit 2f4fabf

File tree

10 files changed

+911
-0
lines changed

10 files changed

+911
-0
lines changed

Diff for: .gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Ignore bin folder and drive binary
2+
bin/
3+
drive
4+
5+
# vim files
6+
.*.sw[a-z]
7+
*.un~
8+
Session.vim
9+
.netrwhist

Diff for: LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License
2+
3+
Copyright (c) 2013 Petter Rasmussen
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
22+

Diff for: auth/auth.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package auth
2+
3+
import (
4+
"net/http"
5+
"fmt"
6+
"code.google.com/p/goauth2/oauth"
7+
"../util"
8+
)
9+
10+
// Get auth code from user
11+
func promptUserForAuthCode(config *oauth.Config) string {
12+
authUrl := config.AuthCodeURL("state")
13+
fmt.Println("Go to the following link in your browser:")
14+
fmt.Printf("%v\n\n", authUrl)
15+
return util.Prompt("Enter verification code: ")
16+
}
17+
18+
// Returns true if we have a valid cached token
19+
func hasValidToken(cacheFile oauth.CacheFile, transport *oauth.Transport) bool {
20+
// Check if we have a cached token
21+
token, err := cacheFile.Token()
22+
if err != nil {
23+
return false
24+
}
25+
26+
// Refresh token if its expired
27+
if token.Expired() {
28+
transport.Token = token
29+
err = transport.Refresh()
30+
if err != nil {
31+
fmt.Println(err)
32+
return false
33+
}
34+
}
35+
return true
36+
}
37+
38+
func GetOauth2Client(clientId, clientSecret, cachePath string) (*http.Client, error) {
39+
cacheFile := oauth.CacheFile(cachePath)
40+
41+
config := &oauth.Config{
42+
ClientId: clientId,
43+
ClientSecret: clientSecret,
44+
Scope: "https://www.googleapis.com/auth/drive",
45+
RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
46+
AuthURL: "https://accounts.google.com/o/oauth2/auth",
47+
TokenURL: "https://accounts.google.com/o/oauth2/token",
48+
TokenCache: cacheFile,
49+
}
50+
51+
transport := &oauth.Transport{
52+
Config: config,
53+
Transport: http.DefaultTransport,
54+
}
55+
56+
// Return client if we have a valid token
57+
if hasValidToken(cacheFile, transport) {
58+
return transport.Client(), nil
59+
}
60+
61+
// Get auth code from user and request a new token
62+
code := promptUserForAuthCode(config)
63+
_, err := transport.Exchange(code)
64+
if err != nil {
65+
return nil, err
66+
}
67+
return transport.Client(), nil
68+
}

Diff for: build-all.sh

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
3+
if [ -z "$1" ]; then
4+
echo "Usage: $0 <app>"
5+
exit 1
6+
fi
7+
8+
PLATFORMS="darwin/386 darwin/amd64 freebsd/386 freebsd/amd64 linux/386 linux/amd64 linux/arm windows/386 windows/amd64"
9+
APP_NAME=$1
10+
11+
# Remove old binaries
12+
rm bin/*
13+
14+
# Load crosscompile environment
15+
source /Users/pii/scripts/golang-crosscompile/crosscompile.bash
16+
17+
# Build binary for each platform in parallel
18+
for PLATFORM in $PLATFORMS; do
19+
GOOS=${PLATFORM%/*}
20+
GOARCH=${PLATFORM#*/}
21+
BIN_NAME="${APP_NAME}-$GOOS-$GOARCH"
22+
23+
if [ $GOOS == "windows" ]; then
24+
BIN_NAME="${BIN_NAME}.exe"
25+
fi
26+
27+
BUILD_CMD="go-${GOOS}-${GOARCH} build -o bin/${BIN_NAME} $APP_NAME.go"
28+
29+
echo "Building $APP_NAME for ${GOOS}/${GOARCH}..."
30+
$BUILD_CMD &
31+
done
32+
33+
# Wait for builds to complete
34+
for job in $(jobs -p); do
35+
wait $job
36+
done
37+
38+
echo "All done"

0 commit comments

Comments
 (0)