Seperated the fetch script into its own file

This commit is contained in:
CapCreeperGR 2024-05-06 00:08:24 +03:00
parent d97ff3d9b6
commit ba42064d3a

46
main.go
View File

@ -10,13 +10,14 @@ import (
"strings" "strings"
) )
var configPath string = "" var configPath = ""
var fetchScriptPath = ""
var config StormfetchConfig = StormfetchConfig{} var config = StormfetchConfig{}
type StormfetchConfig struct { type StormfetchConfig struct {
Ascii string `yaml:"distro_ascii"` Ascii string `yaml:"distro_ascii"`
Fetch string `yaml:"fetch_script"` FetchScript string `yaml:"fetch_script"`
} }
type DistroInfo struct { type DistroInfo struct {
@ -31,10 +32,10 @@ func main() {
func readConfig() { func readConfig() {
// Get home directory // Get home directory
homedir, _ := os.UserConfigDir() configDir, _ := os.UserConfigDir()
// Find valid config directory // Find valid config directory
if _, err := os.Stat(path.Join(homedir, "stormfetch/config.yaml")); err == nil { if _, err := os.Stat(path.Join(configDir, "stormfetch/config.yaml")); err == nil {
configPath = path.Join(homedir, "stormfetch/config.yaml") configPath = path.Join(configDir, "stormfetch/config.yaml")
} else if _, err := os.Stat("/etc/stormfetch/config.yaml"); err == nil { } else if _, err := os.Stat("/etc/stormfetch/config.yaml"); err == nil {
configPath = "/etc/stormfetch/config.yaml" configPath = "/etc/stormfetch/config.yaml"
} else { } else {
@ -49,23 +50,26 @@ func readConfig() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if config.FetchScript == "" {
// Write fetch script to file log.Fatalf("Fetch script path is empty")
temp, err := os.CreateTemp("/tmp", "stormfetch") } else if config.FetchScript != "auto" {
stat, err := os.Stat(config.FetchScript)
if err != nil { if err != nil {
return log.Fatalf("Fetch script file not found: %s", err.Error())
} else if stat.IsDir() {
log.Fatalf("Fetch script path points to a directory")
} }
err = os.WriteFile(temp.Name(), []byte(config.Fetch), 644) }
if err != nil { if _, err := os.Stat(path.Join(configDir, "stormfetch/fetch_script.sh")); err == nil {
return fetchScriptPath = path.Join(configDir, "stormfetch/fetch_script.sh")
} else if _, err := os.Stat("/etc/stormfetch/fetch_script.sh"); err == nil {
fetchScriptPath = "/etc/stormfetch/fetch_script.sh"
} else {
log.Fatalf("Fetch script file not found: %s", err.Error())
} }
//Execute fetch script //Execute fetch script
cmd := exec.Command("/bin/sh", configPath) cmd := exec.Command("/bin/bash", fetchScriptPath)
workdir, err := os.Getwd() cmd.Dir = path.Dir(fetchScriptPath)
if err != nil {
log.Fatal(err)
}
cmd.Dir = workdir
cmd.Env = os.Environ() cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "DISTRO_LONG_NAME="+getDistroInfo().LongName) cmd.Env = append(cmd.Env, "DISTRO_LONG_NAME="+getDistroInfo().LongName)
cmd.Env = append(cmd.Env, "DISTRO_SHORT_NAME="+getDistroInfo().ShortName) cmd.Env = append(cmd.Env, "DISTRO_SHORT_NAME="+getDistroInfo().ShortName)
@ -73,10 +77,6 @@ func readConfig() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
err = os.Remove(temp.Name())
if err != nil {
return
}
// Print Distro Information // Print Distro Information
ascii := getDistroAscii() ascii := getDistroAscii()
maxWidth := 0 maxWidth := 0