aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/lint.sh
blob: 63cdb0e0dc6f803550f980be5e156f1ca3e32431 (plain) (blame)
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
#!/usr/bin/env bash
set -eou pipefail

source "$(dirname "${BASH_SOURCE[0]}")/lib/log.sh"

usage() {
  echo "Usage: $0 [LINTER]"
  echo ""
  echo "Run code linters"
  echo ""
  echo "Arguments:"
  echo "  LINTER    Linter to run (gofmt, shellcheck, shfmt)"
  echo "            If not specified, runs all linters"
  echo ""
  echo "Examples:"
  echo "  $0 gofmt      # Run only gofmt"
  echo "  $0            # Run all linters"
}

main() {
  local linter="${1:-}"
  if [[ $linter == "-h" || $linter == "--help" ]]; then
    usage
    exit 0
  fi

  if [[ -n "$linter" ]]; then
    case "$linter" in
      gofmt)
        lint_gofmt
        ;;
      shellcheck)
        lint_shellcheck
        ;;
      shfmt)
        lint_shfmt
        ;;
      *)
        log_error "Invalid linter: $linter"
        log_newline
        usage
        exit 1
        ;;
    esac
  else
    log_header "Running all linters"
    local failed_linters=()

    if ! lint_gofmt; then
      failed_linters+=("gofmt")
    fi

    if ! lint_shellcheck; then
      failed_linters+=("shellcheck")
    fi

    if ! lint_shfmt; then
      failed_linters+=("shfmt")
    fi

    if [[ ${#failed_linters[@]} -eq 0 ]]; then
      log_success "All linters passed!"
    else
      log_error "Failed linters: ${failed_linters[*]}"
      exit 1
    fi
  fi
}

lint_gofmt() {
  log_info "Running gofmt..."
  local unformatted
  unformatted=$(gofmt -l .)
  if [[ -n "$unformatted" ]]; then
    log_error "The following files are not formatted:"
    echo "$unformatted"
    return 1
  fi
  log_success "gofmt passed"
  return 0
}

lint_shellcheck() {
  log_info "Running shellcheck..."
  if ! command -v shellcheck &>/dev/null; then
    log_error "shellcheck is not installed"
    return 1
  fi

  if shellcheck scripts/*.sh scripts/lib/*.sh; then
    log_success "shellcheck passed"
    return 0
  else
    return 1
  fi
}

lint_shfmt() {
  log_info "Running shfmt..."
  if ! command -v shfmt &>/dev/null; then
    log_error "shfmt is not installed"
    return 1
  fi

  local unformatted
  unformatted=$(shfmt -l scripts/)
  if [[ -n "$unformatted" ]]; then
    log_error "The following files are not formatted:"
    echo "$unformatted"
    return 1
  fi
  log_success "shfmt passed"
  return 0
}

main "$@"