Created
March 9, 2015 06:50
-
-
Save pstuifzand/e039d41cfff59e611025 to your computer and use it in GitHub Desktop.
Git pull tool
This file contains 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 ( | |
"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