-
Notifications
You must be signed in to change notification settings - Fork 0
/
gifs.py
157 lines (133 loc) · 5.02 KB
/
gifs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from github import Github
import github_actions
from github_path_parser import parse_github_path
import click
class Config():
def __init__(self):
self.github_client = None
pass_config = click.make_pass_decorator(Config, ensure=True)
@click.group()
@click.option(
'--token',
type=str,
envvar='GIFS_TOKEN',
required=True
)
@pass_config
def cli(config, token):
config.github_client = Github(token)
@cli.command()
@click.argument('src')
@click.argument('dest')
@pass_config
def cp(config, src, dest):
"""
Copy a file from local to a Github repository or from a Github repository to local
To use a path on a Github repository, you should add 'gifs:' at the beginning
If paths start with gifs: they should be files in Github and they should start with org/repository (example: gifs:kaplanbar/gifs/setup.py)
"""
if src.startswith('gifs:') and dest.startswith('gifs:'):
github_actions.copy_file(config.github_client, parse_github_path(src[5:]), parse_github_path(dest[5:]))
elif src.startswith('gifs'):
github_actions.download_file(config.github_client, parse_github_path(src[5:]), dest)
elif dest.startswith('gifs'):
github_actions.upload_file(config.github_client, src, parse_github_path(dest[5:]))
else:
click.echo('Click does not support copying a local file to a local direction, add "gifs:" at the beginning of the path in repository')
@cli.command()
@click.argument('src')
@click.argument('dest')
@pass_config
def mv(config, src, dest):
"""
Move a file in Github to destination
Both of the paths should start with gifs: and they should start with org/repository (example: gifs:kaplanbar/gifs/setup.py)
"""
if src.startswith('gifs:') and dest.startswith('gifs:'):
github_actions.copy_file(config.github_client, parse_github_path(src[5:]), parse_github_path(dest[5:]))
elif src.startswith('gifs'):
github_actions.download_file(config.github_client, parse_github_path(src[5:]), dest)
elif dest.startswith('gifs'):
github_actions.upload_file(config.github_client, src, parse_github_path(dest[5:]))
else:
click.echo('Click does not support copying a local file to a local direction, add "gifs:" at the beginning of the path in repository')
@cli.command()
@click.argument('path')
@pass_config
def ls(config, path):
"""
Similar to the 'ls' command at the Linux, please add 'gifs:' at the beginning of the path
If paths start with gifs: they should be files in Github and they should start with org/repository (example: gifs:kaplanbar/gifs/setup.py)
"""
if path.startswith('gifs:'):
path = path[5:]
else:
click.echo('Please add "gifs:" at the beginning of the path')
return
try:
files = github_actions.list_files(config.github_client, parse_github_path(path))
styles = {
'dir': 'cyan',
'file': 'white'
}
for file in files:
click.secho(f'{file.name} ', fg=styles[file.type], nl=False)
click.echo('')
except:
click.echo('An error occured')
@cli.command()
@click.argument('path')
@pass_config
def rm(config, path):
"""
Remove a file on the Github repository, add 'gifs:' at the beginning of the path
of the file. Deleting a local file is not supported
If paths start with gifs: they should be files in Github and they should start with org/repository (example: gifs:kaplanbar/gifs/setup.py)
"""
if path.startswith('gifs:'):
path = path[5:]
else:
click.echo('Please add "gifs:" at the beginning of the path')
return
try:
github_actions.delete_file(config.github_client, parse_github_path(path))
click.echo('File deleted successfuly')
except Exception as e:
print(e)
click.echo('An error occured')
@cli.command()
@click.argument('path')
@pass_config
def mkdir(config, path):
"""
Creates a directory on the path
If paths start with gifs: they should be files in Github and they should start with org/repository (example: gifs:kaplanbar/gifs/exampledir)
"""
if path.startswith('gifs:'):
path = path[5:]
else:
click.echo('Please add "gifs:" at the beginning of the path')
return
try:
github_actions.create_dir(config.github_client, parse_github_path(path))
click.echo('Directory created successfuly')
except:
click.echo('An error occured')
@cli.command()
@click.argument('path')
@pass_config
def rmdir(config, path):
"""
Deletes the directory on the path
If paths start with gifs: they should be files in Github and they should start with org/repository (example: gifs:kaplanbar/gifs/exampledir)
"""
if path.startswith('gifs:'):
path = path[5:]
else:
click.echo('Please add "gifs:" at the beginning of the path')
return
try:
github_actions.delete_dir(config.github_client, parse_github_path(path))
click.echo('Directory deleted successfuly')
except:
click.echo('An error occured')