|
1 |
| -#!/usr/bin/env ruby |
2 |
| - |
3 |
| -require 'fileutils' |
4 |
| -require 'open3' |
5 |
| -require 'open-uri' |
6 |
| - |
7 |
| -class Style |
8 |
| - class << self |
9 |
| - INSTALLER_URL = 'https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh' |
10 |
| - EXECUTABLE = 'golangci-lint' |
11 |
| - VERSION = '1.49.0' |
12 |
| - VERSION_TAG = "v#{VERSION}" |
13 |
| - |
14 |
| - def call |
15 |
| - install unless up_to_date? |
16 |
| - run(*ARGV) |
17 |
| - end |
18 |
| - |
19 |
| - private |
20 |
| - |
21 |
| - def install_path |
22 |
| - gopath = ENV.fetch('GOPATH', ENV.fetch('HOME')).split(':').first |
23 |
| - File.join(gopath, 'bin') |
24 |
| - end |
25 |
| - |
26 |
| - def bin_path |
27 |
| - File.join(install_path, EXECUTABLE) |
28 |
| - end |
29 |
| - |
30 |
| - def exist? |
31 |
| - File.executable?(bin_path) |
32 |
| - end |
33 |
| - |
34 |
| - def up_to_date? |
35 |
| - return false unless exist? |
36 |
| - out, stat = Open3.capture2(bin_path, '--version') |
37 |
| - raise("Unable to check version of #{bin_path}") unless stat.success? |
38 |
| - out.match(/\b#{VERSION}\b/) |
39 |
| - end |
40 |
| - |
41 |
| - def install |
42 |
| - puts "Installing #{EXECUTABLE}..." |
43 |
| - |
44 |
| - open(INSTALLER_URL) do |installer| |
45 |
| - Open3.popen2('sh', '-s', '--', '-b', install_path, VERSION_TAG) do |stdin, out, stat| |
46 |
| - IO.copy_stream(installer, stdin) |
47 |
| - stdin.close |
48 |
| - print out.read |
49 |
| - |
50 |
| - raise("failed to install #{EXECUTABLE} #{VERSION_TAG}") unless stat.value.success? |
51 |
| - end |
52 |
| - end |
53 |
| - end |
54 |
| - |
55 |
| - def run(*argv) |
56 |
| - config = ENV.fetch('GOLANGCI_CONFIG', '.golangci.yml') |
57 |
| - exec(bin_path, 'run', '--config', config, *argv) |
58 |
| - end |
59 |
| - end |
60 |
| -end |
61 |
| - |
62 |
| -__FILE__ == $PROGRAM_NAME and Style.call |
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# This script is intentionally written to be POSIX compliant to be as portable as possible |
| 4 | + |
| 5 | +set -e |
| 6 | + |
| 7 | +VERSION="1.51.1" |
| 8 | +VERSION_TAG="v${VERSION}" |
| 9 | + |
| 10 | +INSTALLER_URL="github.com/golangci/golangci-lint/cmd/golangci-lint" |
| 11 | +EXECUTABLE="$(basename "${INSTALLER_URL}")" |
| 12 | + |
| 13 | +INSTALL_PATH="${HOME}/.local/share/${EXECUTABLE}/${VERSION}" |
| 14 | +EXECUTABLE_PATH="${INSTALL_PATH}/${EXECUTABLE}" # e.g. $HOME/.local/share/golangci/1.32.0/golangci-lint |
| 15 | + |
| 16 | +GOPATH="${GOPATH:-${HOME}}" |
| 17 | +GOPATH_PRIMARY="${GOPATH%%:*}" # Delete :* from the end, yielding the first path |
| 18 | +BIN_INSTALL_PATH="${GOPATH_PRIMARY}/bin" |
| 19 | +BIN_EXECUTABLE_PATH="${BIN_INSTALL_PATH}/${EXECUTABLE}" # e.g. $HOME/bin/golangci-lint |
| 20 | + |
| 21 | +installed() { |
| 22 | + [ -x "${EXECUTABLE_PATH}" ] |
| 23 | +} |
| 24 | + |
| 25 | +install() { |
| 26 | + echo "Installing ${EXECUTABLE} version ${VERSION}" >&2 |
| 27 | + |
| 28 | + mkdir -p "${INSTALL_PATH}" |
| 29 | + GOBIN="${INSTALL_PATH}" go install "${INSTALLER_URL}@${VERSION_TAG}" |
| 30 | +} |
| 31 | + |
| 32 | +linked() { |
| 33 | + [ -L "${BIN_EXECUTABLE_PATH}" ] && [ "$(readlink "${BIN_EXECUTABLE_PATH}")" = "${EXECUTABLE_PATH}" ] |
| 34 | +} |
| 35 | + |
| 36 | +link() { |
| 37 | + mkdir -p "${BIN_INSTALL_PATH}" |
| 38 | + rm -fv "${BIN_EXECUTABLE_PATH}" |
| 39 | + ln -sfv "${EXECUTABLE_PATH}" "${BIN_EXECUTABLE_PATH}" |
| 40 | +} |
| 41 | + |
| 42 | +case "$1" in |
| 43 | + "--installed") |
| 44 | + installed |
| 45 | + ;; |
| 46 | + "--install") |
| 47 | + installed || install |
| 48 | + ;; |
| 49 | + "--linked") |
| 50 | + installed && linked |
| 51 | + ;; |
| 52 | + "--link") |
| 53 | + (installed || install) && (linked || link) |
| 54 | + ;; |
| 55 | + *) |
| 56 | + installed || install |
| 57 | + exec "${EXECUTABLE_PATH}" "$@" |
| 58 | + ;; |
| 59 | +esac |
0 commit comments