forked from stdlib-js/stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlint
More file actions
executable file
·130 lines (113 loc) · 2.78 KB
/
lint
File metadata and controls
executable file
·130 lines (113 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env bash
#
# @license Apache-2.0
#
# Copyright (c) 2017 The Stdlib Authors.
#
# 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.
# Script to lint a [Travis CI][1] configuration file.
#
# This script is called with the following arguments:
#
# * `$1` - absolute file path of the configuration file
#
# [1]: https://docs.travis-ci.com/user/customizing-the-build
# shellcheck disable=SC2181
# VARIABLES #
# Configuration file absolute file path:
file="$1"
# Validation URL:
url='http://lint.travis-ci.org/'
# FUNCTIONS #
# Defines an error handler.
#
# $1 - error status
on_error() {
echo 'ERROR: An error was encountered during execution.' >&2
exit "$1"
}
# Lints a configuration file.
#
# $1 - file path
lint() {
# Mimic a web browser form request:
curl \
--silent \
-X POST \
--header 'Content-Type:application/x-www-form-urlencoded' \
--data-urlencode yml@"$1" \
"${url}" 2>&1
}
# Processes an HTML response.
#
# $1 - html
process() {
# Raw HTML results content may look as follows:
#
# Success:
#
# ```
# <p class="result">Hooray, your .travis.yml seems to be solid!</p>
# ```
#
# Errors:
#
# ```
# <ul class="result"><li>in <b class="error">os</b> section: dropping <b class="error">osx</b>, does not support <b class="error">node_js</b></li><li>value for <b class="error">addons</b> section is empty, dropping</li><li>in <b class="error">addons</b> section: unexpected key <b class="error">apt</b>, dropping</li></ul>
# ```
# Processing proceeds as follows:
#
# 1) Find the lint results.
# 2) Replace all `<li>` elements with a temporary character `|`.
# 3) Replace all `|` with newline characters
# 4) Remove all HTML elements.
# 5) Remove empty newlines.
echo "$1" \
| grep -o '<ul class="result">.*</ul>' \
| sed 's/<li>/\|/g' \
| tr '|' '\n' \
| sed -e 's/<[^>]*>//g' \
| awk 'NF'
}
# Prints lint errors.
#
# $1 - results
print_errors() {
echo '' >&2
echo 'Lint errors:' >&2
echo '' >&2
echo "$1" >&2
echo '' >&2
}
# Prints success message.
print_success() {
echo 'No lint errors found.' >&2
}
# Main execution sequence.
main() {
local results
local errors
results=$(lint "${file}")
if [[ "$?" -ne 0 ]]; then
on_error 1
fi
errors=$(process "${results}")
if [[ -n "${errors}" ]]; then
print_errors "${errors}"
exit 1
fi
print_success
exit 0
}
# Run main:
main