forked from winterjung/split
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (47 loc) · 1.77 KB
/
main.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
import os
from typing import Dict, List, Optional
DEFINED_ACTION_OUTPUTS_NUMBER = 100
def set_action_output(name: str, value: str):
# See https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
path = os.getenv('GITHUB_OUTPUT')
if not path:
print_action_error('$GITHUB_OUTPUT env is required.')
exit(1)
with open(path, 'a') as github_output_file:
github_output_file.write(f'{name}={value}\n')
def print_action_error(msg: str):
print(f'::error file={__name__}::{msg}')
def get_action_input(
name: str, required: bool = False, default: Optional[str] = None
) -> str:
v = os.environ.get(f'INPUT_{name.upper()}', '')
if v == '' and default:
v = default
if required and v == '':
print_action_error(f'input required and not supplied: {name}')
exit(1)
return v
def split(msg: str, sep: str = ' ', maxsplit: int = -1) -> List[str]:
results = msg.split(sep=sep, maxsplit=maxsplit)
if len(results) > DEFINED_ACTION_OUTPUTS_NUMBER:
results = msg.split(
sep=sep, maxsplit=DEFINED_ACTION_OUTPUTS_NUMBER - 1
)
return results
def to_outputs(results: List[str]) -> Dict[str, str]:
outputs = {
'length': str(len(results)),
}
for i, result in enumerate(results):
outputs[f'_{i}'] = result
return outputs
def main():
msg = get_action_input('msg', required=True)
separator = get_action_input('separator', required=False, default=' ')
maxsplit = int(get_action_input('maxsplit', required=False, default='-1'))
results = split(msg, separator, maxsplit)
outputs = to_outputs(results)
for k, v in outputs.items():
set_action_output(k, v)
if __name__ == '__main__':
main()