-
Notifications
You must be signed in to change notification settings - Fork 3
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
KEINOS
merged 20 commits into
Qithub-BOT:master
from
KEINOS:KEINOS-static-analysis-and-lint
May 27, 2021
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
67cd55a
feat: add dev container (alpine linux)
KEINOS 9d805c8
feat: add package install for Docker
KEINOS b431cec
feat: add .editorconfig for shfmt
KEINOS 82fc366
feat: add .shellcheck for shellcheck
KEINOS 9080418
feat: add linter runner script
KEINOS 8ed9c61
fix: run-lint.sh 自体の lint 修正
KEINOS 834d497
fix: lint archive
KEINOS c6be77f
fix: lint check
KEINOS 81503f5
fix: lint dec
KEINOS 7699617
fix: enc
KEINOS 3fe5958
fix: keygen
KEINOS 794457f
fix: lint sign
KEINOS fc59f32
fix: lint command verify
KEINOS 0759cb1
feat: GitHub Actions for shfmt and shellcheck
KEINOS abfdc7e
fix: GitHub Action fail
KEINOS 597a646
fix: action で go のキャッシュを使わないようにした
KEINOS 111c152
fix: false positive
KEINOS 4cdbf02
fix: shfmt 不必要なスペース削除
KEINOS 521d75b
fix: .editorconfig
KEINOS bc39cac
docs: indentation
KEINOS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) 拡張機能がインストールされていれば、同じ開発環境を利用することができます。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// 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 wanït 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", "esbenp.prettier-vscode"], | ||
|
||
// 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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# ----------------------------------------------------------------------------- | ||
# editorconfig 互換用設定ファイル | ||
# ----------------------------------------------------------------------------- | ||
|
||
root = true | ||
|
||
# Default/Common settings | ||
[*] | ||
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 | ||
|
||
[*.json] | ||
indent_size = 4 | ||
quote_type = single | ||
|
||
|
||
[*.{yml,yaml}] | ||
indent_size = 2 | ||
quote_type = single |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
#!/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' | ||
} | ||
|
||
# 拡張子 .sh ありのスクリプトの shfmt | ||
runShfmtForShExt() { | ||
find . -name '*.sh' -type f -print0 | xargs -0 shfmt -d | ||
} | ||
|
||
# 拡張子 .sh なしのスクリプトの shfmt | ||
runShfmtForNoExt() { | ||
result=$SUCCESS | ||
|
||
# shellcheck disable=SC2086 | ||
set -- $LIST_SCRIPT_NO_EXT | ||
|
||
while [ "${1:+none}" ]; do | ||
path_file_target="${PATH_DIR_BIN}/${1}" | ||
|
||
shfmt -d "$path_file_target" || { | ||
echo >&2 "shfmt fail: ${path_file_target}" | ||
result=$FAILURE | ||
} | ||
|
||
shift | ||
done | ||
|
||
return $result | ||
} | ||
|
||
runShellCheck() { | ||
printf "%s" '- ShellCheck ... ' | ||
|
||
runShellCheckShExt && runShellCheckForNoExt && echo 'OK' | ||
} | ||
|
||
# 拡張子 .sh ありのスクリプトの shellcheck | ||
runShellCheckShExt() { | ||
find . -name '*.sh' -type f -print0 | xargs -0 shellcheck --external-sources | ||
} | ||
|
||
# 拡張子 .sh なしのスクリプトの shellcheck | ||
runShellCheckForNoExt() { | ||
result=$SUCCESS | ||
|
||
# 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" || { | ||
echo >&2 "shellcheck fail: ${path_file_target}" | ||
result=$FAILURE | ||
} | ||
|
||
shift | ||
done | ||
|
||
return $result | ||
} | ||
|
||
# ----------------------------------------------------------------------------- | ||
# 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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: Install latest shfmt | ||
run: GO111MODULE=on go get mvdan.cc/sh/v3/cmd/shfmt | ||
|
||
- name: Run shfmt and shellcheck | ||
run: | | ||
./.github/run-lint.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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個にすべきではないでしょうか?修正はあとでもよい気がしますが
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
確かに .editorconfig で定義しているのにそうでないのは気持ち悪いですね。気づきませんでした。さすがです。
分離して、ポカよけでフォーマッタを入れてみました。