Created
November 5, 2012 15:48
-
-
Save madmo/4017904 to your computer and use it in GitHub Desktop.
Auto scp on file changes
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
// Copyright (c) 2012, Moritz Bitsch <moritzbitsch@googlemail.com> | |
// | |
// Permission to use, copy, modify, and/or distribute this software for any | |
// purpose with or without fee is hereby granted, provided that the above | |
// copyright notice and this permission notice appear in all copies. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
// Just a simple fs event handler, quite usable for realtime directory syncronisation | |
package main | |
import ( | |
"fmt" | |
"github.com/sdegutis/go.fsevents" | |
"os" | |
"os/exec" | |
"path/filepath" | |
) | |
const matchFlags = fsevents.FlagItemCreated | fsevents.FlagItemModified | |
var ignoreMe = []string{".DS_Store", "*.swp", "*.swx", "*~"} | |
func main() { | |
var ( | |
dir, remote string | |
err error | |
) | |
if len(os.Args) != 3 { | |
fmt.Printf("Usage: %s dir remote", os.Args[0]) | |
} | |
dir, remote = os.Args[1], os.Args[2] | |
err = os.Chdir(dir) | |
if err != nil { | |
fmt.Printf("Can't change working directory: %v\n", err) | |
return | |
} | |
dir, err = os.Getwd() | |
if err != nil { | |
fmt.Printf("Can't get working directory: %v\n", err) | |
return | |
} | |
evs := fsevents.WatchPaths([]string{"."}) | |
if evs == nil { | |
fmt.Println("Can't get fsevents handle, bye") | |
return | |
} | |
for { | |
event := <-evs | |
for _, ev := range event { | |
if (ev.Flags & matchFlags) != 0 { | |
rel, err := filepath.Rel(dir, ev.Path) | |
if err == nil { | |
ignore := false | |
for _, ign := range ignoreMe { | |
if m, _ := filepath.Match(ign, filepath.Base(rel)); m { | |
ignore = true | |
break | |
} | |
} | |
if !ignore { | |
cmd := exec.Command("scp", "-r", ev.Path, remote+rel) | |
fmt.Printf("%s %s %s %s\n", "scp", "-r", ev.Path, remote+rel) | |
err = cmd.Run() | |
if err != nil { | |
fmt.Println(err) | |
} | |
} | |
} else { | |
fmt.Println(err) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment