github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/cmd/camtool/gsinit.go (about)

     1  /*
     2  Copyright 2011 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"bufio"
    21  	"encoding/json"
    22  	"flag"
    23  	"fmt"
    24  	"os"
    25  	"strings"
    26  
    27  	"camlistore.org/pkg/cmdmain"
    28  	"camlistore.org/pkg/googlestorage"
    29  	"camlistore.org/third_party/code.google.com/p/goauth2/oauth"
    30  )
    31  
    32  type gsinitCmd struct{}
    33  
    34  func init() {
    35  	cmdmain.RegisterCommand("gsinit", func(flags *flag.FlagSet) cmdmain.CommandRunner {
    36  		return new(gsinitCmd)
    37  	})
    38  }
    39  
    40  func (c *gsinitCmd) Describe() string {
    41  	return "Init Google Storage."
    42  }
    43  
    44  func (c *gsinitCmd) Usage() {
    45  	fmt.Fprintf(os.Stderr, "Usage: camtool [globalopts] gsinit \n")
    46  }
    47  
    48  func (c *gsinitCmd) RunCommand(args []string) error {
    49  	var (
    50  		err          error
    51  		clientId     string
    52  		clientSecret string
    53  	)
    54  
    55  	if clientId, clientSecret, err = getClientInfo(); err != nil {
    56  		return err
    57  	}
    58  	transport := googlestorage.MakeOauthTransport(clientId, clientSecret, "")
    59  
    60  	var accessCode string
    61  	if accessCode, err = getAccessCode(transport.Config); err != nil {
    62  		return err
    63  	}
    64  	if _, err = transport.Exchange(accessCode); err != nil {
    65  		return err
    66  	}
    67  
    68  	fmt.Printf("\nYour Google Storage auth object:\n\n")
    69  	enc := json.NewEncoder(os.Stdout)
    70  	authObj := map[string]string{
    71  		"client_id":     transport.ClientId,
    72  		"client_secret": transport.ClientSecret,
    73  		"refresh_token": transport.RefreshToken,
    74  	}
    75  	enc.Encode(authObj)
    76  	fmt.Print("\n")
    77  	return nil
    78  }
    79  
    80  // Prompt the user for an input line.  Return the given input.
    81  func prompt(promptText string) (string, error) {
    82  	fmt.Print(promptText)
    83  	input := bufio.NewReader(os.Stdin)
    84  	line, _, err := input.ReadLine()
    85  	if err != nil {
    86  		return "", fmt.Errorf("Failed to read line: %v", err)
    87  	}
    88  	return strings.TrimSpace(string(line)), nil
    89  }
    90  
    91  // Provide the authorization link, then prompt for the resulting access code
    92  func getAccessCode(config *oauth.Config) (string, error) {
    93  	fmt.Printf("In order to obtain a storage access code, you will need to navigate to the following URL:\n\n")
    94  	fmt.Printf("https://accounts.google.com/o/oauth2/auth?client_id=%s&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=%s&response_type=code\n\n",
    95  		config.ClientId, config.Scope)
    96  	return prompt("Please enter the access code provided by that page:")
    97  }
    98  
    99  // Prompt for client id / secret
   100  func getClientInfo() (string, string, error) {
   101  	fmt.Printf("Please provide the client id and client secret for your google storage account\n")
   102  	fmt.Printf("(You can find these at http://code.google.com/apis/console > your project > API Access)\n")
   103  	var (
   104  		err          error
   105  		clientId     string
   106  		clientSecret string
   107  	)
   108  	if clientId, err = prompt("Client ID:"); err != nil {
   109  		return "", "", err
   110  	}
   111  	if clientSecret, err = prompt("Client Secret:"); err != nil {
   112  		return "", "", err
   113  	}
   114  	return clientId, clientSecret, nil
   115  }