env-pass 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
env_pass-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 dapk dapk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,14 @@
1
+ Metadata-Version: 2.4
2
+ Name: env-pass
3
+ Version: 0.1.0
4
+ Summary:
5
+ License-File: LICENSE
6
+ Author: pio4000
7
+ Author-email: pio4000@mailbox.org
8
+ Requires-Python: >=3.14
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.14
11
+ Classifier: Programming Language :: Python :: 3.15
12
+ Requires-Dist: click (>=8.5.0,<9.0.0)
13
+ Requires-Dist: inquirer (>=3.4.1,<4.0.0)
14
+ Requires-Dist: jmespath (>=1.1.0,<2.0.0)
@@ -0,0 +1,205 @@
1
+ import json
2
+ import os
3
+ import re
4
+ import subprocess
5
+
6
+ import click
7
+ import inquirer
8
+ import jmespath
9
+
10
+ OBJ_KEY_TMP = "obj"
11
+
12
+ TMPL_ENV_FILE = """declare -a vars=({env_vars})
13
+ pass_to_get=0
14
+ if ! $(pass-cli info -o json | jq '.username' > /dev/null 2>&1)
15
+ then
16
+ echo "Logging into proton pass..."
17
+ pass-cli login
18
+ pass_to_get=1
19
+ else
20
+ echo "Valid proton pass token found."
21
+ pass_to_get=1
22
+ fi
23
+
24
+ if [[ "$pass_to_get" == 1 ]]; then
25
+ json=$(pass-cli item view --output json --share-id {shared_id} --item-id {item_id})
26
+ {export_list}
27
+ fi
28
+ """
29
+ TMPL_EXPORT_ITEM = '''export {env_name}="$(echo $json | jq -Rnr '[inputs] | join("") | fromjson | .{json_path}')"'''
30
+ TMPL_EXPORT_ITEM_LIST = '''export {env_name}="$(echo $json | jq -Rnr '[inputs] | join("") | fromjson | .{json_path} | select(.name == "{name}") | .content.Text')"'''
31
+ DEFAULT_ENV_FILE_OUTPUT = "./sample.env"
32
+
33
+
34
+ @click.command()
35
+ @click.option(
36
+ "-u",
37
+ "--proton-pass-uri",
38
+ prompt=True,
39
+ type=click.STRING,
40
+ required=True,
41
+ help="set proton pass uri, eg. moz-extension://.../popup.html#/u/7/share/A...",
42
+ )
43
+ @click.option(
44
+ "-kv",
45
+ "--key-values",
46
+ type=click.STRING,
47
+ help="set ENV-VARIABLE=JSON-PATH, eg. PASSWORD=item.login.password or TOKEN=item.content.extra_fields[].name (comma-seperated for more)",
48
+ )
49
+ @click.option(
50
+ "-o",
51
+ "--output-file",
52
+ type=click.STRING,
53
+ default=DEFAULT_ENV_FILE_OUTPUT,
54
+ help="set output file. default is '{DEFAULT_ENV_FILE_OUTPUT}'",
55
+ )
56
+ @click.option("-s", "--show-json-paths", is_flag=True)
57
+ def main(proton_pass_uri, key_values, output_file, show_json_paths):
58
+ _uri = proton_pass_uri.split("/")
59
+ _share = None
60
+ _item = None
61
+ for i, _u in enumerate(_uri):
62
+ if _u == "share":
63
+ _share = _clr(_uri[i + 1])
64
+ if _u == "item":
65
+ _item = _clr(_uri[i + 1])
66
+
67
+ proc = subprocess.Popen(
68
+ "$(which pass-cli) info",
69
+ shell=True,
70
+ stdin=subprocess.PIPE,
71
+ stdout=subprocess.PIPE,
72
+ )
73
+ out, err = proc.communicate()
74
+
75
+ if out.decode() == "":
76
+ print("pass-cli login not available, please login first, eg. 'pass-cli login'")
77
+ exit(1)
78
+ _cmd = f"$(which pass-cli) item view --output json --share-id {_share} --item-id {_item}"
79
+ print("-" * 144)
80
+ print(f"Execute: {_cmd}")
81
+ print("-" * 144)
82
+ proc = subprocess.Popen(
83
+ _cmd,
84
+ shell=True,
85
+ stdin=subprocess.PIPE,
86
+ stdout=subprocess.PIPE,
87
+ )
88
+ out, err = proc.communicate()
89
+ dict_obj = json.loads(out.decode())
90
+ _mylist = getKeys(dict_obj)
91
+ if show_json_paths is True:
92
+ for mk, mv in _mylist.items():
93
+ if show_json_paths is True:
94
+ print(mk, "=", mv)
95
+ exit(1)
96
+
97
+ _env_names = {}
98
+ if key_values is None:
99
+ _selection = []
100
+ for mk, mv in _mylist.items():
101
+ _selection.append(mk + " = " + mv)
102
+
103
+ questions_path = [
104
+ inquirer.Checkbox(
105
+ "values",
106
+ message="Select (secret) values you needed",
107
+ choices=_selection,
108
+ ),
109
+ ]
110
+
111
+ answers_path = inquirer.prompt(questions_path)
112
+ _choosen = []
113
+ for ans in answers_path["values"]:
114
+ _name = str(ans.split(" = ")[0]).strip()
115
+ _choosen.append(
116
+ inquirer.Text(
117
+ _name, message=f"Set environment variable name for '{_name}'"
118
+ )
119
+ )
120
+ questions_env_name = _choosen
121
+
122
+ _env_names = inquirer.prompt(questions_env_name)
123
+ for k, v in _env_names.items():
124
+ _env_names[k] = v.upper()
125
+ else:
126
+ _kv = key_values.split(",")
127
+ for kv in _kv:
128
+ _kvs = kv.split("=")
129
+ _env_names[str(_kvs[1]).strip()] = str(_kvs[0]).strip().upper()
130
+
131
+ if len(_env_names) > 0:
132
+ _export_items = []
133
+ for k, v in _env_names.items():
134
+ _iss = k.split(".")
135
+ _m = False
136
+ for _is in _iss:
137
+ if len(re.findall(r"\[\]", _is)) > 0:
138
+ _m = True
139
+ break
140
+
141
+ if _m is True:
142
+ _iss_val = _iss.pop()
143
+ _export_items.append(
144
+ TMPL_EXPORT_ITEM_LIST.format(
145
+ env_name=v, json_path=".".join(_iss), name=_iss_val
146
+ )
147
+ )
148
+ else:
149
+ _export_items.append(TMPL_EXPORT_ITEM.format(env_name=v, json_path=k))
150
+ _script = TMPL_ENV_FILE.format(
151
+ env_vars=" ".join(list(_env_names.values())),
152
+ shared_id=_share,
153
+ item_id=_item,
154
+ export_list="\n\t".join(_export_items),
155
+ )
156
+ print("-" * 144)
157
+ print("ENV FILE FOR SOURCE")
158
+ print("-" * 144)
159
+ print(_script)
160
+ print("-" * 144)
161
+ write_file = os.path.expanduser(output_file)
162
+ with open(write_file, "w") as f:
163
+ f.write(_script)
164
+ print(f"File '{write_file}' was written.")
165
+
166
+
167
+ def getKeys(obj):
168
+ _mlist = []
169
+
170
+ def _gk(obj, parent=OBJ_KEY_TMP):
171
+ # global _mlist
172
+ for i in obj.keys():
173
+ _mlist.append(parent + "." + i)
174
+ try:
175
+ _gk(obj[i], parent + "." + i)
176
+ except AttributeError:
177
+ pass
178
+
179
+ _gk(obj)
180
+ _mobj = {}
181
+ for mi in _mlist:
182
+ _val = jmespath.search(mi[len(OBJ_KEY_TMP) + 1 :], obj)
183
+ if isinstance(_val, str):
184
+ _mobj[mi[len(OBJ_KEY_TMP) + 1 :]] = _val
185
+ elif (
186
+ isinstance(_val, list)
187
+ and len(_val) > 0
188
+ and isinstance(_val[0], dict)
189
+ and "name" in _val[0]
190
+ ):
191
+ _mobj[mi[len(OBJ_KEY_TMP) + 1 :] + "[]" + f".{_val[0]['name']}"] = _val[0][
192
+ "content"
193
+ ]["Text"]
194
+
195
+ return _mobj
196
+
197
+
198
+ def _clr(s):
199
+ if len(re.findall(r"\?", s)) > 0:
200
+ return s.split("?")[0]
201
+ return s
202
+
203
+
204
+ if __name__ == "__main__":
205
+ main()
@@ -0,0 +1,30 @@
1
+ [project]
2
+ name = "env-pass"
3
+ version = "0.1.0"
4
+ description = ""
5
+ authors = [
6
+ {name = "pio4000",email = "pio4000@mailbox.org"}
7
+ ]
8
+ requires-python = ">=3.14"
9
+ dependencies = [
10
+ "click (>=8.5.0,<9.0.0)",
11
+ "jmespath (>=1.1.0,<2.0.0)",
12
+ "inquirer (>=3.4.1,<4.0.0)",
13
+ ]
14
+
15
+ [tool.poetry]
16
+ packages = [{include = "app"}]
17
+
18
+ [tool.poetry.scripts]
19
+ env-pass = "app.main:main"
20
+
21
+ [build-system]
22
+ requires = ["poetry-core>=2.0.0,<3.0.0"]
23
+ build-backend = "poetry.core.masonry.api"
24
+
25
+ [dependency-groups]
26
+ dev = [
27
+ "pytest (>=9.1.1,<10.0.0)",
28
+ "black (>=26.10.1,<27.0.0)",
29
+ "isort (>=9.0.2,<10.0.0)"
30
+ ]