Skip to content
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

#3 shellcheck(静的解析)と shfmt(lint)の実装 #13

Merged
merged 20 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.177.0/containers/alpine/.devcontainer/base.Dockerfile

# [Choice] Alpine version: 3.13, 3.12, 3.11, 3.10
ARG VARIANT="3.13"
FROM mcr.microsoft.com/vscode/devcontainers/base:0-alpine-${VARIANT}

RUN apk update \
&& apk add --no-cache \
shellcheck \
shfmt
5 changes: 5 additions & 0 deletions .devcontainer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 開発用ディレクトリ

このディレクトリは [Docker](https://ja.wikipedia.org/wiki/Docker) の Alpine Linux コンテナ上でスクリプトの開発をするためのものです。

[VSCode](https://code.visualstudio.com/) と、その [Remote - Containers](https://code.visualstudio.com/docs/remote/remote-overview) 拡張機能がインストールされていれば、同じ開発環境を利用することができます。
33 changes: 33 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.177.0/containers/alpine
{
"name": "Alpine",
"build": {
"dockerfile": "Dockerfile",
// Update 'VARIANT' to pick an Alpine version: 3.10, 3.11, 3.12, 3.13
"args": { "VARIANT": "3.13" }
},

// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},

// Add the IDs of extensions you want installed when the container is created.
// Note that some extensions may not work in Alpine Linux. See https://aka.ms/vscode-remote/linux.
"extensions": [
"foxundermoon.shell-format"
],

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "uname -a",

// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
// "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],

// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
}
30 changes: 30 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -----------------------------------------------------------------------------
# editorconfig 互換用設定ファイル
# -----------------------------------------------------------------------------

root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

# Shell-format
[*.sh]
indent_style = space # shfmt -i=4
indent_size = 4 # shfmt -i=4
shell_variant = posix # shfmt -ln=posix
binary_next_line = true # shfmt -bn
switch_case_indent = true # shfmt -ci
space_redirects = false # shfmt -sr
keep_padding = true # shfmt -kp
function_next_line = false # disable shfmt -fn

[*.{yml,yaml,json}]
indent_style = space
indent_size = 4
Copy link
Collaborator

@yoshi389111 yoshi389111 May 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

少しだけ気になったのは、ワークフローの定義 .github/workflows/shellcheck_linux.yml は、インデントがスペース2個になっています。

(jsonファイルはインデント 4 になっているようなので)
yml,yamlを分離して 2 にするか、あるいは shellcheck_linux.yml のインデントをスペース4個にすべきではないでしょうか?

修正はあとでもよい気がしますが

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かに .editorconfig で定義しているのにそう・・でないのは気持ち悪いですね。気づきませんでした。さすがです。

分離して、ポカよけでフォーマッタを入れてみました。

insert_final_newline = true
quote_type = single
124 changes: 124 additions & 0 deletions .github/run-lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/bin/sh
# =============================================================================
# ShellCheck & ShellFormat(shfmt) の一括実行スクリプト
# =============================================================================

# -----------------------------------------------------------------------------
# Constants
# -----------------------------------------------------------------------------
PATH_DIR_REPO="$(dirname "$(cd "$(dirname "${BASH_SOURCE:-$0}")" && pwd)")"
PATH_DIR_BIN="${PATH_DIR_REPO}/bin"
PATH_DIR_RETURN="$(cd . && pwd)"
SUCCESS=0
FAILURE=1

# 拡張子のないスクリプトファイル一覧(テスト対象リスト)
LIST_SCRIPT_NO_EXT="archive check dec enc keygen sign verify"

echo '-------------------------------------------------------------------------------'
echo ' Requirement Check'
echo '-------------------------------------------------------------------------------'

# ShellCheck のインストールチェック
which shellcheck 1>/dev/null 2>/dev/null || {
echo >&2 "shellcheck がインストールされていません"
echo >&2 "参考文献: https://github.com/Qithub-BOT/QiiCipher/issues/3"

exit $FAILURE
}
echo "ShellCheck $(shellcheck --version | grep version:)"

# shfmt のインストールチェック
which shfmt 1>/dev/null 2>/dev/null || {
echo >&2 "shfmt がインストールされていません"
echo >&2 "参考文献: https://github.com/Qithub-BOT/QiiCipher/issues/3"

exit $FAILURE
}
echo "shfmt version: $(shfmt --version)"

# -----------------------------------------------------------------------------
# Functions
# -----------------------------------------------------------------------------
indentStdIn() {
indent="\t"
while IFS= read -r line; do
printf "${indent}%s\n" "${line}"
done
echo
}

runShfmt() {
printf "%s" '- Shellformat ... '

runShfmtForShExt && runShfmtForNoExt && echo 'OK'
}

runShfmtForShExt() {
find . -name '*.sh' -type f -print0 | xargs -0 shfmt -d
}

runShfmtForNoExt() {
result=$SUCCESS

# shellcheck disable=SC2086
set -- $LIST_SCRIPT_NO_EXT

while [ ! "$1" ]; do
path_file_target="${PATH_DIR_BIN}/${1}"

shfmt -d "$path_file_target" || {
echo "File: ${path_file_target}"
result=$FAILURE
}

shift
done

return $result
}

runShellCheck() {
printf "%s" '- ShellCheck ... '

runShellCheckShExt && runShellCheckForNoExt && echo 'OK'
}

runShellCheckShExt() {
find . -name '*.sh' -type f -print0 | xargs -0 shellcheck --external-sources
}

runShellCheckForNoExt() {
# shellcheck disable=SC2086
set -- $LIST_SCRIPT_NO_EXT
while [ ! "${1:+none}" ]; do
path_file_target="${PATH_DIR_BIN}/${1}"

shellcheck --external-sources "$path_file_target"

shift
done
}

# -----------------------------------------------------------------------------
# Main
# -----------------------------------------------------------------------------
set -eu

cd "$PATH_DIR_REPO" || {
echo >&2 "Failed to change dir to: ${PATH_DIR_REPO}"

exit $FAILURE
}

echo '-------------------------------------------------------------------------------'
echo ' Running linters'
echo '-------------------------------------------------------------------------------'
runShfmt
runShellCheck

cd "$PATH_DIR_RETURN" || {
echo >&2 "Failed to change dir to: ${PATH_DIR_RETURN}"

exit $FAILURE
}
36 changes: 36 additions & 0 deletions .github/workflows/shellcheck_linux.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: shellcheck

on:
workflow_dispatch:
pull_request:

jobs:
lint_check:
runs-on: ubuntu-latest
steps:
- name: Checkout the code
uses: actions/checkout@v2

- uses: actions/setup-go@v2
with:
go-version: 1.15.x

- name: Use Cache for Go
uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-

- name: Download Modules
if: steps.cache.outputs.cache-hit != 'true'
run: go mod download

- name: Install latest shfmt
run: |
GO111MODULE=on go get mvdan.cc/sh/v3/cmd/shfmt

- name: Run shfmt and shellcheck
run: |
./.github/run-lint.sh
8 changes: 8 additions & 0 deletions .shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# =============================================================================
# ShellCheck Configuration
# =============================================================================
# See: https://github.com/koalaman/shellcheck/wiki/

# Disable
# SC2230: https://github.com/koalaman/shellcheck/wiki/SC2230
disable=SC2230
Loading