-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding click usage. Fixing cli usecases: ls and mkdir
- Loading branch information
Showing
4 changed files
with
58 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |