Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #4 from cloudfoundry-incubator/add-httplint
Browse files Browse the repository at this point in the history
Add httplint.sh
  • Loading branch information
jandubois authored Aug 21, 2020
2 parents 1363d83 + 5e9d5d0 commit 6e83084
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 15 deletions.
15 changes: 0 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
# kubecf-tools

This repository contains various helper tools for [KubeCF](https://github.com/cloudfoundry-incubator/kubecf)

## versioning

This dir contains a common versioning script which prints the version of the current git commit to stdout.

This script is a wrapper around the functionality of `git describe --tags --dirty` with small changes to split the git pre-release identifiers to allow
semver sorting.
It requires the latest tag of the current branch to be a semver version, otherwise it raises an exception.
Semver versions with plus elements like `1.0.2+gold` are not supported and also raise an exception.

If the latest commit is not tagged by a semver tag then a pre-release version is printed.

`<latest-released-version>-[<additional-pre-release-identifier>.]<commits-since-release>.g<small-latest-git-sha>[-<dirty-tag>]`

For example `1.0.2-5.gb3f7a0c1-dirty` or if there are additional pre-release identifiers in the git tag `1.0.2-alpha.5.gb3f7a0c1-dirty`.
9 changes: 9 additions & 0 deletions httplint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Linters

## httplint.sh

Lint for URLs using http, instead of https.

The linter looks for a file __.http_exceptions__ in `pwd`. If it doesn't exist,
it excludes `127.0.0.1` & `localhost`by default. If it exists, it expects one
line for each allowed domain. The file can contain comments, starting with `#`.
14 changes: 14 additions & 0 deletions httplint/httplint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

HTTP_EXCEPTIONS=.http_exceptions
if [ -f "${HTTP_EXCEPTIONS}" ]; then
EXCEPTIONS=$(perl -e 'while(<>){chomp; push @_,quotemeta unless /^#/} print join("|", @_)' < "${HTTP_EXCEPTIONS}")
else
EXCEPTIONS="127\.0\.0\.1|localhost"
fi

if [ "$(git grep --untracked -P "http://(?!${EXCEPTIONS})" -- ':(exclude)httplint.sh' | wc -l)" -gt 0 ]; then
echo "URLS should not start with 'http://' (with excepted domains listed in ${HTTP_EXCEPTIONS})"
git grep --untracked -P "http://(?!${EXCEPTIONS})" -- ':(exclude)httplint.sh'
exit 1
fi
58 changes: 58 additions & 0 deletions httplint/test_httplint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env bash

set -Eeuo pipefail

create_example() {
# let's create an example file on the fly, to not need to implement ignore
# lists ignoring this script
echo 'http''://example.com/fail_without_exceptions_file' > example.md
echo 'http''://localhost/fail_with_exceptions_file' >> example.md
}

cleanup() {
rm -f example.md .http_exceptions
}

trap cleanup EXIT

# Positive test without .http_exceptions
cleanup
create_example
if ./httplint.sh > /dev/null ; then
# we expect to fail because example.com
echo "FAIL: httplint.sh without .http_exceptions didn't find example.com"
exit 1
fi

# Positive test with .http_exceptions
cleanup
create_example
cat > .http_exceptions <<EOF
# exceptions file for httplint
localhost
127.0.0.1
example.com
EOF
if ! ./httplint.sh > /dev/null ; then
# we expect to succeed because example.com & localhost are on the exceptions
echo "FAIL: httplint.sh with .http_exceptions found example.com, albeit it's on exceptions"
exit 1
fi

# Negative test with .http_exceptions
cleanup
create_example
cat > .http_exceptions <<EOF
# exceptions file for httplint
localhost
127.0.0.1
local.com
EOF
if ./httplint.sh > /dev/null ; then
# we expect to fail because example.com is not on the exceptions
echo "FAIL: httplint.sh with .http_exceptions failed to find example.com"
exit 1
fi

echo "SUCCESS: httplint.sh linted correctly"
cleanup
14 changes: 14 additions & 0 deletions versioning/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## versioning

This dir contains a common versioning script which prints the version of the current git commit to stdout.

This script is a wrapper around the functionality of `git describe --tags --dirty` with small changes to split the git pre-release identifiers to allow
semver sorting.
It requires the latest tag of the current branch to be a semver version, otherwise it raises an exception.
Semver versions with plus elements like `1.0.2+gold` are not supported and also raise an exception.

If the latest commit is not tagged by a semver tag then a pre-release version is printed.

`<latest-released-version>-[<additional-pre-release-identifier>.]<commits-since-release>.g<small-latest-git-sha>[-<dirty-tag>]`

For example `1.0.2-5.gb3f7a0c1-dirty` or if there are additional pre-release identifiers in the git tag `1.0.2-alpha.5.gb3f7a0c1-dirty`.

0 comments on commit 6e83084

Please sign in to comment.