Skip to content

Commit

Permalink
Use argparse to handle args, moving input file paths to an option as …
Browse files Browse the repository at this point in the history
…multiline strings
  • Loading branch information
p3lim committed Apr 24, 2023
1 parent a7317d6 commit 7f7010c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ branding:
color: white
inputs:
files:
description: Space-separated list of license files to update
description: Multiline string of license files to update
required: true
runs:
using: composite
steps:
- run: python3 $GITHUB_ACTION_PATH/update.py ${{ inputs.files }}
- run: python3 $GITHUB_ACTION_PATH/update.py -f "${{ inputs.files }}"
shell: bash
26 changes: 22 additions & 4 deletions update.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
#!/usr/bin/env python3

import os
import re
import sys
import fileinput
import argparse
from datetime import datetime

if len(sys.argv) <= 1:
print('Please tell me which license file(s) to update')
sys.exit(1)
parser = argparse.ArgumentParser(
usage='%(prog)s [OPTION]',
description='Updates license file(s) to the current year.'
)

parser.add_argument('-f', '--files', help='Multiline string of file paths', required=True)
args = parser.parse_args()

paths = []
for line in iter(args.files.splitlines()):
if line != '':
if not os.path.isfile(line):
print(f'file not found at path "{line}"')
sys.exit(1)
elif line in paths:
print(f'duplicate path "{line}"')
sys.exit(1)
else:
paths.append(line)

PATTERN = re.compile(r'^([Cc]opyright.+) ([0-9-]{4,9}) (.+)$')
NEW_YEAR = datetime.now().year

for line in fileinput.input(inplace=True):
for line in fileinput.input(files=paths, inplace=True):
match = PATTERN.match(line)
if match:
sys.stdout.write(f'{match.group(1)} {match.group(2)[:4]}-{NEW_YEAR} {match.group(3)}\n')
Expand Down

0 comments on commit 7f7010c

Please sign in to comment.