Skip to content

Instantly share code, notes, and snippets.

@pstuifzand
Created March 9, 2015 06:50
Show Gist options
  • Save pstuifzand/e039d41cfff59e611025 to your computer and use it in GitHub Desktop.
Save pstuifzand/e039d41cfff59e611025 to your computer and use it in GitHub Desktop.
Git pull tool
package main
import (
"bufio"
"io"
"log"
"net"
"os/exec"
"strings"
)
func handleOneLine(line string) string {
parts := strings.Split(line, ";")
username := parts[0]
//gitUrl := parts[1]
cmd := exec.Command("su", "-c", "git pull", username)
cmd.Dir = "/home/" + username
s, err := cmd.CombinedOutput()
if err != nil {
log.Print(err)
return "error"
}
log.Print(string(s))
log.Print("done")
return "ok"
}
func handleConnection(conn net.Conn) {
reader := bufio.NewReader(conn)
currentLine := ""
for {
line, isPrefix, err := reader.ReadLine()
if err == io.EOF {
return
}
if err != nil {
log.Print(err)
return
}
if isPrefix {
currentLine = currentLine + string(line)
} else {
currentLine = currentLine + string(line)
msg := handleOneLine(currentLine)
conn.Write([]byte(msg + "\n"))
currentLine = ""
}
}
}
func main() {
cmd := exec.Command("whoami")
s, _ := cmd.CombinedOutput()
log.Print(string(s))
ln, err := net.Listen("tcp", ":7000")
if err != nil {
log.Fatal(err)
}
for {
conn, err := ln.Accept()
if err != nil {
// handle error
}
go handleConnection(conn)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment