graykode

(add) script about ngrok, (fixed) commit configure

...@@ -24,6 +24,7 @@ def get_diff_from_project(): ...@@ -24,6 +24,7 @@ def get_diff_from_project():
24 proc = subprocess.Popen(["git", "diff", "--cached"], stdout=subprocess.PIPE) 24 proc = subprocess.Popen(["git", "diff", "--cached"], stdout=subprocess.PIPE)
25 staged_files = proc.stdout.readlines() 25 staged_files = proc.stdout.readlines()
26 staged_files = [f.decode("utf-8") for f in staged_files] 26 staged_files = [f.decode("utf-8") for f in staged_files]
27 + assert staged_files, "You have to update the file via `git add` to change not staged for commit."
27 return staged_files 28 return staged_files
28 29
29 def commit_message_parser(messages): 30 def commit_message_parser(messages):
...@@ -33,16 +34,23 @@ def commit_message_parser(messages): ...@@ -33,16 +34,23 @@ def commit_message_parser(messages):
33 result.append(" ".join(commit["message"])) 34 result.append(" ".join(commit["message"]))
34 return result 35 return result
35 36
36 -def tokenizing(code): 37 +def healthcheck(endpoint):
38 + response = requests.get(
39 + f"{endpoint}/",
40 + headers={'Content-Type': 'application/json; charset=utf-8'}
41 + )
42 + assert response.status_code == 200, f"{endpoint} is not running."
43 +
44 +def tokenizing(code, endpoint):
37 data = {"code": code } 45 data = {"code": code }
38 res = requests.post( 46 res = requests.post(
39 - 'http://127.0.0.1:5000/tokenizer', 47 + f'{endpoint}/tokenizer',
40 data=json.dumps(data), 48 data=json.dumps(data),
41 headers={'Content-Type': 'application/json; charset=utf-8'} 49 headers={'Content-Type': 'application/json; charset=utf-8'}
42 ) 50 )
43 return json.loads(res.text)["tokens"] 51 return json.loads(res.text)["tokens"]
44 52
45 -def commit_autosuggestions(diffs): 53 +def commit_autosuggestions(diffs, endpoint):
46 commit_message = {} 54 commit_message = {}
47 for idx, example in enumerate(whatthepatch.parse_patch(diffs)): 55 for idx, example in enumerate(whatthepatch.parse_patch(diffs)):
48 if not example.changes: 56 if not example.changes:
...@@ -52,16 +60,16 @@ def commit_autosuggestions(diffs): ...@@ -52,16 +60,16 @@ def commit_autosuggestions(diffs):
52 added, deleted = [], [] 60 added, deleted = [], []
53 for change in example.changes: 61 for change in example.changes:
54 if change.old == None and change.new != None: 62 if change.old == None and change.new != None:
55 - added.extend(tokenizing(change.line)) 63 + added.extend(tokenizing(change.line, endpoint=endpoint))
56 isadded = True 64 isadded = True
57 elif change.old != None and change.new == None: 65 elif change.old != None and change.new == None:
58 - deleted.extend(tokenizing(change.line)) 66 + deleted.extend(tokenizing(change.line, endpoint=endpoint))
59 isdeleted = True 67 isdeleted = True
60 68
61 if isadded and isdeleted and example.header.new_path: 69 if isadded and isdeleted and example.header.new_path:
62 data = {"idx": idx, "added" : added, "deleted" : deleted} 70 data = {"idx": idx, "added" : added, "deleted" : deleted}
63 res = requests.post( 71 res = requests.post(
64 - 'http://127.0.0.1:5000/diff', 72 + f'{endpoint}/diff',
65 data=json.dumps(data), 73 data=json.dumps(data),
66 headers={'Content-Type': 'application/json; charset=utf-8'} 74 headers={'Content-Type': 'application/json; charset=utf-8'}
67 ) 75 )
...@@ -70,7 +78,7 @@ def commit_autosuggestions(diffs): ...@@ -70,7 +78,7 @@ def commit_autosuggestions(diffs):
70 else: 78 else:
71 data = {"idx": idx, "added": added, "deleted": deleted} 79 data = {"idx": idx, "added": added, "deleted": deleted}
72 res = requests.post( 80 res = requests.post(
73 - 'http://127.0.0.1:5000/added', 81 + f'{endpoint}/added',
74 data=json.dumps(data), 82 data=json.dumps(data),
75 headers={'Content-Type': 'application/json; charset=utf-8'} 83 headers={'Content-Type': 'application/json; charset=utf-8'}
76 ) 84 )
...@@ -86,6 +94,8 @@ def commit(messages): ...@@ -86,6 +94,8 @@ def commit(messages):
86 94
87 @click.group(invoke_without_command=True) 95 @click.group(invoke_without_command=True)
88 @click.pass_context 96 @click.pass_context
97 +@click.option('--profile', default='default', type=str,
98 + help='unique name for managing each independent settings')
89 @click.option('--file', '-f', type=click.File('r'), 99 @click.option('--file', '-f', type=click.File('r'),
90 help='patch file containing git diff ' 100 help='patch file containing git diff '
91 '(e.g. file created by `git add` and `git diff --cached > test.diff`)') 101 '(e.g. file created by `git add` and `git diff --cached > test.diff`)')
...@@ -93,13 +103,26 @@ def commit(messages): ...@@ -93,13 +103,26 @@ def commit(messages):
93 help='print suggested commit message more detail.') 103 help='print suggested commit message more detail.')
94 @click.option('--autocommit', '-a', is_flag=True, 104 @click.option('--autocommit', '-a', is_flag=True,
95 help='automatically commit without asking if you want to commit') 105 help='automatically commit without asking if you want to commit')
96 -def cli(ctx, file, verbose, autocommit): 106 +def cli(ctx, profile, file, verbose, autocommit):
97 if not ctx.invoked_subcommand: 107 if not ctx.invoked_subcommand:
108 + profile = profile.upper()
109 + path = join(expanduser("~"), '.commit-autosuggestions.ini')
110 + config = configparser.ConfigParser()
111 + if not exists(path):
112 + raise FileNotFoundError("The configuration file for commit-autosuggestions could not be found. "
113 + "Enter the `commit configure --help` command.")
114 + config.read(path)
115 + if profile.upper() not in list(config.keys()):
116 + raise KeyError(f"That profile({profile}) cannot be found in the configuration file. Check the {path}.")
117 +
118 + endpoint = config[profile]['endpoint']
119 + healthcheck(endpoint=endpoint)
120 +
98 staged_files = file if file else get_diff_from_project() 121 staged_files = file if file else get_diff_from_project()
99 staged_files = [f.strip() for f in staged_files] 122 staged_files = [f.strip() for f in staged_files]
100 diffs = "\n".join(staged_files) 123 diffs = "\n".join(staged_files)
101 124
102 - result = commit_autosuggestions(diffs=diffs) 125 + result = commit_autosuggestions(diffs=diffs, endpoint=endpoint)
103 if verbose: 126 if verbose:
104 click.echo( 127 click.echo(
105 json.dumps(result, indent=4, sort_keys=True) + "\n" 128 json.dumps(result, indent=4, sort_keys=True) + "\n"
...@@ -115,22 +138,26 @@ def cli(ctx, file, verbose, autocommit): ...@@ -115,22 +138,26 @@ def cli(ctx, file, verbose, autocommit):
115 @cli.command() 138 @cli.command()
116 @click.option('--profile', default='default', type=str, 139 @click.option('--profile', default='default', type=str,
117 help='unique name for managing each independent settings') 140 help='unique name for managing each independent settings')
118 -@click.option('--endpoint', default='http://127.0.0.1:5000/', type=str, 141 +@click.option('--endpoint', required=True, type=str,
119 - help='endpoint address accessible to the server (example:http://127.0.0.1:5000/)') 142 + help='endpoint address accessible to the server (example : http://127.0.0.1:5000/)')
120 -
121 def configure(profile, endpoint): 143 def configure(profile, endpoint):
144 + profile = profile.upper()
122 path = join(expanduser("~"), '.commit-autosuggestions.ini') 145 path = join(expanduser("~"), '.commit-autosuggestions.ini')
123 config = configparser.ConfigParser() 146 config = configparser.ConfigParser()
124 if exists(path): 147 if exists(path):
125 config.read(path) 148 config.read(path)
126 - if profile not in config: 149 + if profile not in list(config.keys()):
127 config[profile] = {} 150 config[profile] = {}
128 if endpoint: 151 if endpoint:
129 config[profile]['endpoint'] = endpoint 152 config[profile]['endpoint'] = endpoint
130 153
131 click.echo(f"configure of commit-autosuggestions is setted up in {path}.") 154 click.echo(f"configure of commit-autosuggestions is setted up in {path}.")
155 + with open(path, 'w') as configfile:
156 + config.write(configfile)
132 for key in config[profile]: 157 for key in config[profile]:
133 - click.echo(click.style(key, fg='green') + config[profile][key]) 158 + click.echo("[" + click.style('username', fg='blue') + "] : " + profile)
159 + click.echo("[" + click.style(key, fg='green') + "] : " + config[profile][key])
160 + click.echo()
134 161
135 if __name__ == '__main__': 162 if __name__ == '__main__':
136 cli() 163 cli()
...\ No newline at end of file ...\ No newline at end of file
......
1 +#!/usr/bin/env bash
2 +
3 +command -v ./ngrok >/dev/null 2>&1
4 +if [[ $? -ne 0 ]]; then
5 + echo ngrok is not found, installing...
6 + wget -q -nc https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
7 + unzip -qq -n ngrok-stable-linux-amd64.zip
8 + echo Done!
9 +fi
10 +
1 +#!/usr/bin/env bash
2 +
3 +cmd="./ngrok start --all --config ngrok.conf"
4 +
5 +kill -9 $(ps aux | grep $cmd | awk '{print $2}') 2> /dev/null
6 +
7 +echo Opening tunnel
8 +$cmd
9 +