Skip to content

Commit

Permalink
Merge branch 'master' into 2275-upd-go
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed May 19, 2021
2 parents 9bb32bc + 6f7fd33 commit 6536b32
Show file tree
Hide file tree
Showing 18 changed files with 470 additions and 320 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
'restore-keys': '${{ runner.os }}-go-'
- 'name': 'Get npm cache directory'
'id': 'npm-cache'
'run': 'echo "::set-output name=dir::$(npm config get cache)"'
'run': 'echo "::set-output name=dir::$( npm config get cache )"'
- 'name': 'Set up npm cache'
'uses': 'actions/cache@v2'
'with':
Expand Down
54 changes: 42 additions & 12 deletions HACKING.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# AdGuard Home Developer Guidelines

As of **March 2021**, following this document is obligatory for all new code.
Some of the rules aren't enforced as thoroughly or remain broken in old code,
but this is still the place to find out about what we **want** our code to look
like and how to improve it.
Following this document is obligatory for all new code. Some of the rules
aren't enforced as thoroughly or remain broken in old code, but this is still
the place to find out about what we **want** our code to look like and how to
improve it.

The rules are mostly sorted in the alphabetical order.

Expand All @@ -21,6 +21,7 @@ The rules are mostly sorted in the alphabetical order.
* [Recommended Reading](#recommended-reading)
* [Markdown](#markdown)
* [Shell Scripting](#shell-scripting)
* [Shell Conditionals](#shell-cond)
* [Text, Including Comments](#text-including-comments)
* [YAML](#yaml)

Expand Down Expand Up @@ -315,6 +316,26 @@ on GitHub and most other Markdown renderers. -->

* Avoid spaces between patterns of the same `case` condition.

* `export` and `readonly` should be used separately from variable assignment,
because otherwise failures in command substitutions won't stop the script.
That is, do this:

```sh
X="$( echo 42 )"
export X
```

And **not** this:

```sh
# Bad!
export X="$( echo 42 )"
```

* If a binary value is needed, use `0` for `false`, and `1` for `true`.

* Mark every variable that shouldn't change later as `readonly`.

* Prefer `'raw strings'` to `"double quoted strings"` whenever possible.

* Put spaces within `$( cmd )`, `$(( expr ))`, and `{ cmd; }`.
Expand All @@ -330,8 +351,6 @@ on GitHub and most other Markdown renderers. -->
* UPPERCASE names for external exported variables, lowercase for local,
unexported ones.

* Use `readonly` liberally.

* Use `set -e -f -u` and also `set -x` in verbose mode.

* Use the `"$var"` form instead of the `$var` form, unless word splitting is
Expand All @@ -355,14 +374,23 @@ on GitHub and most other Markdown renderers. -->
dir="${TOP_DIR}"/sub
```

* When using `test` (aka `[`), spell compound conditions with `&&`, `||`, and
`!` **outside** of `test` instead of `-a`, `-o`, and `!` inside of `test`
correspondingly. The latter ones are pretty much deprecated in POSIX.
Also, prefer `!= ''` form instead of `-n` to check if string is empty.
### <a id="shell-cond" href="#shell-cond">Shell Conditionals</a>

Guidelines and agreements for using command `test`, also known as `[`:

* Prefer the `!= ''` form instead of using `-n` to check if string is empty.

* Spell compound conditions with `&&`, `||`, and `!` **outside** of `test`
instead of `-a`, `-o`, and `!` **inside** of `test` correspondingly. The
latter ones are pretty much deprecated in POSIX.

See also: “[Problems With the `test` Builtin: What Does `-a` Mean?][test]”.

* Use `=` for strings and `-eq` for numbers to catch typing errors.

[test]: https://www.oilshell.org/blog/2017/08/31.html

See also: “[Problems With the `test` Builtin: What Does `-a` Mean?]”.

[Problems With the `test` Builtin: What Does `-a` Mean?]: https://www.oilshell.org/blog/2017/08/31.html

## <a id="text-including-comments" href="#text-including-comments">Text, Including Comments</a>

Expand Down Expand Up @@ -400,6 +428,8 @@ on GitHub and most other Markdown renderers. -->
// TODO(usr1, usr2): Fix the frobulation issue.
```



## <a id="yaml" href="#yaml">YAML</a>

* **TODO(a.garipov):** Define naming conventions for schema names in our
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,5 @@ go-os-check:

openapi-lint: ; cd ./openapi/ && $(YARN) test
openapi-show: ; cd ./openapi/ && $(YARN) start

txt-lint: ; $(ENV) "$(SHELL)" ./scripts/make/txt-lint.sh
4 changes: 3 additions & 1 deletion bamboo-specs/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@
cd ./dist/
export CHANNEL="${bamboo.channel}"
CHANNEL="${bamboo.channel}"
export CHANNEL
../bamboo-deploy-publisher/deploy.sh adguard-home-"$CHANNEL"
'final-tasks':
- 'clean'
Expand Down
3 changes: 1 addition & 2 deletions internal/dhcpd/v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,8 +975,7 @@ func (s *v4Server) Start() (err error) {
log.Info("dhcpv4: listening")

go func() {
serr := s.srv.Serve()
if errors.Is(serr, net.ErrClosed) {
if serr := s.srv.Serve(); errors.Is(serr, net.ErrClosed) {
log.Info("dhcpv4: server is closed")

return
Expand Down
13 changes: 9 additions & 4 deletions scripts/hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@

set -e -f -u

if [ "$(git diff --cached --name-only -- 'client/*.js')" ]
if [ "$( git diff --cached --name-only -- 'client/*.js' )" ]
then
make js-lint js-test
fi

if [ "$(git diff --cached --name-only -- 'client2/*.js' 'client2/*.ts' 'client2/*.tsx')" ]
if [ "$( git diff --cached --name-only -- 'client2/*.js' 'client2/*.ts' 'client2/*.tsx' )" ]
then
make js-beta-lint js-beta-test
fi

if [ "$(git diff --cached --name-only -- '*.go' 'go.mod')" ]
if [ "$( git diff --cached --name-only -- '*.go' '*.mod' '*.sh' 'Makefile' )" ]
then
make go-os-check go-lint go-test
fi

if [ "$(git diff --cached --name-only -- './openapi/openapi.yaml')" ]
if [ "$( git diff --cached --name-only -- '*.md' '*.yaml' '*.yml' )" ]
then
make txt-lint
fi

if [ "$( git diff --cached --name-only -- './openapi/openapi.yaml' )" ]
then
make openapi-lint
fi
36 changes: 21 additions & 15 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ is_little_endian() {
# unzip (macOS) / tar (other unices)
#
check_required() {
readonly required_darwin="unzip"
readonly required_unix="tar"
required_darwin="unzip"
required_unix="tar"
readonly required_darwin required_unix

# Split with space.
required="curl"
Expand Down Expand Up @@ -132,7 +133,7 @@ parse_opts() {
esac
done

if [ "$uninstall" = '1' ] && [ "$reinstall" = '1' ]
if [ "$uninstall" -eq '1' ] && [ "$reinstall" -eq '1' ]
then
error_exit 'the -r and -u options are mutually exclusive'
fi
Expand Down Expand Up @@ -277,7 +278,9 @@ fix_freebsd() {
return 0
fi

readonly rcd='/usr/local/etc/rc.d'
rcd='/usr/local/etc/rc.d'
readonly rcd

if ! [ -d "$rcd" ]
then
mkdir "$rcd"
Expand All @@ -292,16 +295,17 @@ configure() {
fix_darwin
check_out_dir

readonly pkg_name="AdGuardHome_${os}_${cpu}.${pkg_ext}"
readonly url="https://static.adguard.com/adguardhome/${channel}/${pkg_name}"
readonly agh_dir="${out_dir}/AdGuardHome"
pkg_name="AdGuardHome_${os}_${cpu}.${pkg_ext}"
url="https://static.adguard.com/adguardhome/${channel}/${pkg_name}"
agh_dir="${out_dir}/AdGuardHome"
readonly pkg_name url agh_dir

log "AdGuard Home will be installed into $agh_dir"
}

# Function is_root checks for root privileges to be granted.
is_root() {
if [ "$( id -u )" = '0' ]
if [ "$( id -u )" -eq '0' ]
then
log 'script is executed with root privileges'

Expand All @@ -325,24 +329,26 @@ please, restart it with root privileges'
#
# TODO(e.burkov): Try to avoid restarting.
rerun_with_root() {
readonly script_url=\
script_url=\
'https://raw.githubusercontent.com/AdguardTeam/AdGuardHome/master/scripts/install.sh'
readonly script_url

flags=''
if [ "$reinstall" = '1' ]
if [ "$reinstall" -eq '1' ]
then
flags="${flags} -r"
fi
if [ "$uninstall" = '1' ]
if [ "$uninstall" -eq '1' ]
then
flags="${flags} -u"
fi
if [ "$verbose" = '1' ]
if [ "$verbose" -eq '1' ]
then
flags="${flags} -v"
fi

readonly opts="-c $channel -C $cpu -O $os -o $out_dir $flags"
opts="-c $channel -C $cpu -O $os -o $out_dir $flags"
readonly opts

log 'restarting with root privileges'

Expand Down Expand Up @@ -397,7 +403,7 @@ handle_existing() {
then
log 'no need to uninstall'

if [ "$uninstall" = '1' ]
if [ "$uninstall" -eq '1' ]
then
exit 0
fi
Expand Down Expand Up @@ -428,7 +434,7 @@ handle_existing() {
log 'AdGuard Home was successfully uninstalled'
fi

if [ "$uninstall" = '1' ]
if [ "$uninstall" -eq '1' ]
then
exit 0
fi
Expand Down
2 changes: 1 addition & 1 deletion scripts/make/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# A docker file for scripts/make/build-docker.sh.

FROM alpine:3.12
FROM alpine:3.13

ARG BUILD_DATE
ARG VERSION
Expand Down
66 changes: 38 additions & 28 deletions scripts/make/build-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,70 +14,80 @@ fi
set -e -f -u

# Require these to be set. The channel value is validated later.
readonly channel="$CHANNEL"
readonly commit="$COMMIT"
readonly dist_dir="$DIST_DIR"
channel="$CHANNEL"
commit="$COMMIT"
dist_dir="$DIST_DIR"
readonly channel commit dist_dir

if [ "${VERSION:-}" = 'v0.0.0' ] || [ "${VERSION:-}" = '' ]
then
readonly version="$(sh ./scripts/make/version.sh)"
version="$( sh ./scripts/make/version.sh )"
else
readonly version="$VERSION"
version="$VERSION"
fi
readonly version

echo $version

# Allow users to use sudo.
readonly sudo_cmd="${SUDO:-}"
sudo_cmd="${SUDO:-}"
readonly sudo_cmd

readonly docker_platforms="\
docker_platforms="\
linux/386,\
linux/amd64,\
linux/arm/v6,\
linux/arm/v7,\
linux/arm64,\
linux/ppc64le"
readonly docker_platforms

readonly build_date="$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
build_date="$( date -u +'%Y-%m-%dT%H:%M:%SZ' )"
readonly build_date

# Set DOCKER_IMAGE_NAME to 'adguard/adguard-home' if you want (and are
# allowed) to push to DockerHub.
readonly docker_image_name="${DOCKER_IMAGE_NAME:-adguardhome-dev}"
# Set DOCKER_IMAGE_NAME to 'adguard/adguard-home' if you want (and are allowed)
# to push to DockerHub.
docker_image_name="${DOCKER_IMAGE_NAME:-adguardhome-dev}"
readonly docker_image_name

# Set DOCKER_OUTPUT to 'type=image,name=adguard/adguard-home,push=true'
# if you want (and are allowed) to push to DockerHub.
readonly docker_output="${DOCKER_OUTPUT:-type=image,name=${docker_image_name},push=false}"
# Set DOCKER_OUTPUT to 'type=image,name=adguard/adguard-home,push=true' if you
# want (and are allowed) to push to DockerHub.
docker_output="${DOCKER_OUTPUT:-type=image,name=${docker_image_name},push=false}"
readonly docker_output

case "$channel"
in
('release')
readonly docker_image_full_name="${docker_image_name}:${version}"
readonly docker_tags="--tag ${docker_image_name}:latest"
docker_image_full_name="${docker_image_name}:${version}"
docker_tags="--tag ${docker_image_name}:latest"
;;
('beta')
readonly docker_image_full_name="${docker_image_name}:${version}"
readonly docker_tags="--tag ${docker_image_name}:beta"
docker_image_full_name="${docker_image_name}:${version}"
docker_tags="--tag ${docker_image_name}:beta"
;;
('edge')
# Don't set the version tag when pushing to the edge channel.
readonly docker_image_full_name="${docker_image_name}:edge"
readonly docker_tags=''
docker_image_full_name="${docker_image_name}:edge"
docker_tags=''
;;
('development')
readonly docker_image_full_name="${docker_image_name}"
readonly docker_tags=''
docker_image_full_name="${docker_image_name}"
docker_tags=''
;;
(*)
echo "invalid channel '$channel', supported values are\
'development', 'edge', 'beta', and 'release'" 1>&2
exit 1
;;
esac
readonly docker_image_full_name docker_tags

# Copy the binaries into a new directory under new names, so that it's eaiser to
# COPY them later. DO NOT remove the trailing underscores. See file
# scripts/make/Dockerfile.
dist_docker="${dist_dir}/docker"
readonly dist_docker

# Copy the binaries into a new directory under new names, so that it's
# eaiser to COPY them later. DO NOT remove the trailing underscores.
# See scripts/make/Dockerfile.
readonly dist_docker="${dist_dir}/docker"
mkdir -p "$dist_docker"
cp "${dist_dir}/AdGuardHome_linux_386/AdGuardHome/AdGuardHome"\
"${dist_docker}/AdGuardHome_linux_386_"
Expand All @@ -92,8 +102,8 @@ cp "${dist_dir}/AdGuardHome_linux_arm_7/AdGuardHome/AdGuardHome"\
cp "${dist_dir}/AdGuardHome_linux_ppc64le/AdGuardHome/AdGuardHome"\
"${dist_docker}/AdGuardHome_linux_ppc64le_"

# Don't use quotes with $docker_tags and $debug_flags because we want
# word splitting and or an empty space if tags are empty.
# Don't use quotes with $docker_tags and $debug_flags because we want word
# splitting and or an empty space if tags are empty.
$sudo_cmd docker\
$debug_flags\
buildx build\
Expand Down
Loading

0 comments on commit 6536b32

Please sign in to comment.