Skip to content

Commit dd9286a

Browse files
authored
[PLUTO-1396] Add update commnad (#77)
* [PLUTO-1396] Add update command
1 parent 333782d commit dd9286a

File tree

3 files changed

+126
-33
lines changed

3 files changed

+126
-33
lines changed

cli-v2.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func main() {
2323
}
2424
}
2525

26-
// Check if command is init
27-
if len(os.Args) > 1 && os.Args[1] == "init" {
26+
// Check if command is init/update
27+
if len(os.Args) > 1 && (os.Args[1] == "init" || os.Args[1] == "update") {
2828
cmd.Execute()
2929
return
3030
}

cmd/update.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cmd
2+
3+
import (
4+
"codacy/cli-v2/config"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/spf13/cobra"
10+
"gopkg.in/yaml.v3"
11+
)
12+
13+
var updateCmd = &cobra.Command{
14+
Use: "update",
15+
Short: "Update to the latest version",
16+
Long: ``,
17+
Run: func(cmd *cobra.Command, args []string) {
18+
// Read version from yaml
19+
versionFile := filepath.Join(config.Config.CodacyDirectory(), "version.yaml")
20+
versionData, err := os.ReadFile(versionFile)
21+
if err != nil {
22+
if os.IsNotExist(err) {
23+
fmt.Println("Could not read version file. Make sure you have the latest version of the script at:")
24+
fmt.Println("https://github.com/codacy/codacy-cli-v2/blob/main/README.md#download t")
25+
} else {
26+
fmt.Printf("Failed to read version.yaml: %v\n", err)
27+
}
28+
os.Exit(1)
29+
}
30+
31+
var versionInfo struct {
32+
Version string `yaml:"version"`
33+
}
34+
if err := yaml.Unmarshal(versionData, &versionInfo); err != nil {
35+
fmt.Printf("Failed to parse version.yaml: %v\n", err)
36+
os.Exit(1)
37+
}
38+
39+
fmt.Printf("✓ Updated to version %s\n", versionInfo.Version)
40+
},
41+
}
42+
43+
func init() {
44+
rootCmd.AddCommand(updateCmd)
45+
}

codacy-cli.sh

Lines changed: 79 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#!/usr/bin/env bash
22

3+
34
set -e +o pipefail
45

6+
# Set up paths first
7+
bin_name="codacy-cli-v2"
8+
9+
# Determine OS-specific paths
510
os_name=$(uname)
611
arch=$(uname -m)
712

@@ -14,16 +19,60 @@ case "$arch" in
1419
;;
1520
esac
1621

22+
if [ -z "$CODACY_CLI_V2_TMP_FOLDER" ]; then
23+
if [ "$(uname)" = "Linux" ]; then
24+
CODACY_CLI_V2_TMP_FOLDER="$HOME/.cache/codacy/codacy-cli-v2"
25+
elif [ "$(uname)" = "Darwin" ]; then
26+
CODACY_CLI_V2_TMP_FOLDER="$HOME/Library/Caches/Codacy/codacy-cli-v2"
27+
else
28+
CODACY_CLI_V2_TMP_FOLDER=".codacy-cli-v2"
29+
fi
30+
fi
31+
32+
version_file="$CODACY_CLI_V2_TMP_FOLDER/version.yaml"
33+
34+
35+
get_version_from_yaml() {
36+
if [ -f "$version_file" ]; then
37+
local version=$(grep -o 'version: *"[^"]*"' "$version_file" | cut -d'"' -f2)
38+
if [ -n "$version" ]; then
39+
echo "$version"
40+
return 0
41+
fi
42+
fi
43+
return 1
44+
}
45+
46+
get_latest_version() {
47+
local response
48+
if [ -n "$GH_TOKEN" ]; then
49+
response=$(curl -Lq --header "Authorization: Bearer $GH_TOKEN" "https://api.github.com/repos/codacy/codacy-cli-v2/releases/latest" 2>/dev/null)
50+
else
51+
response=$(curl -Lq "https://api.github.com/repos/codacy/codacy-cli-v2/releases/latest" 2>/dev/null)
52+
fi
53+
54+
handle_rate_limit "$response"
55+
local version=$(echo "$response" | grep -m 1 tag_name | cut -d'"' -f4)
56+
echo "$version"
57+
}
58+
59+
handle_rate_limit() {
60+
local response="$1"
61+
if echo "$response" | grep -q "API rate limit exceeded"; then
62+
fatal "Error: GitHub API rate limit exceeded. Please try again later"
63+
fi
64+
}
65+
1766
download_file() {
1867
local url="$1"
1968

20-
echo "Download url: ${url}"
69+
echo "Downloading from URL: ${url}"
2170
if command -v curl > /dev/null 2>&1; then
2271
curl -# -LS "$url" -O
2372
elif command -v wget > /dev/null 2>&1; then
2473
wget "$url"
2574
else
26-
fatal "Could not find curl or wget, please install one."
75+
fatal "Error: Could not find curl or wget, please install one."
2776
fi
2877
}
2978

@@ -40,51 +89,50 @@ download_cli() {
4089

4190
local bin_folder="$1"
4291
local bin_path="$2"
92+
local version="$3"
4393

4494
if [ ! -f "$bin_path" ]; then
45-
echo "Downloading the codacy cli v2 version ($CODACY_CLI_V2_VERSION)"
95+
echo "📥 Downloading CLI version $version..."
4696

47-
remote_file="codacy-cli-v2_${CODACY_CLI_V2_VERSION}_${suffix}_${arch}.tar.gz"
48-
url="https://github.com/codacy/codacy-cli-v2/releases/download/${CODACY_CLI_V2_VERSION}/${remote_file}"
97+
remote_file="codacy-cli-v2_${version}_${suffix}_${arch}.tar.gz"
98+
url="https://github.com/codacy/codacy-cli-v2/releases/download/${version}/${remote_file}"
4999

50100
download "$url" "$bin_folder"
51101
tar xzfv "${bin_folder}/${remote_file}" -C "${bin_folder}"
102+
else
103+
echo "✓ Using cached CLI version $version"
52104
fi
53105
}
54106

55-
# Temporary folder for downloaded files
56-
if [ -z "$CODACY_CLI_V2_TMP_FOLDER" ]; then
57-
if [ "$os_name" = "Linux" ]; then
58-
CODACY_CLI_V2_TMP_FOLDER="$HOME/.cache/codacy/codacy-cli-v2"
59-
elif [ "$os_name" = "Darwin" ]; then
60-
CODACY_CLI_V2_TMP_FOLDER="$HOME/Library/Caches/Codacy/codacy-cli-v2"
61-
else
62-
CODACY_CLI_V2_TMP_FOLDER=".codacy-cli-v2"
63-
fi
107+
# Warn if CODACY_CLI_V2_VERSION is set and update is requested
108+
if [ -n "$CODACY_CLI_V2_VERSION" ] && [ "$1" = "update" ]; then
109+
echo "⚠️ Warning: Performing update with forced version $CODACY_CLI_V2_VERSION"
110+
echo " This might prevent updating to the latest version"
64111
fi
65112

66-
# if no version is specified, we fetch the latest
67-
if [ -z "$CODACY_CLI_V2_VERSION" ]; then
68-
if [ -n "$GH_TOKEN" ]; then
69-
CODACY_CLI_V2_VERSION="$(curl -Lq --header "Authorization: Bearer $GH_TOKEN" "https://api.github.com/repos/codacy/codacy-cli-v2/releases/latest" 2>/dev/null | grep -m 1 tag_name | cut -d'"' -f4)"
70-
else
71-
CODACY_CLI_V2_VERSION="$(curl -Lq "https://api.github.com/repos/codacy/codacy-cli-v2/releases/latest" 2>/dev/null | grep -m 1 tag_name | cut -d'"' -f4)"
72-
fi
113+
# Ensure version.yaml exists and is up to date
114+
if [ ! -f "$version_file" ] || [ "$1" = "update" ]; then
115+
echo "ℹ️ Fetching latest version..."
116+
version=$(get_latest_version)
117+
echo "version: \"$version\"" > "$version_file"
73118
fi
74119

75-
# Folder containing the binary
76-
bin_folder="${CODACY_CLI_V2_TMP_FOLDER}/${CODACY_CLI_V2_VERSION}"
77-
# Create the folder if not exists
78-
mkdir -p "$bin_folder"
120+
# Set the version to use
121+
if [ -n "$CODACY_CLI_V2_VERSION" ]; then
122+
version="$CODACY_CLI_V2_VERSION"
123+
else
124+
version=$(get_version_from_yaml)
125+
fi
79126

80-
# name of the binary
81-
bin_name="codacy-cli-v2"
82127

83-
# Set binary path
128+
# Set up version-specific paths
129+
bin_folder="${CODACY_CLI_V2_TMP_FOLDER}/${version}"
130+
131+
mkdir -p "$bin_folder"
84132
bin_path="$bin_folder"/"$bin_name"
85133

86-
# download the tool
87-
download_cli "$bin_folder" "$bin_path"
134+
# Download the tool if not already installed
135+
download_cli "$bin_folder" "$bin_path" "$version"
88136
chmod +x "$bin_path"
89137

90138
run_command="$bin_path"
@@ -93,7 +141,7 @@ if [ -z "$run_command" ]; then
93141
fi
94142

95143
if [ "$#" -eq 1 ] && [ "$1" = "download" ]; then
96-
echo "$g" "Codacy cli v2 download succeeded";
144+
echo "Codacy cli v2 download succeeded"
97145
else
98146
eval "$run_command $*"
99147
fi

0 commit comments

Comments
 (0)