Skip to content

Commit

Permalink
Adding click usage. Fixing cli usecases: ls and mkdir
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardogr authored and egarcia committed Dec 9, 2020
1 parent 658ebf5 commit aa249d0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 71 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CHANGELOG

## 0.1.3

- fixing cli usecases
- google-drive ls
- google-drive mkdir
- adding usage of click for cli interaction

## 0.1.2

- fixing api.GoogleService.__get_credentials function, adding scopes argument to its __init__ funciton
Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
- [ ] Add documentation
- [ ] Add pull requests and issues templates
- [X] Add CONTRIBUTING
- [ ] Nice print for cli output
3 changes: 2 additions & 1 deletion googledrive/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def authenticate(self, credentials, scopes):
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
credentials, GoogleAuth.SCOPES)
credentials,
scopes)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
Expand Down
118 changes: 48 additions & 70 deletions googledrive/cli.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,53 @@
import sys
import argparse
import click

from googledrive.api import GoogleAuth
from googledrive.api import GoogleDrive

# If modifying these scopes, delete the file token.pickle.
SCOPES = [
# drive: Full, permissive scope to access all of a user's files,
# excluding the Application Data folder.
'https://www.googleapis.com/auth/drive',
# docs: Per-file access to files that the app created or opened.
'https://www.googleapis.com/auth/drive.file',
# sheets:
# Allows read-only access to the user's sheets and their properties.
'https://www.googleapis.com/auth/spreadsheets.readonly',
]

def help():
return '''
Usage: google-drive [OPTIONS] COMMAND
Manage and interact with your Google Drive.
Commands:
login Perform a login with google oauth
ls List directory contents
mkdir Make directory
'''

def googledrive(args=sys.argv):
args = args[1:]

if len(args) == 0:
print(help())
exit()

command = args[0]
args = args[1:] or []

if 'login' == command:
credentials = args[0]
GoogleAuth().authenticate(
credentials=credentials,
scopes=SCOPES)

elif 'ls' == command:
credentials = args[0]
path = args[1]
files = GoogleDrive(credentials).googledrive_ls(path)
for file in files:
print(file.name) # TODO: nice print

elif 'mkdir' == command:
credentials = args[0]
name = args[1]
folder = GoogleDrive(credentials).create_folder(name)
print(folder) # TODO: nice print

# TODO
elif 'cat' == command:
credentials = args[0]
path = args[1]

print(f"cat command for file: {path}")

# TODO
elif 'analysis' == command:
# would be nice to expose interesting data
# e.g. duplicated files that we cannot disambiguate through API
print('analyzing your drive...')

else:
print(f"Command {command} not expected.")
class Config:
# If modifying these scopes, delete the file token.pickle.
SCOPES = [
# drive: Full, permissive scope to access all of a user's files,
# excluding the Application Data folder.
'https://www.googleapis.com/auth/drive',
# docs: Per-file access to files that the app created or opened.
'https://www.googleapis.com/auth/drive.file',
# sheets:
# Allows read-only access to the user's sheets and their properties.
'https://www.googleapis.com/auth/spreadsheets.readonly',
]

@click.command()
@click.argument('credentials', type=click.Path(exists=True))
def login(credentials):
"""Perform a login with google oauth"""
GoogleAuth().authenticate(
credentials=credentials,
scopes=Config.SCOPES)

@click.command()
@click.argument('credentials', type=click.Path(exists=True))
@click.argument('path')
def ls(credentials, path):
"""List directory contents"""
google_drive = GoogleDrive(credentials, Config.SCOPES)
files = google_drive.googledrive_ls(path)
for file in files:
print(file.name) # TODO: nice print

@click.command()
@click.argument('credentials', type=click.Path(exists=True))
@click.argument('name')
def mkdir(credentials, name):
"""Make directory"""
google_drive = GoogleDrive(credentials, Config.SCOPES)
folder = google_drive.create_folder(name)
print(folder) # TODO: nice print

@click.group()
def googledrive():
pass

googledrive.add_command(login)
googledrive.add_command(ls)
googledrive.add_command(mkdir)

0 comments on commit aa249d0

Please sign in to comment.