Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions dockermachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func RunSSHCommand(machineName, command string, verbose bool) (out []byte, err e
if verbose {
fmt.Println(`docker-machine ssh ` + machineName + ` '` + command + `'`)
}
return exec.Command("/bin/sh", "-c", `docker-machine ssh `+machineName+` '`+command+`'`).CombinedOutput()
return exec.Command("sh", "-c", `docker-machine ssh `+machineName+` '`+command+`'`).CombinedOutput()
}

func GetSSHPort(machineName string) (port uint, err error) {
out, err := exec.Command("/bin/sh", "-c", `docker-machine inspect `+machineName).CombinedOutput()
out, err := exec.Command("sh", "-c", `docker-machine inspect `+machineName).CombinedOutput()
if err != nil {
return 0, err
}
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build darwin

package main

import (
Expand Down
166 changes: 166 additions & 0 deletions main_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// +build windows

package main

import (
"flag"
"fmt"
"gopkg.in/fsnotify.v1"
"log"
"os"
pathpkg "path"
"strings"
)

func main() {
pwd, err := os.Getwd()
if err != nil {
fmt.Println("error: unable to get current directory:", err)
os.Exit(1)
}

flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %v [options] DOCKER-MACHINE-NAME\n", os.Args[0])
fmt.Fprintf(os.Stderr, " or: %v [options] rsync://IP:PORT/MODULE\n", os.Args[0])
fmt.Print("\nOptions:\n")
flag.PrintDefaults()
}

var version = flag.Bool("version", false, "Print version")
var watch = flag.Bool("watch", true, "Watch source directory for changes")
var verbose = flag.Bool("verbose", false, "Verbose output")
var srcpath = flag.String("src", pwd, "Source directory")
var dstpath = flag.String("dst", pathpkg.Join("/rsync", pwd), "Destination directory")

flag.Parse()

if *version {
fmt.Println(Version)
os.Exit(0)
}

if len(flag.Args()) != 1 {
flag.Usage()
os.Exit(1)
}

via := flag.Args()[0]

if *srcpath == "" {
fmt.Println("error: please specify -src argument")
os.Exit(1)
}

if *dstpath == "" {
fmt.Println("error: please specify -dst argument")
os.Exit(1)
}

rpath := *srcpath
rpathDir := pathpkg.Dir(*dstpath)

// TODO: refactor the following part...

if strings.HasPrefix(via, "rsync://") {
// use rsync protocol directly
rsyncEndpoint := via

fmt.Printf("Syncing %s (local) to %s (%s)\n", *srcpath, *dstpath, rsyncEndpoint)
Sync(rsyncEndpoint, 0, rpath, rpathDir, *verbose) // initial sync

if *watch {
fmt.Println("Watching for file changes ...")
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()

done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
log.Println("event:", event)
if event.Op&fsnotify.Create == fsnotify.Create {
Sync(rsyncEndpoint, 0, rpath, rpathDir, true)
}
if event.Op&fsnotify.Write == fsnotify.Write {
Sync(rsyncEndpoint, 0, rpath, rpathDir, true)
}
if event.Op&fsnotify.Remove == fsnotify.Remove {
Sync(rsyncEndpoint, 0, rpath, rpathDir, true)
}
if event.Op&fsnotify.Rename == fsnotify.Rename {
Sync(rsyncEndpoint, 0, rpath, rpathDir, true)
}
case err := <-watcher.Errors:
log.Println("error:", err)
}
}
}()

err = watcher.Add("/tmp/foo")
if err != nil {
log.Fatal(err)
}
<-done
}

} else {
// use rsync via ssh
machineName := via

port, err := GetSSHPort(machineName)
if err != nil {
fmt.Printf("error: unable to get port for machine '%v': %v\n", machineName, err)
os.Exit(1)
}

Provision(machineName, *verbose)
RunSSHCommand(machineName, "sudo mkdir -p "+rpathDir, *verbose)
fmt.Printf("Syncing %s (local) to %s (docker-machine %s)\n", *srcpath, *dstpath, machineName)
Sync(machineName, port, rpath, rpathDir, *verbose) // initial sync

if *watch {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if statement is the only differing part. I also was not pleased to duplicate whole main.go - Maybe there could be a small wrapper around the actual (darwin) watch.go, which imitates fsnotify.v1 signature?

fmt.Println("Watching for file changes ...")
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()

done := make(chan bool)
go func() {
for {
select {
case event := <-watcher.Events:
log.Println("event:", event)
if event.Op&fsnotify.Create == fsnotify.Create {
Sync(machineName, port, rpath, rpathDir, true)
}
if event.Op&fsnotify.Write == fsnotify.Write {
Sync(machineName, port, rpath, rpathDir, true)
}
if event.Op&fsnotify.Remove == fsnotify.Remove {
Sync(machineName, port, rpath, rpathDir, true)
}
if event.Op&fsnotify.Rename == fsnotify.Rename {
Sync(machineName, port, rpath, rpathDir, true)
}
case err := <-watcher.Errors:
log.Println("error:", err)
}
}
}()

err = watcher.Add("/tmp/foo")
if err != nil {
log.Fatal(err)
}
<-done
}

}

}
4 changes: 2 additions & 2 deletions rsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func Sync(via string, port uint, src, dst string, verbose bool) {

command := "rsync " + strings.Join(args, " ")

// fmt.Println("/bin/sh", "-c", command)
cmd := exec.Command("/bin/sh", "-c", command)
// fmt.Println("sh", "-c", command)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this dependency on sh? Or replace it by a corresponding multiplattfrom go library?

cmd := exec.Command("sh", "-c", command)

if verbose {
cmd.Stdout = os.Stdout
Expand Down
2 changes: 2 additions & 0 deletions watch.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build darwin

package main

import (
Expand Down