11#!/opt/homebrew/Caskroom/miniconda/base/bin/python
2- """
3- Execute this command to get pw from json into clipboard.
4- Type "pw -h" to get help on all available commands.
5- Basic usage: "pw <entity>" -> Gets password of entity from main section.
6-
7- The creds.json file should look like this:
8-
9- "section": {
10- "entity": {
11- "password": "lorem",
12- "username": "ipsum",
13- "additional_info": "...",
14- "website": "www.kuda.ai"
15- }
16- }
17- """
182import argparse
193
204import pyperclip
215
22- from pw_config import CREDS_DIR , CREDS_FILE_PATH
6+ from pw_config import CREDS_DIR , CREDS_FILE_PATH , ENCRYPTION_KEY
237import pw_client
248from pw_encryption import SynchronousEncryption
25- from pw_config import ENCRYPTION_KEY
269
2710HELP_TEXT = {
28- 'input ' : 'Name of entity that holds the password.' ,
11+ 'entity ' : 'Name of entity that holds the password.' ,
2912 'all_sections' : 'Print all available sections.' ,
3013 'section' :
3114 '''Pass a section to print all entities of that section.
4528}
4629
4730
31+ # TODO: Can I attach a callable / function directly to arg parse so
32+ # TODO: that I can avoid the exhaustingly long if / else statements below?
4833def parse_args ():
4934 parser = argparse .ArgumentParser (description = 'Manage your passwords from your terminal.' )
50- parser .add_argument ('input' , type = str , help = HELP_TEXT ['input' ], nargs = '?' )
35+ parser .add_argument ('entity' , type = str , help = HELP_TEXT ['entity' ], nargs = '?' )
36+
37+ parser .add_argument ('-k' , '--secret_key' , type = str , default = 'password' )
38+ parser .add_argument ('-ks' , '--available_keys' , action = 'store_true' )
5139 parser .add_argument ('-as' , '--all_sections' , action = 'store_true' , help = HELP_TEXT ['all_sections' ])
5240 parser .add_argument ('-s' , '--section' , type = str , help = HELP_TEXT ['section' ])
5341 parser .add_argument ('-r' , '--generate_random_pw' , action = 'store_true' , help = HELP_TEXT ['generate_random_pw' ])
54- parser .add_argument ('-n' , '--add_new_password' , type = str , help = HELP_TEXT ['add_new_password' ])
42+ parser .add_argument ('-rl' , '--random_password_length' , type = int , default = 42 )
43+
44+ parser .add_argument ('-n' , '--new_secrets_data' , type = str , help = HELP_TEXT ['add_new_password' ])
45+ parser .add_argument ('-pw' , '--set_password' , type = str , help = HELP_TEXT ['set_password' ])
5546 parser .add_argument ('-u' , '--username' , type = str )
5647 parser .add_argument ('-w' , '--website' , type = str )
57- parser .add_argument ('-pw' , '--set_password' , type = str , help = HELP_TEXT ['set_password' ])
58- parser .add_argument ('-rm' , '--remove_password' , type = str , help = HELP_TEXT ['remove' ])
48+ parser .add_argument ('--kwargs' , '--keyword_arguments' , type = str )
49+ parser .add_argument ('-ow' , '--overwrite' , action = 'store_true' )
50+
51+ parser .add_argument ('-rm' , '--remove_entity' , type = str , help = HELP_TEXT ['remove' ])
5952 parser .add_argument ('-rms' , '--remove_section' , type = str , help = HELP_TEXT ['remove' ])
6053
6154 args = parser .parse_args ()
@@ -67,67 +60,88 @@ def main():
6760 crypto = SynchronousEncryption (ENCRYPTION_KEY )
6861 pw = pw_client .PasswordClient (CREDS_DIR , CREDS_FILE_PATH )
6962
70- # TODO: Get data inside pw_command.py
71- # pw_data = pw.get_pw(???)
72-
7363 if args .all_sections :
7464 return pw .print_sections ()
7565
76- if args .add_new_password :
66+ if args .new_secrets_data :
67+ secrets_data = {}
68+
7769 if args .set_password :
78- encrypted_password = crypto . encrypt ( args .set_password )
70+ new_password = args .set_password
7971 else :
80- new_random_password = pw .generate_random_password ()
81- encrypted_password = crypto .encrypt (new_random_password )
82- return pw .add_new_pw (entity = args .add_new_password , username = args .username ,
83- website = args .website , section = args .section ,
84- password = encrypted_password )
85-
86- if args .username :
87- decrypted_username = pw .get_pw (args .username , 'username' , args .section )
88- encrypted_username = crypto .encrypt (decrypted_username )
89- return encrypted_username
90-
91- if args .website :
92- return pw .get_pw (args .website , 'website' , args .section )
93-
94- if args .remove_password :
95- return pw .remove_password (entity = args .remove_password , section = args .section )
72+ new_password = pw .generate_random_password (password_length = args .random_password_length )
73+ encrypted_password = crypto .encrypt (new_password )
74+ secrets_data ['password' ] = encrypted_password
75+
76+ if args .username :
77+ encrypted_username = crypto .encrypt (args .username )
78+ secrets_data ['username' ] = encrypted_username
79+
80+ if args .website :
81+ encrypted_website = crypto .encrypt (args .website )
82+ secrets_data ['website' ] = encrypted_website
83+
84+ if args .kwargs :
85+ kwargs_as_list = args .kwargs .split (',' )
86+ for kwarg in kwargs_as_list :
87+ if len (kwarg .split ('=' )) > 2 :
88+ print ('There is something wrong with your kwargs ...' )
89+ print ('Desired format:' )
90+ print ('"pw --kwargs brand=fender,guitar=strat,string_gauge=0.10"' )
91+ print ('' )
92+ key , value = kwarg .split ('=' )
93+ encrypted_value = crypto .encrypt (value )
94+ secrets_data [key ] = encrypted_value
95+
96+ pw .add_new_secrets_data (entity = args .new_secrets_data ,
97+ secrets_data = secrets_data ,
98+ section = args .section ,
99+ overwrite = args .overwrite )
100+
101+ return True
102+
103+ if args .remove_entity :
104+ return pw .remove_entity (entity = args .remove_entity , section = args .section )
96105
97106 if args .remove_section :
98107 return pw .remove_section (args .remove_section )
99108
100- if args .section and args .input :
101- encrypted_password = pw .get_pw (entity = args .input , section = args .section )
102- password = crypto .decrypt (encrypted_password )
103- return password
104-
109+ # TODO: Move logic of "create new section if not exists" to client
105110 if args .section :
106111 try :
107112 return pw .print_keys_of_section (args .section )
108113 except KeyError :
109114 return pw .create_section (args .section )
110115
116+ # TODO: Move function "generate_random_pw" from pw_client to utils
111117 if args .generate_random_pw :
112- if args .input is not None :
113- random_pw = pw .generate_random_password (password_length = int ( args .input ) )
118+ if args .entity is not None :
119+ random_pw = pw .generate_random_password (password_length = args .random_password_length )
114120 else :
115- random_pw = pw .generate_random_password ()
121+ random_pw = pw .generate_random_password (password_length = args . random_password_length )
116122 pyperclip .copy (random_pw )
117123 print ('The random password has been copied into your clipboard.' )
118124 print ('' )
119125 return random_pw
120126
121- if args .input is None :
122- return print ('Nothing happened. No flags used. No args passed after pw command.' )
127+ if args .entity is None :
128+ print ('Nothing happened. No flags used. No args passed after pw command.' )
129+ return False
123130
124- if args .input :
125- encrypted_password = pw .get_pw (args .input )
126- decrypted_password = crypto .decrypt (encrypted_password )
127- pyperclip .copy (decrypted_password )
128- print (f'Copied { attribute } for "{ entity } " into your clipboard.' )
131+ secrets_data = pw .get_secrets_data (entity = args .entity , section = args .section )
132+
133+ if args .available_keys :
134+ print (f'There are { len (secrets_data .keys ())} available keys for { args .entity } :' )
135+ print (', ' .join (secrets_data .keys ()))
136+ return True
137+
138+ if args .entity :
139+ encrypted_secret_value = secrets_data [args .secret_key ]
140+ decrypted_secret_value = crypto .decrypt (encrypted_secret_value )
141+ pyperclip .copy (decrypted_secret_value )
142+ print (f'Copied { args .secret_key } for "{ args .entity } " into your clipboard.' )
129143 print ('' )
130- return encrypted_password
144+ return True
131145
132146
133147def decrypt_pw_file ():
@@ -145,4 +159,3 @@ def encrypt_pw_file():
145159
146160 # decrypt_pw_file()
147161 # encrypt_pw_file()
148-
0 commit comments