This repository was archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
WIP (windows support): stub for supporting windows via fsnotify.v1 #6
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| // +build darwin | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
|
|
||
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
| 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 { | ||
| 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 | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| // +build darwin | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?