Skip to content

Commit

Permalink
Fix newLint.sh CLI
Browse files Browse the repository at this point in the history
The `newLint.sh` script now
- accepts the lint name as an argument and
- validates its prefix (e_, w_, or n_) for consistency.
- automatically determines the filename.

The CONTRIBUTING.md file has been updated accordingly.
  • Loading branch information
kowshikRoy committed Nov 3, 2024
1 parent eba3486 commit 3248fe0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ Adding New Lints
----------------

**Generating Lint Scaffolding.** The scaffolding for a new lints can be created
by running `./newLint.sh <path_name> <lint_name> <structName>`. Path name may be
by running `./newLint.sh -r <requirement> -n <lint_name> -s <structName>`. Path name may be
one of the existing folders under `lints` (for example `apple`, `cabf_br`, `rfc`
etc) and the choice depends on who authors/suggests the lint specification. Lint
names are generally of the form `e_subject_common_name_not_from_san` where the
first letter is one of: `e`, `w`, or `n` (error, warning, or notice respectively).
Struct names following Go conventions, e.g., `subjectCommonNameNotFromSAN`. Example:
`./newLint.sh rfc e_subject_common_name_not_from_san subjectCommonNameNotFromSAN`.
`./newLint.sh -r rfc -n e_subject_common_name_not_from_san -s subjectCommonNameNotFromSAN`.
This will generate a new lint in the `lints/rfc` directory with the necessary
fields filled out.

Expand Down
28 changes: 19 additions & 9 deletions v3/newLint.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env bash

function usage() {
echo "./newLint.sh [-h|--help] -r|--req <REQUIREMENT> -f|--file <FILENAME> -s|--struct <STRUCTNAME>"
echo "./newLint.sh [-h|--help] -r|--req <REQUIREMENT> -n|--name <LINTNAME> -s|--struct <STRUCTNAME>"
echo ""
echo "Options:"
echo " -h|--help Prints this help text."
echo " -r|--req The name of the requirements body governing this lint. Valid options are $(valid_requirement_names)."
echo " -f|--file The target filename for the given lint (no file extension is required)."
echo " -n|--name The lintname for the given lints."
echo " -s|--struct The name of the Golang struct to create."
echo ""
echo "Example:"
Expand Down Expand Up @@ -45,10 +45,8 @@ while [[ $# -gt 0 ]]; do
REQUIREMENT="${2}"
shift 2
;;
-f | --file)
-n| --name)
LINTNAME="${2}"
FILENAME="lint_${LINTNAME}.go"
TEST_FILENAME="lint_${LINTNAME}_test.go"
shift 2
;;
-s | --struct)
Expand All @@ -67,34 +65,46 @@ while [[ $# -gt 0 ]]; do
esac
done

if [[ $LINTNAME =~ ^[enw]_ ]]; then
FILENAME="lint_${LINTNAME:2}.go"
TEST_FILENAME="lint_${LINTNAME:2}_test.go"
else
echo "The lintname should start with e_, w_, n_"
usage
exit 1
fi

if [ -z "${REQUIREMENT}" ]; then
echo "The -r|--req flag is required. Valid options are $(valid_requirement_names)"
usage
exit 1
fi

if [ -z "${LINTNAME}" ]; then
echo "The -f|--file flag is required."
echo "The -n|--name flag is required."
usage
exit 1
fi

if [ -z "${STRUCTNAME}" ]; then
echo "The -s|--strut flag is required."
echo "The -s|--struct flag is required."
usage
exit 1
fi

echo $STRUCTNAME
PATHNAME="$(git_root)/v3/lints/${REQUIREMENT}/${FILENAME}"
TEST_PATHNAME="$(git_root)/v3/lints/${REQUIREMENT}/${TEST_FILENAME}"
echo "$(git_root)/v3/template"
echo "$(git_root)/v3/test_template"

sed -e "s/PACKAGE/${REQUIREMENT}/" \
-e "s/PASCAL_CASE_SUBST/${STRUCTNAME^}/g" \
-e "s/PASCAL_CASE_SUBST/${STRUCTNAME}/g" \
-e "s/SUBST/${STRUCTNAME}/g" \
-e "s/SUBTEST/${LINTNAME}/g" "$(git_root)/v3/template" > "${PATHNAME}"

sed -e "s/PACKAGE/${REQUIREMENT}/" \
-e "s/PASCAL_CASE_SUBST/${STRUCTNAME^}/g" \
-e "s/PASCAL_CASE_SUBST/${STRUCTNAME}/g" \
-e "s/SUBST/${STRUCTNAME}/g" \
-e "s/SUBTEST/${LINTNAME}/g" "$(git_root)/v3/test_template" > "${TEST_PATHNAME}"

Expand Down

0 comments on commit 3248fe0

Please sign in to comment.