Android - Java Apache Config Windows Batch Linux Bash CSS Gradle HTML ini File iOS - Swift Java JavaScript JSON Format Kotlin LESS Markdown MySQL PHP Plain Text Properties File Rust Go Lang SCSS Swift XML
package main
import (
"context"
"flag"
"fmt"
"os"
"os/exec"
"time"
)
func main() {
urlPtr := flag.String("url", "", "git repo url")
branchPtr := flag.String("branch", "production", "git repo branch")
intervalPtr := flag.Int("interval", 60, "run sync every <interval> seconds")
targetPtr := flag.String("target", "", "target directory")
flag.Parse()
if *targetPtr == "" || *urlPtr == "" {
flag.Usage()
os.Exit(1)
}
for {
if _, err := os.Stat(*targetPtr); os.IsNotExist(err) {
_ = os.MkdirAll(*targetPtr, 0755)
if _, err := os.Stat(*targetPtr); os.IsNotExist(err) {
os.Exit(1)
}
runCommand(time.Minute, fmt.Sprintf("git clone -b %s %s %s", *branchPtr, *urlPtr, *targetPtr))
} else {
runCommand(time.Minute, fmt.Sprintf("git -C %s reset --hard && git -C %s pull", *targetPtr, *targetPtr))
}
time.Sleep(time.Second * time.Duration(*intervalPtr))
}
}
func runCommand(timeout time.Duration, command string) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
cmd := exec.CommandContext(ctx, "cmd", "/C", command)
out, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded {
fmt.Println("Command timed out")
return
}
fmt.Println("Output:", string(out))
if err != nil {
fmt.Println("Non-zero exit code:", err)
}
}