-
-
Save pldubouilh/f0024bf9d1ceea5d8554fa7cac56a5d6 to your computer and use it in GitHub Desktop.
cross platform launcher program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "os" | |
| "os/exec" | |
| "strconv" | |
| "strings" | |
| "time" | |
| ) | |
| func help() { | |
| fmt.Println(`Thistle Launcher - allows to execute an update program before your main applcation | |
| syntax: ./launcher [main_application args] [update_command args](defaults "./tuc") [seconds delay between update checks](defaults 7200) | |
| e.g. ./launcher "/opt/myapp /etc/app_cfg" | |
| e.g. ./launcher "/opt/myapp /etc/app_cfg" "/opt/thistle/tuc -c /etc/thistle.conf" 3600`) | |
| os.Exit(0) | |
| } | |
| func check_update(updaterBin string, updaterArgs []string) { | |
| fmt.Println("executing", updaterBin, "args:", updaterArgs) | |
| updaterCmd := exec.Command(updaterBin, updaterArgs...) | |
| updaterCmd.Stdout = os.Stdout | |
| updaterCmd.Stderr = os.Stderr | |
| updaterCmd.Start() | |
| updaterCmd.Wait() | |
| } | |
| func main() { | |
| args := os.Args[1:] | |
| if len(args) < 1 || (len(args) == 1 && (args[0] == "--help" || args[0] == "-h" || args[0] == "help")) { | |
| help() | |
| } | |
| program := strings.Fields(args[0]) | |
| programBin := program[0] | |
| programArgs := program[1:] | |
| updaterDefault := "./tuc" | |
| var updater []string | |
| if len(args) >= 2 { | |
| updater = strings.Fields(args[1]) | |
| } else { | |
| updater = strings.Fields(updaterDefault) | |
| } | |
| updaterBin := updater[0] | |
| updaterArgs := updater[1:] | |
| delay := 7200 * time.Second | |
| if len(args) >= 3 { | |
| seconds_parsed, err := strconv.Atoi(args[2]) | |
| if err != nil { | |
| fmt.Println("Error parsing delay:", err) | |
| os.Exit(1) | |
| } | |
| delay = time.Duration(seconds_parsed) * time.Second | |
| } | |
| // check now | |
| check_update(updaterBin, updaterArgs) | |
| // and check every XXX seconds | |
| go func() { | |
| for { | |
| time.Sleep(delay) | |
| check_update(updaterBin, updaterArgs) | |
| } | |
| }() | |
| // main program | |
| fmt.Println("executing", programBin, "args:", programArgs) | |
| programCmd := exec.Command(programBin, programArgs...) | |
| programCmd.Stdout = os.Stdout | |
| programCmd.Stderr = os.Stderr | |
| programCmd.Start() | |
| programCmd.Wait() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment