diff --git a/.github/workflows/check_pull_request.yml b/.github/workflows/check_pull_request.yml index a0f9458..3078da9 100644 --- a/.github/workflows/check_pull_request.yml +++ b/.github/workflows/check_pull_request.yml @@ -37,7 +37,6 @@ jobs: run: make test license_validation: - needs: [run_tests] name: "Validate that license added to all files" runs-on: ubuntu-latest @@ -51,7 +50,6 @@ jobs: run: make validation/license lint: - needs: [run_tests, license_validation] name: "Lint golang code" runs-on: ubuntu-latest diff --git a/Makefile b/Makefile index 144c0da..376a296 100644 --- a/Makefile +++ b/Makefile @@ -60,16 +60,25 @@ go-installed: go version bin/jq: curl-installed bin - curl -sSfL https://github.com/jqlang/jq/releases/download/jq-$(JQ_VERSION)/jq-$(JQ_PLATFORM_ARCH) -o ./bin/jq - @chmod +x "./bin/jq" + if ! ./hack/check_binary.sh "jq" "--version" "$(JQ_VERSION)" ; then \ + echo "Install jq"; \ + curl -sSfL https://github.com/jqlang/jq/releases/download/jq-$(JQ_VERSION)/jq-$(JQ_PLATFORM_ARCH) -o ./bin/jq; \ + chmod +x "./bin/jq"; \ + fi bin/golangci-lint: curl-installed bin - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | BINARY=golangci-lint bash -s -- v${GOLANGCI_VERSION} - @chmod +x "./bin/golangci-lint" + if ! ./hack/check_binary.sh "golangci-lint" "--version" "$(GOLANGCI_VERSION)"; then \ + echo "Install golangci-lint"; \ + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | BINARY=golangci-lint bash -s -- v${GOLANGCI_VERSION}; \ + chmod +x "./bin/golangci-lint"; \ + fi bin/gofumpt: curl-installed bin - curl -sSfLo "bin/gofumpt" https://github.com/mvdan/gofumpt/releases/download/v$(GOFUMPT_VERSION)/gofumpt_v$(GOFUMPT_VERSION)_$(GOFUMPT_PLATFORM)_$(GOFUMPT_ARCH) - @chmod +x "./bin/gofumpt" + if ! ./hack/check_binary.sh "gofumpt" "-version" "$(GOFUMPT_VERSION)"; then \ + echo "Install gofumpt"; \ + curl -sSfLo "bin/gofumpt" https://github.com/mvdan/gofumpt/releases/download/v$(GOFUMPT_VERSION)/gofumpt_v$(GOFUMPT_VERSION)_$(GOFUMPT_PLATFORM)_$(GOFUMPT_ARCH); \ + chmod +x "./bin/gofumpt"; \ + fi deps: bin bin/jq bin/golangci-lint bin/gofumpt diff --git a/hack/check_binary.sh b/hack/check_binary.sh new file mode 100755 index 0000000..94089f0 --- /dev/null +++ b/hack/check_binary.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash + +# Copyright 2026 Flant JSC +# +# 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. + +binary="$1" +version_arg="$2" +version="$3" + +function not_empty_or_exit() { + if [ -z "$2" ]; then + echo "$1 is empty" + exit 1 + fi + + return 0 +} + +not_empty_or_exit "binary" "$binary" +not_empty_or_exit "version_arg" "$version_arg" +not_empty_or_exit "version" "$version" + +binary_full_path="$(pwd)/bin/${binary}" + +if [ ! -x "$binary_full_path" ]; then + echo "$binary_full_path not exists or not executable" + exit 1 +fi + +if ! "$binary_full_path" "$version_arg" | grep -q "$version" ; then + echo "$binary_full_path version not match ${version}. Version is $("$binary_full_path" "$version_arg")" + exit 1 +fi + +exit 0 diff --git a/hack/run_tests.sh b/hack/run_tests.sh index 7dde07a..429d026 100755 --- a/hack/run_tests.sh +++ b/hack/run_tests.sh @@ -14,6 +14,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +source "$(pwd)/hack/utils.sh" + +check_go + run_tests="" if [ -n "$RUN_TEST" ]; then @@ -25,6 +29,13 @@ run_dir="$(pwd)" packages="$(go list ./... | grep -v /validation/)" prefix="$(grep -oP 'module .*$' go.mod | sed 's|module ||')" +if [ -z "$(trim_spaces "$packages")" ]; then + echo -e '\033[1;33m!!!\033[0m' + echo -e "\033[1;33mNot found packages in $run_dir with module ${prefix}. Skip go tests\033[0m" + echo -e '\033[1;33m!!!\033[0m' + exit 0 +fi + echo "Found packages: ${packages[@]} in $run_dir with module $prefix" while IFS= read -r p; do diff --git a/hack/utils.sh b/hack/utils.sh new file mode 100644 index 0000000..629f6f6 --- /dev/null +++ b/hack/utils.sh @@ -0,0 +1,78 @@ +#!/usr/bin/env bash + +# Copyright 2026 Flant JSC +# +# 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. + +function trim_spaces() { + local v="$1" + # Remove leading whitespace + v="${v#"${v%%[![:space:]]*}"}" + + # Remove trailing whitespace + v="${v%"${v##*[![:space:]]}"}" + + echo -n "$v" +} + +function do_in_cycle(){ + local msg="$1" + local get_arguments="$2" + local action="$3" + + if [ -z "$msg" ]; then + echo "msg is empty" + exit 1 + fi + + if [ -z "$get_arguments" ]; then + echo "get_arguments is empty" + exit 1 + fi + + if [ -z "$action" ]; then + echo "action is empty" + exit 1 + fi + + local attempts=10 + + echo "Starting $msg with $attempts attempts" + + local sleep_time=2 + local current_attempt=1 + + while [[ -n "$(trim_spaces "$("$get_arguments")")" ]]; do + if [[ "$current_attempt" == "$attempts" ]]; then + echo "All attempts $attempts failed for ${msg}. Exit" + exit 1 + fi + + if ! $action $("$get_arguments"); then + echo "Attempt ${current_attempt}: $msg failed. Sleep ${sleep_time} before next attempt" + sleep "$sleep_time" + fi + + ((current_attempt++)) + done + + echo "$msg done!" + return 0 +} + +function check_go() { + if ! command -v go; then + echo "Go not found!" + exit 1 + fi +} \ No newline at end of file diff --git a/pkg/log/ln_logger_wrapper.go b/pkg/log/ln_logger_wrapper.go index 2d1b0aa..fc55d3a 100644 --- a/pkg/log/ln_logger_wrapper.go +++ b/pkg/log/ln_logger_wrapper.go @@ -14,8 +14,6 @@ package log -import "fmt" - // formatWithNewLineLogger // we often use *F function, but for pretty log we use "\n" in end of string // this interface and wrapper help us for get rit of this @@ -28,22 +26,21 @@ func newFormatWithNewLineLoggerWrapper(parent baseLogger) *formatWithNewLineLogg } func (w *formatWithNewLineLoggerWrapper) InfoF(format string, a ...any) { - w.parent.InfoFWithoutLn(addLnToMessage(format, a...)) + w.parent.InfoFWithoutLn(addLnToFormat(format), a...) } func (w *formatWithNewLineLoggerWrapper) ErrorF(format string, a ...any) { - w.parent.ErrorFWithoutLn(addLnToMessage(format, a...)) + w.parent.ErrorFWithoutLn(addLnToFormat(format), a...) } func (w *formatWithNewLineLoggerWrapper) DebugF(format string, a ...any) { - w.parent.DebugFWithoutLn(addLnToMessage(format, a...)) + w.parent.DebugFWithoutLn(addLnToFormat(format), a...) } func (w *formatWithNewLineLoggerWrapper) WarnF(format string, a ...any) { - w.parent.WarnFWithoutLn(addLnToMessage(format, a...)) + w.parent.WarnFWithoutLn(addLnToFormat(format), a...) } -func addLnToMessage(format string, a ...any) string { - f := format + "\n" - return fmt.Sprintf(f, a...) +func addLnToFormat(format string) string { + return format + "\n" } diff --git a/pkg/log/ln_logger_wrapper_test.go b/pkg/log/ln_logger_wrapper_test.go index 3b9cf18..0e469a7 100644 --- a/pkg/log/ln_logger_wrapper_test.go +++ b/pkg/log/ln_logger_wrapper_test.go @@ -15,6 +15,7 @@ package log import ( + "errors" "fmt" "testing" @@ -38,12 +39,21 @@ func TestLnLoggerWrapper(t *testing.T) { wrapper.ErrorF("Error") assertAddNewLine(t, "Error") + wrapper.ErrorF("VariablesError %s %v", "msg", true) + assertAddNewLine(t, "VariablesError msg true") + wrapper.WarnF("Warn") assertAddNewLine(t, "Warn") + wrapper.WarnF("VariablesWarn %s %v", "msg", true) + assertAddNewLine(t, "VariablesWarn msg true") + wrapper.InfoF("Info") assertAddNewLine(t, "Info") - wrapper.DebugF("Debug") - assertAddNewLine(t, "Debug") + wrapper.InfoF("VariablesInfo %s %v", "msg", errors.New("error")) + assertAddNewLine(t, "VariablesInfo msg error") + + wrapper.DebugF("VariablesDebug %v %s", 42, "msg") + assertAddNewLine(t, "VariablesDebug 42 msg") }