refactor: clean up code and restructure

This commit is contained in:
Patryk Hegenberg 2024-12-25 08:02:52 +01:00
parent ef0e24f128
commit 03941d0d41

96
main.go
View file

@ -9,20 +9,31 @@ import (
"strings"
)
func main() {
type Config struct {
DefaultWidth string
DefaultHeight string
ArgsVersion []string
}
var config = Config{
DefaultWidth: "80%",
DefaultHeight: "80%",
ArgsVersion: []string{"--version"},
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Bitte geben Sie eine Aktion an: tmux, lazygit, harlequin, postings")
return
}
action := os.Args[1]
width := "80%"
height := "80%"
width := config.DefaultWidth
height := config.DefaultHeight
if len(os.Args) > 2 {
if len(os.Args) > 3 {
width = os.Args[2]
height = os.Args[2]
height = os.Args[3]
}
switch action {
@ -35,19 +46,22 @@ func main() {
case "postings":
openPopup("postings", "posting", width, height)
default:
CheckDependencies()
fmt.Println("Unbekannte Aktion")
if err := CheckDependencies(); err != nil {
fmt.Println("Fehler bei der Überprüfung der Abhängigkeiten:", err)
} else {
fmt.Println("Unbekannte Aktion")
}
}
}
func openPopup(sessionName, command string, width, height string) {
currentSession, err := exec.Command("tmux", "display-message", "-p", "-F", "#{session_name}").Output()
func openPopup(sessionName, command, width, height string) {
isCurrent, err := isCurrentSession(sessionName)
if err != nil {
fmt.Println("Fehler beim Abrufen der aktuellen Session:", err)
return
}
if strings.TrimSpace(string(currentSession)) == sessionName {
if isCurrent {
exec.Command("tmux", "detach-client").Run()
} else {
args := []string{"popup", "-d", "#{pane_current_path}", "-xC", "-yC", fmt.Sprintf("-w%s", width), fmt.Sprintf("-h%s", height), "-E"}
@ -56,46 +70,43 @@ func openPopup(sessionName, command string, width, height string) {
} else {
args = append(args, fmt.Sprintf("tmux attach -t %s || tmux new -s %s '%s'", sessionName, sessionName, command))
}
exec.Command("tmux", args...).Run()
if err := exec.Command("tmux", args...).Run(); err != nil {
slog.Error("Failed to execute tmux command", "error", err)
}
}
}
func isCurrentSession(sessionName string) (bool, error) {
currentSession, err := exec.Command("tmux", "display-message", "-p", "-F", "#{session_name}").Output()
if err != nil {
return false, err
}
return strings.TrimSpace(string(currentSession)) == sessionName, nil
}
func CheckDependencies() error {
deps := []string{"lazygit", "harlequin", "posting"}
args := []string{"--version"}
os, err := platformInfo()
slog.Info("OS is: ", "os", os)
if err != nil {
slog.Error("error getting os infos: ", "err", err)
}
for _, dep := range deps {
if dep != "posting" {
err := runCmd(dep, args)
if err != nil {
return err
}
} else {
err := runCmd(dep, []string{"--help"})
if err != nil {
return err
}
checkArgs := config.ArgsVersion
if dep == "posting" {
checkArgs = []string{"--help"}
}
if err := runCmd(dep, checkArgs); err != nil {
return err
}
}
return nil
}
func runCmd(dep string, args []string) error {
slog.Info("checking: ", "dep", dep)
slog.Info("Checking dependency", "dep", dep)
cmd := exec.Command(dep, args...)
var stdo, stde bytes.Buffer
cmd.Stdout = &stdo
cmd.Stderr = &stde
cmd.Env = append(os.Environ(), "LANGUAGE=en")
err := cmd.Run()
if err != nil {
slog.Error(err.Error())
if err := cmd.Run(); err != nil {
slog.Error("Command execution failed", "error", err)
return err
}
return nil
@ -107,15 +118,16 @@ func platformInfo() (string, error) {
return "", fmt.Errorf("unable to read system information")
}
osRelease, _ := os.ReadFile("/etc/os-release")
return parseOsRelease(string(osRelease)), nil
osRelease, err := os.ReadFile("/etc/os-release")
if err != nil {
return "", err
}
id, _, _ := parseOsRelease(string(osRelease))
return id, nil
}
func parseOsRelease(osRelease string) string {
var result string
func parseOsRelease(osRelease string) (id, name, version string) {
lines := strings.Split(osRelease, "\n")
for _, line := range lines {
splitLine := strings.SplitN(line, "=", 2)
if len(splitLine) != 2 {
@ -123,10 +135,12 @@ func parseOsRelease(osRelease string) string {
}
switch splitLine[0] {
case "ID":
id = strings.Trim(splitLine[1], "\"")
case "NAME":
result = strings.Trim(splitLine[1], "\"")
name = strings.Trim(splitLine[1], "\"")
case "VERSION_ID":
version = strings.Trim(splitLine[1], "\"")
}
}
return result
return
}