Skip to content

Commit

Permalink
version 1
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Jan 1, 2013
0 parents commit 2f4fabf
Show file tree
Hide file tree
Showing 10 changed files with 911 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore bin folder and drive binary
bin/
drive

# vim files
.*.sw[a-z]
*.un~
Session.vim
.netrwhist
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License

Copyright (c) 2013 Petter Rasmussen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

68 changes: 68 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package auth

import (
"net/http"
"fmt"
"code.google.com/p/goauth2/oauth"
"../util"
)

// Get auth code from user
func promptUserForAuthCode(config *oauth.Config) string {
authUrl := config.AuthCodeURL("state")
fmt.Println("Go to the following link in your browser:")
fmt.Printf("%v\n\n", authUrl)
return util.Prompt("Enter verification code: ")
}

// Returns true if we have a valid cached token
func hasValidToken(cacheFile oauth.CacheFile, transport *oauth.Transport) bool {
// Check if we have a cached token
token, err := cacheFile.Token()
if err != nil {
return false
}

// Refresh token if its expired
if token.Expired() {
transport.Token = token
err = transport.Refresh()
if err != nil {
fmt.Println(err)
return false
}
}
return true
}

func GetOauth2Client(clientId, clientSecret, cachePath string) (*http.Client, error) {
cacheFile := oauth.CacheFile(cachePath)

config := &oauth.Config{
ClientId: clientId,
ClientSecret: clientSecret,
Scope: "https://www.googleapis.com/auth/drive",
RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
AuthURL: "https://accounts.google.com/o/oauth2/auth",
TokenURL: "https://accounts.google.com/o/oauth2/token",
TokenCache: cacheFile,
}

transport := &oauth.Transport{
Config: config,
Transport: http.DefaultTransport,
}

// Return client if we have a valid token
if hasValidToken(cacheFile, transport) {
return transport.Client(), nil
}

// Get auth code from user and request a new token
code := promptUserForAuthCode(config)
_, err := transport.Exchange(code)
if err != nil {
return nil, err
}
return transport.Client(), nil
}
38 changes: 38 additions & 0 deletions build-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

if [ -z "$1" ]; then
echo "Usage: $0 <app>"
exit 1
fi

PLATFORMS="darwin/386 darwin/amd64 freebsd/386 freebsd/amd64 linux/386 linux/amd64 linux/arm windows/386 windows/amd64"
APP_NAME=$1

# Remove old binaries
rm bin/*

# Load crosscompile environment
source /Users/pii/scripts/golang-crosscompile/crosscompile.bash

# Build binary for each platform in parallel
for PLATFORM in $PLATFORMS; do
GOOS=${PLATFORM%/*}
GOARCH=${PLATFORM#*/}
BIN_NAME="${APP_NAME}-$GOOS-$GOARCH"

if [ $GOOS == "windows" ]; then
BIN_NAME="${BIN_NAME}.exe"
fi

BUILD_CMD="go-${GOOS}-${GOARCH} build -o bin/${BIN_NAME} $APP_NAME.go"

echo "Building $APP_NAME for ${GOOS}/${GOARCH}..."
$BUILD_CMD &
done

# Wait for builds to complete
for job in $(jobs -p); do
wait $job
done

echo "All done"
Loading

0 comments on commit 2f4fabf

Please sign in to comment.