Skip to content

Commit 7764d98

Browse files
committed
feat(main): add token retrieval and validation for API authentication
This update introduces a new function `get_token` that retrieves the API token based on priority from three sources: a passed parameter, an environment variable, and a configuration file. The main command-line interface (CLI) has been updated to utilize this function, ensuring that the API token is validated before executing subcommands such as installing hooks, generating documentation, and committing changes. This change improves the user experience by providing clear error messages when the API token is not provided, enhancing the overall robustness of the Penify CLI tool.
1 parent 963953e commit 7764d98

1 file changed

Lines changed: 47 additions & 8 deletions

File tree

penify_hook/main.py

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,48 +199,87 @@ def log_message(self, format, *args):
199199

200200
print("Login process completed. You can now use other commands with your API token.")
201201

202+
203+
def get_token(passed_token):
204+
"""
205+
Get the token based on priority:
206+
1. Passed parameter
207+
2. Environment variable
208+
3. Config file
209+
"""
210+
if passed_token:
211+
return passed_token
212+
213+
env_token = os.getenv('PENIFY_API_TOKEN')
214+
if env_token:
215+
return env_token
216+
217+
config_file = Path.home() / '.penify'
218+
if config_file.exists():
219+
try:
220+
with open(config_file, 'r') as f:
221+
config = json.load(f)
222+
return config.get('api_keys')
223+
except json.JSONDecodeError:
224+
print("Error reading .penify config file. File may be corrupted.")
225+
except Exception as e:
226+
print(f"Error reading .penify config file: {str(e)}")
227+
228+
return None
229+
202230
def main():
203231
parser = argparse.ArgumentParser(description="Penify CLI tool for managing Git hooks and generating documentation.")
204232

233+
parser.add_argument("-t", "--token", help="API token for authentication. If not provided, will check PENIFY_API_TOKEN environment variable, then .penify config file.")
234+
205235
subparsers = parser.add_subparsers(title="subcommands", dest="subcommand")
206236

207237
# Subcommand: install-hook
208238
install_parser = subparsers.add_parser("install-hook", help="Install the Git post-commit hook.")
209239
install_parser.add_argument("-l", "--location", required=True, help="Location in which to install the Git hook.")
210-
install_parser.add_argument("-t", "--token", help="API token for authentication. If not provided, the environment variable 'PENIFY_API_TOKEN' will be used.", default=os.getenv('PENIFY_API_TOKEN'))
211240

212241
# Subcommand: uninstall-hook
213242
uninstall_parser = subparsers.add_parser("uninstall-hook", help="Uninstall the Git post-commit hook.")
214243
uninstall_parser.add_argument("-l", "--location", required=True, help="Location from which to uninstall the Git hook.")
215244

216245
# Subcommand: doc-gen
217246
doc_gen_parser = subparsers.add_parser("doc-gen", help="Generate documentation for specified files or folders.")
218-
doc_gen_parser.add_argument("-t", "--token", help="API token for authentication. If not provided, the environment variable 'PENIFY_API_TOKEN' will be used.", default=os.getenv('PENIFY_API_TOKEN'))
219247
doc_gen_parser.add_argument("-fl", "--file_path", help="Path of the file to generate documentation.")
220248
doc_gen_parser.add_argument("-cf", "--complete_folder_path", help="Generate documentation for the entire folder.")
221249
doc_gen_parser.add_argument("-gf", "--git_folder_path", help="Path to the folder, with git, to scan for modified files. Defaults to the current folder.", default=os.getcwd())
222250

223251
# Subcommand: commit
224252
commit_parser = subparsers.add_parser("commit", help="Commit with a message.")
225253
commit_parser.add_argument("-gf", "--git_folder_path", help="Path to the folder, with git, to scan for modified files. Defaults to the current folder.", default=os.getcwd())
226-
commit_parser.add_argument("-t", "--token", help="API token for authentication. If not provided, the environment variable 'PENIFY_API_TOKEN' will be used.", default=os.getenv('PENIFY_API_TOKEN'))
227254
commit_parser.add_argument("-m", "--message", required=False, help="Commit message.", default="N/A")
228255
commit_parser.add_argument("-e", "--terminal", required=False, help="Open edit terminal", default="False")
229256

230-
login_parser = subparsers.add_parser("login", help="Log in to Penify and automatically obtain an API token.")
231-
257+
# Subcommand: login
258+
login_parser = subparsers.add_parser("login", help="Log in to Penify and obtain an API token.")
232259

233260
args = parser.parse_args()
234261

262+
# Get the token based on priority
263+
token = get_token(args.token)
264+
235265
if args.subcommand == "install-hook":
236-
install_git_hook(args.location, args.token)
266+
if not token:
267+
print("Error: API token is required. Please provide it using -t option, PENIFY_API_TOKEN environment variable, or log in first.")
268+
sys.exit(1)
269+
install_git_hook(args.location, token)
237270
elif args.subcommand == "uninstall-hook":
238271
uninstall_git_hook(args.location)
239272
elif args.subcommand == "doc-gen":
240-
generate_doc(args.token, args.file_path, args.complete_folder_path, args.git_folder_path)
273+
if not token:
274+
print("Error: API token is required. Please provide it using -t option, PENIFY_API_TOKEN environment variable, or log in first.")
275+
sys.exit(1)
276+
generate_doc(token, args.file_path, args.complete_folder_path, args.git_folder_path)
241277
elif args.subcommand == "commit":
278+
if not token:
279+
print("Error: API token is required. Please provide it using -t option, PENIFY_API_TOKEN environment variable, or log in first.")
280+
sys.exit(1)
242281
open_terminal = args.terminal.lower() == "true"
243-
commit_code(args.git_folder_path, args.token, args.message, open_terminal)
282+
commit_code(args.git_folder_path, token, args.message, open_terminal)
244283
elif args.subcommand == "login":
245284
login()
246285
else:

0 commit comments

Comments
 (0)