Skip to content

Commit

Permalink
Refactor and improve the new lint creation bash script (#786)
Browse files Browse the repository at this point in the history
* Improving the new lint creation bash scripts

* fixed typo
  • Loading branch information
christopher-henderson authored Jan 20, 2024
1 parent be8dd6a commit 8a61dfa
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 36 deletions.
2 changes: 2 additions & 0 deletions v3/cmd/genTestCerts/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Expand Down
123 changes: 87 additions & 36 deletions v3/newLint.sh
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}"
31 changes: 31 additions & 0 deletions v3/test_template
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)
}
}

0 comments on commit 8a61dfa

Please sign in to comment.