This repository has been archived by the owner on Mar 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GiteaUpdater.go
91 lines (70 loc) · 1.81 KB
/
GiteaUpdater.go
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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
)
func main() {
fmt.Println("The Gitea Updater")
version := readVersion()
if len(version) < 2 || len(version) > 10 {
fmt.Println("Your version doesn't seem to match :(")
return
}
// Stopping Gitea service via systemctl
runCommand("systemctl", "stop", "gitea")
// Downloading the Gitea executable
path, err := DownloadGitea(version)
if err != nil {
fmt.Println("Maybe your wanted version doesn't exists?")
handleError(err)
}
fmt.Println("Path: " + path)
fmt.Println("Installing ...")
// Allowing the executable to claim ports under 1024
runCommand("chmod", "+x", path)
runCommand("chown", "git:git", path)
runCommand("setcap", "cap_net_bind_service=+ep", path)
// Creating a symlink to the new version
createSymlink(version)
// Starting Gitea service via systemctl
runCommand("systemctl", "start", "gitea")
fmt.Println("Update was sucessful!")
}
func readVersion() (version string) {
// Ask the user for a version
fmt.Print("Version (1.2.3): ")
// Read the input
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
// Removing the linefeed
text = strings.Replace(text, "\n", "", -1)
text = strings.TrimSpace(text)
return text
}
func runCommand(command string, args ...string) {
// Constructing the command
cmd := exec.Command(command, args...)
// Running it
fmt.Println("'" + command + " " + strings.Join(args, " ") + "'")
cmd.Run()
}
func createSymlink(version string) {
// If there's a old system link, we'll delete it
_, err := os.Lstat("gitea")
if err == nil {
os.Remove("gitea")
}
// Creating the new system link
err = os.Symlink("gitea-"+version, "gitea")
if err != nil {
handleError(err)
}
}
func handleError(err error) {
// Print the error
fmt.Println("\nError =>")
panic(err)
}