Skip to content

Commit c5dbdfa

Browse files
committed
wrapper upgrade
1 parent c1f76cf commit c5dbdfa

3 files changed

Lines changed: 77 additions & 9 deletions

File tree

.plugstep-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.1.2
1+
v0.1.4

cmd/plugstep/main.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,35 @@ func main() {
3838
args := flag.Args()
3939
log.Debug("Loaded args", "args", args)
4040

41-
ps := plugstep.CreatePlugstep(args, *serverDirectory)
42-
err := ps.Init()
43-
if err != nil {
41+
if len(args) < 1 {
42+
ShowVersion()
4443
return
4544
}
45+
command := args[0]
4646

47-
if len(args) < 1 {
47+
switch command {
48+
case "version", "v":
4849
ShowVersion()
4950
return
51+
case "upgrade", "u":
52+
targetVersion := ""
53+
if len(args) > 1 {
54+
targetVersion = args[1]
55+
}
56+
commands.UpgradeCommand(*serverDirectory, targetVersion)
57+
return
58+
}
59+
60+
ps := plugstep.CreatePlugstep(args, *serverDirectory)
61+
err := ps.Init()
62+
if err != nil {
63+
return
5064
}
51-
command := args[0]
5265

5366
switch command {
5467
case "install", "i":
5568
commands.InstallCommand(ps)
5669
return
57-
case "version", "v":
58-
ShowVersion()
59-
return
6070
}
6171

6272
log.Info("Unknown command", "command", command)

pkg/plugstep/commands/upgrade.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package commands
2+
3+
import (
4+
"io"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
9+
"forgejo.perny.dev/mineframe/plugstep/pkg/plugstep/utils"
10+
"github.com/charmbracelet/log"
11+
)
12+
13+
const latestVersionURL = "https://releases.perny.dev/mineframe/plugstep/latest"
14+
15+
func UpgradeCommand(serverDirectory string, targetVersion string) {
16+
versionFile := filepath.Join(serverDirectory, ".plugstep-version")
17+
18+
currentVersion := ""
19+
if data, err := os.ReadFile(versionFile); err == nil {
20+
currentVersion = strings.TrimSpace(string(data))
21+
}
22+
23+
var newVersion string
24+
if targetVersion != "" {
25+
newVersion = targetVersion
26+
} else {
27+
r, err := utils.HTTPClient.Get(latestVersionURL)
28+
if err != nil {
29+
log.Error("Failed to fetch latest version", "err", err)
30+
return
31+
}
32+
defer r.Body.Close()
33+
34+
body, err := io.ReadAll(r.Body)
35+
if err != nil {
36+
log.Error("Failed to read latest version", "err", err)
37+
return
38+
}
39+
40+
newVersion = strings.TrimSpace(string(body))
41+
}
42+
43+
if currentVersion == newVersion {
44+
log.Info("Already on version", "version", currentVersion)
45+
return
46+
}
47+
48+
if err := os.WriteFile(versionFile, []byte(newVersion), 0644); err != nil {
49+
log.Error("Failed to write version file", "err", err)
50+
return
51+
}
52+
53+
if currentVersion == "" {
54+
log.Info("Set version", "version", newVersion)
55+
} else {
56+
log.Info("Upgraded Plugstep Wrapper pinned version", "from", currentVersion, "to", newVersion)
57+
}
58+
}

0 commit comments

Comments
 (0)