-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor and improve the new lint creation bash script #786
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,102 @@ | ||
# Script to create new lint from template | ||
#!/usr/bin/env bash | ||
|
||
USAGE="Usage: $0 <ARG1> <ARG2> <ARG3> | ||
function usage() { | ||
echo "./newLint.sh [-h|--help] -r|--req <REQUIREMENT> -f|--file <FILENAME> -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 " -s|--struct The name of the Golang struct to create." | ||
echo "" | ||
echo "Example:" | ||
echo " $ ./newLint.sh --req rfc --file crl_must_be_good --struct CrlMustBeGood " | ||
echo " Created lint file /home/chris/projects/zlint/v3/lints/rfc/lint_crl_must_be_good.go with struct name CrlMustBeGood" | ||
echo " Created test file /home/chris/projects/zlint/v3/lints/rfc/lint_crl_must_be_good_test.go" | ||
} | ||
|
||
ARG1: Path_name | ||
ARG2: File_name/TestName (no 'lint_' prefix) | ||
ARG3: Struct_name" | ||
function git_root() { | ||
git rev-parse --show-toplevel | ||
} | ||
|
||
if [ $# -eq 0 ]; then | ||
echo "No arguments provided..." | ||
echo "$USAGE" | ||
exit 1 | ||
fi | ||
# Searches within the v3/lints directory for a subdirectory matching | ||
# the name of the governing requirements body provided by the -r|--req flag. | ||
# | ||
# Exits with error code 1 if no such directory is found | ||
function requirement_dir_exists() { | ||
exists=$(find "$(git_root)/v3/lints/" -maxdepth 1 -type d -not -name lints -name "${1}") | ||
if [ -z "${exists}" ]; then | ||
echo "Unknown requirements body (${1}). Valid options are $(valid_requirement_names)." | ||
usage | ||
exit 1 | ||
fi | ||
} | ||
|
||
if [ $# -eq 1 ]; then | ||
echo "Not enough arguments provided..." | ||
echo "$USAGE" | ||
exit 1 | ||
fi | ||
# Echoes out a comma separated list of directories within v3/lints | ||
function valid_requirement_names() { | ||
names=$(find "$(git_root)/v3/lints/" -type d -not -name "lints" -exec basename {} \;) | ||
echo -n "${names}" | tr '\n' ', ' | ||
} | ||
|
||
if [ $# -eq 2 ]; then | ||
echo "Not enough arguments provided..." | ||
echo "$USAGE" | ||
exit 1 | ||
fi | ||
while [[ $# -gt 0 ]]; do | ||
case "$1" in | ||
-r | --req) | ||
requirement_dir_exists "${2}" | ||
REQUIREMENT="${2}" | ||
shift 2 | ||
;; | ||
-f | --file) | ||
LINTNAME="${2}" | ||
FILENAME="lint_${LINTNAME}.go" | ||
TEST_FILENAME="lint_${LINTNAME}_test.go" | ||
shift 2 | ||
;; | ||
-s | --struct) | ||
STRUCTNAME="$2" | ||
shift 2 | ||
;; | ||
-h | --help) | ||
usage | ||
exit 0 | ||
;; | ||
*) | ||
echo "Unknown option: $1" | ||
usage | ||
exit 1 | ||
;; | ||
esac | ||
done | ||
|
||
if [ ! -d lints/$1 ] | ||
then | ||
echo "Directory 'lints/$1' does not exist. Can't make new file." | ||
exit 1 | ||
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." | ||
usage | ||
exit 1 | ||
fi | ||
|
||
if [ -e lints/$1/lint_$2.go ] | ||
then | ||
echo "File already exists. Can't make new file." | ||
exit 1 | ||
if [ -z "${STRUCTNAME}" ]; then | ||
echo "The -s|--strut flag is required." | ||
usage | ||
exit 1 | ||
fi | ||
|
||
PATHNAME=$1 | ||
LINTNAME=$2 | ||
# Remove the first two characters from ${LINTNAME} and save the resulting string into FILENAME | ||
FILENAME=${LINTNAME:2} | ||
STRUCTNAME=$3 | ||
PATHNAME="$(git_root)/v3/lints/${REQUIREMENT}/${FILENAME}" | ||
TEST_PATHNAME="$(git_root)/v3/lints/${REQUIREMENT}/${TEST_FILENAME}" | ||
|
||
sed -e "s/PACKAGE/${REQUIREMENT}/" \ | ||
-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/${PATHNAME}/" \ | ||
sed -e "s/PACKAGE/${REQUIREMENT}/" \ | ||
-e "s/PASCAL_CASE_SUBST/${STRUCTNAME^}/g" \ | ||
-e "s/SUBST/${STRUCTNAME}/g" \ | ||
-e "s/SUBTEST/${LINTNAME}/g" template > lints/${PATHNAME}/lint_${FILENAME}.go | ||
-e "s/SUBTEST/${LINTNAME}/g" "$(git_root)/v3/test_template" > "${TEST_PATHNAME}" | ||
|
||
echo "Created file lints/${PATHNAME}/lint_${FILENAME}.go with struct name ${STRUCTNAME}" | ||
echo "Created lint file ${PATHNAME} with struct name ${STRUCTNAME}" | ||
echo "Created test file ${TEST_PATHNAME}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* ZLint Copyright 2023 Regents of the University of Michigan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package PACKAGE | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/zmap/zlint/v3/lint" | ||
"github.com/zmap/zlint/v3/test" | ||
) | ||
|
||
func TestPASCAL_CASE_SUBST(t *testing.T) { | ||
inputPath := "TEST_CERT.pem" | ||
expected := lint.Error | ||
out := test.TestLint("LINT_NAME", inputPath) | ||
if out.Status != expected { | ||
t.Errorf("%s: expected %s, got %s", inputPath, expected, out.Status) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a dependency update in the test certificate generator script was laying around. It's not related to this change, but I don't see any harm in including it.