Exemple #1
0
func main() {
	flag.Parse()
	path = os.Getenv("PWD")
	if len(flag.Args()) > 1 {
		path = flag.Arg(1)
	}

	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()

	done := make(chan bool)
	updates := make(chan Update)
	go WatcherEventLoop(watcher, updates, done)

	log.Println("Watching directory", path)
	err = filepath.Walk(path, AddWatch(watcher))
	if err != nil {
		log.Fatal(err)
	}

	listeners := make(chan Listener)
	go UpdateListeners(updates, listeners)

	http.HandleFunc("/", RootFunc)
	http.HandleFunc("/md/", PageFunc)
	http.HandleFunc("/github.css", CSSFunc(githubCss))
	http.Handle("/ws/", websocket.Handler(HandleListener(listeners)))
	http.ListenAndServe(fmt.Sprintf("%s:%s", *host, *port), nil)
}
Exemple #2
0
func (s *Syncer) AddPair(left, right string, config *PairConfig) error {

	lUri, err := uri.Parse(left)
	if err != nil {
		return err
	}
	rUri, err := uri.Parse(right)
	if err != nil {
		return err
	}
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return err
	}

	p := &Pair{
		Left:     lUri,
		Right:    rUri,
		watcher:  watcher,
		progress: make(chan int64),
		handlers: make(map[fsnotify.Op][]OpHandler),
		syncer:   s,
		Config:   config,
	}
	s.Pairs = append(s.Pairs, p)
	go func(pair *Pair) {
		s.addPair <- pair
		fmt.Println("add pair", pair.Left.Uri())
	}(p)
	return nil
}
Exemple #3
0
func main() {
	flag.Parse()

	w, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatalf("Error making watcher: %v", err)
	}

	dir, err := os.Getwd()
	if err != nil {
		log.Fatalf("Error getting wd: %v", err)
	}
	for ; !exists(path.Join(dir, ".git")); dir = path.Dir(dir) {
		if dir == "/" {
			log.Fatalf("Could not find git root")
		}
	}

	filepath.Walk(dir, makeWalker(w))

	wg.Add(1)
	populateOnce(dir)

	ch := make(chan struct{}, 100) // Buffered to ensure we can multiple-increment wg for pending writes
	go doPopulate(ch, dir)

	go watchFS(ch, w)
	fmt.Printf("Listening on port %d\n", *port)
	http.HandleFunc("/", serve)
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))

}
Exemple #4
0
// MustRegisterWatcher creates a new Watcher and starts listening to
// given folders
func MustRegisterWatcher(params *Params) *Watcher {
	watchVendorStr := params.Get("watch-vendor")
	var watchVendor bool
	var err error
	if watchVendorStr != "" {
		watchVendor, err = strconv.ParseBool(watchVendorStr)
		if err != nil {
			log.Println("Wrong watch-vendor value: %s (default=false)", watchVendorStr)
		}
	}

	w := &Watcher{
		update:      make(chan struct{}),
		rootdir:     params.Get("watch"),
		watchVendor: watchVendor,
	}

	w.watcher, err = fsnotify.NewWatcher()
	if err != nil {
		log.Fatalf("Could not register watcher: %s", err)
	}

	// add folders that will be watched
	w.watchFolders()

	return w
}
Exemple #5
0
func (s *SpecInfoGatherer) watchForFileChanges() {
	s.waitGroup.Add(1)

	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		logger.APILog.Error("Error creating fileWatcher: %s", err)
	}
	defer watcher.Close()

	done := make(chan bool)
	go func() {
		for {
			select {
			case event := <-watcher.Events:
				s.handleEvent(event, watcher)
			case err := <-watcher.Errors:
				logger.APILog.Error("Error event while watching specs", err)
			}
		}
	}()

	allDirsToWatch := make([]string, 0)

	specDir := filepath.Join(config.ProjectRoot, common.SpecsDirectoryName)
	allDirsToWatch = append(allDirsToWatch, specDir)
	allDirsToWatch = append(allDirsToWatch, util.FindAllNestedDirs(specDir)...)

	for _, dir := range allDirsToWatch {
		s.addDirToFileWatcher(watcher, dir)
	}
	s.waitGroup.Done()
	<-done
}
Exemple #6
0
// NewWatcher create watcher
func NewWatcher(dirs []string) (*Watcher, error) {
	// w
	w, err := fs.NewWatcher()
	if err != nil {
		return nil, err
	}

	// listen
	log.Println("[umon]", "Watcher: watch dirs", dirs)
	for _, dir := range dirs {
		filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
			if info == nil {
				return err
			}
			if !info.IsDir() {
				return nil
			}
			w.Add(path)
			return nil
		})
	}

	// ok
	return &Watcher{
		w:    w,
		exit: make(ChanExit, 1),
	}, nil
}
Exemple #7
0
func NewNotifier(config *Config) (*Notifier, error) {
	w, err := fsnotify.NewWatcher()
	if err != nil {
		return &Notifier{}, err
	}

	return &Notifier{c: config, w: w}, nil
}
Exemple #8
0
func TestWatchWrite(t *testing.T) {

	sandbox(func(tmpDir string) {
		var err error
		var watcher *fsnotify.Watcher

		if watcher, err = fsnotify.NewWatcher(); err != nil {
			t.Fatal(fmt.Errorf("failed to create watcher: %s", err))
		}
		defer watcher.Close()

		result := make(chan interface{})
		timeout := time.Millisecond * 200
		go func() {
			for {
				select {
				case event := <-watcher.Events:
					result <- event.Op
				case err := <-watcher.Errors:
					result <- err
				case <-time.After(timeout):
					result <- fmt.Errorf("event did not occur within timeout of %v", timeout)
				}
			}
		}()

		// create file before adding to watcher
		var f *os.File
		if f, err = os.Create(filepath.Join(tmpDir, "a.txt")); err != nil {
			t.Fatal(err)
		}
		defer f.Close()

		if err = watcher.Add(tmpDir); err != nil {
			t.Fatal(err)
		}

		// perform write
		f.Write([]byte("hello"))

		// wait on result
		res := <-result

		expect := fsnotify.Write
		switch res.(type) {
		case fsnotify.Op:
			if res != fsnotify.Write {
				t.Errorf("expect: %v, result: %v", expect, res)
			}
		case error:
			t.Error(res)
		default:
			t.Fatal("unexpected response")
		}
	})

}
func main() {
	if *hotReload {
		// set up file watcher
		log.Printf("setting up file watcher for %s\n", *hooksFilePath)

		var err error

		watcher, err = fsnotify.NewWatcher()
		if err != nil {
			log.Fatal("error creating file watcher instance", err)
		}

		defer watcher.Close()

		go watchForFileChange()

		err = watcher.Add(*hooksFilePath)
		if err != nil {
			log.Fatal("error adding hooks file to the watcher", err)
		}
	}

	l := negroni.NewLogger()
	l.Logger = log.New(os.Stdout, "[webhook] ", log.Ldate|log.Ltime)

	negroniRecovery := &negroni.Recovery{
		Logger:     l.Logger,
		PrintStack: true,
		StackAll:   false,
		StackSize:  1024 * 8,
	}

	n := negroni.New(negroniRecovery, l)

	router := mux.NewRouter()

	var hooksURL string

	if *hooksURLPrefix == "" {
		hooksURL = "/{id}"
	} else {
		hooksURL = "/" + *hooksURLPrefix + "/{id}"
	}

	router.HandleFunc(hooksURL, hookHandler)

	n.UseHandler(router)

	if *secure {
		log.Printf("starting secure (https) webhook on %s:%d", *ip, *port)
		log.Fatal(http.ListenAndServeTLS(fmt.Sprintf("%s:%d", *ip, *port), *cert, *key, n))
	} else {
		log.Printf("starting insecure (http) webhook on %s:%d", *ip, *port)
		log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", *ip, *port), n))
	}

}
Exemple #10
0
// watchFile assigns a new fsnotify watcher to the file if possible.
// It it watches for any signals that lead to file change, and responds accordingly.
func (t *Tail) watchFile(newFile bool) error {
	if t.watcher != nil {
		t.watcher.Close()
	}

	var err error
	t.watcher, err = fsnotify.NewWatcher()
	if err != nil {
		return err
	}

	err = t.watcher.Add(t.fname)
	if err != nil {
		return err
	}

	go func() {
		// Start reading at the beginning of the file if new
		if newFile {
			t.readLines()
		}

		for {
			closed := false
			select {
			case evt, ok := <-t.watcher.Events:
				// Exit if the channel is closed
				if !ok {
					closed = true
					break
				}
				if evt.Op&(fsnotify.Create|fsnotify.Rename|fsnotify.Remove) != 0 {
					if err = t.openAndWatch(); err != nil {
						log.Fatalln("[FATA] open and watch failed: ", err)
					}
				}
				if evt.Op&fsnotify.Write == fsnotify.Write {
					t.readLines()
				}
			case err, ok := <-t.watcher.Errors:
				// Exit if the channel is closed
				if !ok {
					closed = true
					break
				}
				if err != nil {
					log.Println("[WARN] watcher error: ", err)
				}
			}
			if closed {
				break
			}
		}
	}()

	return nil
}
Exemple #11
0
func startWatch(watches *services.ServiceWatch, cfg services.OperationConfig) (*fsnotify.Watcher, error) {
	fmt.Printf("Watching %v paths for service %v\n", len(watches.IncludedPaths), watches.Service.GetName())
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return nil, errors.WithStack(err)
	}

	go func() {
		for {
			select {
			case event := <-watcher.Events:
				if event.Op&fsnotify.Write == fsnotify.Write {
					fmt.Printf("File edited: %v\n", event.Name)

					var wasExcluded bool
					for _, excluded := range watches.ExcludedPaths {
						if strings.HasPrefix(event.Name, excluded) {
							fmt.Println("File is under excluded path:", excluded)
							wasExcluded = true
							break
						}
					}

					if wasExcluded {
						continue
					}
					fmt.Printf("Rebuilding %v\n", watches.Service.GetName())
					err = rebuildService(watches.Service, cfg)
					if err != nil {
						fmt.Printf("Could not rebuild %v: %v\n", watches.Service.GetName(), err)
					}
				}

			case err := <-watcher.Errors:
				if err != nil {
					log.Println("error:", err)
				}
			}
		}
	}()

	for _, dir := range watches.IncludedPaths {
		err = watcher.Add(dir)
		if err != nil {
			watcher.Close()
			return nil, errors.WithStack(err)
		}
	}
	return watcher, nil
}
Exemple #12
0
func watchFFDB(fxLocation string, lastLogData []byte, errorLog *os.File) {
	fxPlaces := fxLocation + "places.sqlite"

	fxPlacesDB, err := OpenAndCheckSQLiteDB(fxPlaces)
	if err != nil {
		SomethingBroke(errorLog, err, "Did not connect to Firefox database")
	} else {
		Notify(errorLog, "Database "+fxPlaces+" opened.")
	}

	dbWatcher, err := fsnotify.NewWatcher()
	if err != nil {
		SomethingBroke(errorLog, err, "White Walkers got to the new Watcher before he could be assigned.")
	} else {
		Notify(errorLog, fmt.Sprintf("Watcher assigned to %s", fxPlaces))
	}
	defer dbWatcher.Close()
	dbWatcher.Add(fxPlaces)

	var lastLog Visit

	//This whole section exists for persistence; after logging is initiated, only writes are made, no reads.
	//By keeping a record of the last entry, we don't end up with duplicate data being sent after restart.

	err2 := json.Unmarshal([]byte(lastLogData), &lastLog)
	Notify(errorLog, "Initiating; unmarshaled last log")
	if err2 != nil {
		SomethingBroke(errorLog, err2)
	}

	var (
		lastDate int
		lastURL  string
	)

	lastDate = lastLog.Timestamp
	lastURL = lastLog.URL

	for {
		select {
		case event := <-dbWatcher.Events:
			if event.Op&fsnotify.Write == fsnotify.Write {
				lastDate, lastURL = catchDBChanges(fxPlacesDB, lastDate, lastURL, errorLog)
			}
		case err := <-dbWatcher.Errors:
			SomethingBroke(errorLog, err, "dbWatcher encountered an error and gave up in disgust")
		}
	}
}
Exemple #13
0
func NewWatcher() (*Watcher, error) {
	var w Watcher
	var err error

	w.watcher, err = fsnotify.NewWatcher()
	if err != nil {
		return nil, err
	}

	w.Events = make(chan fsnotify.Event)
	w.Errors = make(chan error)
	w.quit = make(chan bool)

	go w.watch()

	return &w, err
}
Exemple #14
0
// MustRegisterWatcher creates a new Watcher and starts listening
// given folders
func MustRegisterWatcher(params *Params) *Watcher {

	w := &Watcher{
		update:  make(chan bool),
		rootdir: params.Get("watch"),
	}

	var err error
	w.watcher, err = fsnotify.NewWatcher()
	if err != nil {
		log.Fatalf("Could not register watcher: %s", err)
	}

	// add watched paths
	w.watchFolders()

	return w
}
Exemple #15
0
func FollowFile(path string, cfg *tf.TfcatConfig) {

	if !FileExists(path) {
		fmt.Fprintf(os.Stderr, "input file '%s' does not exist.\n", path)
		os.Exit(1)
	}

	watcher, err := fsnotify.NewWatcher()
	panicOn(err)
	defer watcher.Close()

	f, err := os.Open(path)
	panicOn(err)
	// move to end for tailing
	_, err = f.Seek(0, 2)
	panicOn(err)

	err = watcher.Add(path)
	panicOn(err)

	fr := tf.NewFrameReader(f, 1024*1024)

	var frame tf.Frame

	i := int64(1)
nextFrame:
	for {
		_, _, err, _ = fr.NextFrame(&frame)
		if err != nil {
			if err == io.EOF {
				select {
				case event := <-watcher.Events:
					if event.Op&fsnotify.Write == fsnotify.Write {
						continue nextFrame
					}
				}
			}
			fmt.Fprintf(os.Stderr, "tfcat error from fr.NextFrame(): '%v'\n", err)
			os.Exit(1)
		}
		frame.DisplayFrame(os.Stdout, i, cfg.PrettyPrint, cfg.SkipPayload, cfg.Rreadable)
		i++
	}
}
Exemple #16
0
// NewTilesetIndex creates a new tileset index, but does not read the tile tilesets from disk
func NewTilesetIndex(mbtilesDir string) (tsi *TilesetIndex, err error) {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return
	}
	watcher.Add(mbtilesDir)
	cache, err := InitTileCache(filepath.Join(mbtilesDir, CacheName))
	if err != nil {
		return
	}
	tsi = &TilesetIndex{
		Path:     mbtilesDir,
		Tilesets: make(map[string]*mbtiles.Tileset),
		Events:   make(chan TsEvent),
		watcher:  watcher,
		cache:    cache,
	}
	return
}
Exemple #17
0
func (rs *RestartableServer) WatchConfig() {
	w, err := fsnotify.NewWatcher()
	if err != nil {
		glog.Fatalf("Failed to create watcher: %s", err)
	}
	defer w.Close()

	stopSignals := make(chan os.Signal, 1)
	signal.Notify(stopSignals, syscall.SIGTERM, syscall.SIGINT)

	err = w.Add(rs.configFile)
	watching, needRestart := (err == nil), false
	for {
		select {
		case <-time.After(1 * time.Second):
			if !watching {
				err = w.Add(rs.configFile)
				if err != nil {
					glog.Errorf("Failed to set up config watcher: %s", err)
				} else {
					watching, needRestart = true, true
				}
			} else if needRestart {
				rs.MaybeRestart()
				needRestart = false
			}
		case ev := <-w.Events:
			if ev.Op == fsnotify.Remove {
				glog.Warningf("Config file disappeared, serving continues")
				w.Remove(rs.configFile)
				watching, needRestart = false, false
			} else if ev.Op == fsnotify.Write {
				needRestart = true
			}
		case s := <-stopSignals:
			signal.Stop(stopSignals)
			glog.Infof("Signal: %s", s)
			rs.hs.Stop()
			rs.authServer.Stop()
			glog.Exitf("Exiting")
		}
	}
}
Exemple #18
0
func (s *Syncer) newPair(config settings.SyncConfig, source, target string, IgnoreRules []string) error {
	lURI, err := uri.Parse(source)
	if err != nil {
		return err
	}
	rURI, err := uri.Parse(target)
	if err != nil {
		return err
	}
	var m = make(map[string]bool, 6)
	for _, ignore := range IgnoreRules {
		m[ignore] = true
	}

	pair := &SyncPair{
		Left:      lURI,
		Right:     rURI,
		Config:    config,
		IgnoreMap: m,
	}
	fmt.Println("s.ignore", IgnoreRules)
	pair.watcher, err = fsnotify.NewWatcher()
	if err != nil {
		return err
	}

	if !pair.Left.IsAbs() || !pair.Right.IsAbs() {
		err = PairNotValidError{pair.Left.Abs(), pair.Right.Abs(), "Pair Uris not absolute"}
		return err
	}
	if !pair.Left.Exist() {
		err = PairNotValidError{pair.Left.Abs(), pair.Right.Abs(), "Res of left Uri not exist"}
		return err
	}

	s.SyncPairs = append(s.SyncPairs, pair)
	s.PairMap[pair.Left.Uri()] = pair.Right.Uri()
	go func(p *SyncPair) {
		p.BeginWatch()
	}(pair)

	return nil
}
Exemple #19
0
func getWatcher(rootPaths []string) (*fsnotify.Watcher, error) {

	watchPaths, err := getWatchPaths(rootPaths)
	if err != nil {
		return nil, err
	}

	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return nil, err
	}

	for _, path := range watchPaths {
		if err := watcher.Add(path); err != nil {
			return nil, err
		}
	}

	return watcher, nil
}
Exemple #20
0
func NewBuilder(c config) (*Bob, error) {
	w, err := fsn.NewWatcher()
	if err != nil {
		return nil, err
	}

	cmds := make([][]string, len(c.Script))
	for i, s := range c.Script {
		cmds[i] = strings.Split(s, " ")
	}

	return &Bob{
		w:            w,
		done:         make(chan struct{}),
		watching:     map[string]struct{}{},
		cmds:         cmds,
		ignoredItems: c.IgnoredItems,
		verbose:      c.Verbose,
	}, nil
}
Exemple #21
0
func NewBuilder(c config) (*Bob, error) {
	w, err := fsn.NewWatcher()
	if err != nil {
		return nil, err
	}

	parseCmd := func(cmd string) (c []string) {
		s := bufio.NewScanner(strings.NewReader(cmd))
		s.Split(splitFunc)
		for s.Scan() {
			c = append(c, s.Text())
		}

		// check for environment variables inside script
		if strings.Contains(cmd, "$$") {
			replaceEnv(c)
		}
		return c
	}

	buildCmds := make([][]string, len(c.Build))
	for i, s := range c.Build {
		buildCmds[i] = parseCmd(s)
	}

	runCmds := make([][]string, len(c.Run))
	for i, s := range c.Run {
		runCmds[i] = parseCmd(s)
	}

	return &Bob{
		w:            w,
		done:         make(chan struct{}),
		watching:     map[string]struct{}{},
		buildCmds:    buildCmds,
		runCmds:      runCmds,
		depWarning:   c.DepWarnning,
		ignoredItems: c.IgnoredItems,
		verbose:      c.Verbose,
	}, nil
}
Exemple #22
0
func ListenFile(path string) {
	watcher, err := fs.NewWatcher()
	if err != nil {
		return
	}
	_ = watcher.Add(path)

	defer watcher.Close()
	for {
		select {
		case ev := <-watcher.Events:
			ft := filepath.Ext(ev.Name)
			if ft != ".swp" && ft != ".swx" {
				if ev.Op&fs.Remove == fs.Remove {
					RemoveTail <- ev.Name
				} else if ev.Op&fs.Create == fs.Create {
					go tailFile(ev.Name)
				}
			}
		}
	}
}
Exemple #23
0
func main() {
	flag.Parse()

	w, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatalf("Error making watcher: %v", err)
	}

	dir, err := os.Getwd()
	if err != nil {
		log.Fatalf("Error getting wd: %v", err)
	}
	for ; !exists(path.Join(dir, ".git")); dir = path.Dir(dir) {
		if dir == "/" {
			log.Fatalf("Could not find git root")
		}
	}

	walker := makeWalker(dir, w)
	paths := []string{"api", "changelogs", "event-schemas", "scripts",
		"specification", "templating"}

	for _, p := range paths {
		filepath.Walk(path.Join(dir, p), walker)
	}

	wg.Add(1)
	populateOnce(dir)

	ch := make(chan struct{}, 100) // Buffered to ensure we can multiple-increment wg for pending writes
	go doPopulate(ch, dir)

	go watchFS(ch, w)
	fmt.Printf("Listening on port %d\n", *port)
	http.HandleFunc("/", serve)
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))

}
Exemple #24
0
func (p *SyncPair) BeginWatch() {
	var err error

	p.watcher, err = fsnotify.NewWatcher()
	p.tokens = make(chan bool, runtime.NumCPU())
	if err != nil {
		logger.Error(err.Error())
		return
	}

	err = p.Left.Walk(
		func(root, lDir uri.Uri) error {
			//fmt.Println("now: " + lDir.Abs())
			err = p.WatchLeft(lDir)
			if err != nil {
				logger.Error(err.Error())
				return nil
			}
			logger.Info("Add to watcher: " + lDir.Abs())
			p.handleCreate(lDir)
			return nil

		},
		func(root, lFile uri.Uri) error {
			//fmt.Println("now: " + lFile.Abs())
			p.handleCreate(lFile)
			return nil
		},
	)

	if err != nil {
		logger.Info(err.Error())
		return
	}

	logger.Info("Add pair : " + p.Left.Uri() + " ==> " + p.Right.Uri())
	p.loopMsg()
}
Exemple #25
0
// watchFile assigns a new fsnotify watcher to the file if possible.
// It it watches for any signals that lead to file change, and responds accordingly.
func (t *Tail) watchFile() error {
	if t.watcher != nil {
		t.watcher.Close()
	}

	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return err
	}
	t.watcher = watcher

	err = t.watcher.Add(t.fname)

	if err != nil {
		return err
	}

	go func() {
		for {
			select {
			case evt := <-t.watcher.Events:
				if evt.Op == fsnotify.Create || evt.Op == fsnotify.Rename || evt.Op == fsnotify.Remove {
					if err = t.openAndWatch(); err != nil {
						log.Fatalln("open and watch failed:", err)
					}
				}
			case err := <-t.watcher.Errors:
				if err != nil {
					log.Fatalln("Watcher err:", err)
				}
			}
		}
	}()

	return nil
}
Exemple #26
0
// WatchRuntimes watches for new running kernels. This is a placeholder.
func WatchRuntimes() error {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return err
	}
	defer watcher.Close()

	done := make(chan bool)

	go func() {
		for {
			select {
			case event := <-watcher.Events:
				log.Println("event:", event)
				if event.Op&fsnotify.Write == fsnotify.Write {
					log.Println("modified file:", event.Name)
				}
			case err := <-watcher.Errors:
				log.Println("error:", err)
			}
		}
	}()

	runtimeDir, err := RuntimeDir()
	if err != nil {
		return err
	}

	err = watcher.Add(runtimeDir)
	if err != nil {
		return err
	}
	<-done

	return err
}
Exemple #27
0
func main() {
	fmt.Println("Autoexec v1.1")
	io.WriteString(os.Stdout, "\033]0;Autoexec\007")

	if help || cmd == "" || suffix == "" {
		flag.PrintDefaults()
		return
	}
	fmt.Println(flag.Args())
	args = flag.Args()

	notifier = notificator.New(notificator.Options{
		AppName: "Autoexec",
	})

	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if path == "" {

		path, err = os.Getwd()
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
	} else {
		path, err = filepath.Abs(path)
		if err != nil {
			panic(err)
		}
		fmt.Println("Path: ", path)
	}

	filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			fmt.Println("Error", err)
			return err
		}

		if info.IsDir() && !strings.HasPrefix(path, ".") {
			matches, err := filepath.Glob(path + "/*" + suffix)
			if err != nil {
				panic(err)
			}
			if len(matches) > 0 {
				if verbose {
					fmt.Println("Adding  ", path)
				}
				watcher.Add(path)
			} else {
				if verbose {
					fmt.Println("Skipping", path)
				}
			}
			return nil
		}

		return nil
	})

	defer watcher.Close()

	signal := make(chan struct{})

	// start our worker routine
	go startExec(signal, cmd, args)

	fmt.Println("Monitoring", path, suffix)

	for {
		select {
		case ev := <-watcher.Events:
			if strings.HasSuffix(ev.Name, suffix) {
				if verbose {
					fmt.Printf("[%d] %v\n", time.Now().UnixNano(), ev.Name)
				}
				signal <- struct{}{}
			}

		case err := <-watcher.Errors:
			fmt.Println(err)
		}
	}
}
Exemple #28
0
func debug() {
	// fetch all go dependencies
	if nil != settings.Dependencies {
		golang.FetchDependencies(*settings.Dependencies)
	}

	regen()

	if nil != settings.StaticDir {
		watcher, err := fsnotify.NewWatcher()
		if nil != err {
			log.Fatalf("Unable to setup source file monitor. Error: %s", err)
		}
		defer watcher.Close()

		var artifactDir *string
		staticDir := filepath.Join(workingDir, *settings.StaticDir)
		if nil != settings.ClosureArtifacts {
			a := filepath.Join(staticDir, settings.ClosureArtifacts.ArtifactDir)
			artifactDir = &a
		}

		go func() {
			for {
				select {
				case event := <-watcher.Events:
					path := event.Name
					if nil != settings.ClosureArtifacts && strings.HasPrefix(path, *artifactDir) {
						continue
					}
					skip := false
					for _, r := range FileWatchExcludes {
						if r.MatchString(path) {
							skip = true
							break
						}
					}
					if skip {
						continue
					}
					log.Printf("File '%s' changed, regenerating closure artifacts",
						path)
					log.Printf("Change type: %d", event.Op)
					regen()
				}
			}
		}()

		if err = filepath.Walk(staticDir, func(path string, info os.FileInfo, err error) error {
			if nil != err {
				return nil
			}
			if !info.IsDir() {
				return nil
			}
			if nil != artifactDir && strings.HasPrefix(path, *artifactDir) {
				return nil
			}
			for _, r := range FileWatchExcludes {
				if r.MatchString(path) {
					return nil
				}
			}
			log.Printf("Watching: %s", path)
			watcher.Add(path)
			return nil
		}); nil != err {
			log.Fatalf("Unable to set up static resource watching. Error: %s", err)
		}
	}

	if err := appengine.DevServer(workingDir); nil != err {
		log.Fatalf("Error starting Google App Engine dev server. Error: %s",
			err)
	}
}
Exemple #29
0
func main() {
	hooks = hook.Hooks{}

	flag.Var(&responseHeaders, "header", "response header to return, specified in format name=value, use multiple times to set multiple headers")

	flag.Parse()

	log.SetPrefix("[webhook] ")
	log.SetFlags(log.Ldate | log.Ltime)

	if !*verbose {
		log.SetOutput(ioutil.Discard)
	}

	log.Println("version " + version + " starting")

	// set os signal watcher
	setupSignals()

	// load and parse hooks
	log.Printf("attempting to load hooks from %s\n", *hooksFilePath)

	err := hooks.LoadFromFile(*hooksFilePath)

	if err != nil {
		if !*verbose && !*noPanic {
			log.SetOutput(os.Stdout)
			log.Fatalf("couldn't load any hooks from file! %+v\naborting webhook execution since the -verbose flag is set to false.\nIf, for some reason, you want webhook to start without the hooks, either use -verbose flag, or -nopanic", err)
		}

		log.Printf("couldn't load hooks from file! %+v\n", err)
	} else {
		log.Printf("loaded %d hook(s) from file\n", len(hooks))

		for _, hook := range hooks {
			log.Printf("\t> %s\n", hook.ID)
		}
	}

	if *hotReload {
		// set up file watcher
		log.Printf("setting up file watcher for %s\n", *hooksFilePath)

		var err error

		watcher, err = fsnotify.NewWatcher()
		if err != nil {
			log.Fatal("error creating file watcher instance", err)
		}

		defer watcher.Close()

		go watchForFileChange()

		err = watcher.Add(*hooksFilePath)
		if err != nil {
			log.Fatal("error adding hooks file to the watcher", err)
		}
	}

	l := negroni.NewLogger()
	l.Logger = log.New(os.Stderr, "[webhook] ", log.Ldate|log.Ltime)

	negroniRecovery := &negroni.Recovery{
		Logger:     l.Logger,
		PrintStack: true,
		StackAll:   false,
		StackSize:  1024 * 8,
	}

	n := negroni.New(negroniRecovery, l)

	router := mux.NewRouter()

	var hooksURL string

	if *hooksURLPrefix == "" {
		hooksURL = "/{id}"
	} else {
		hooksURL = "/" + *hooksURLPrefix + "/{id}"
	}

	router.HandleFunc(hooksURL, hookHandler)

	n.UseHandler(router)

	if *secure {
		log.Printf("starting secure (https) webhook on %s:%d", *ip, *port)
		log.Fatal(http.ListenAndServeTLS(fmt.Sprintf("%s:%d", *ip, *port), *cert, *key, n))
	} else {
		log.Printf("starting insecure (http) webhook on %s:%d", *ip, *port)
		log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", *ip, *port), n))
	}

}
Exemple #30
0
func main() {
	parser := flags.NewParser(&opts, flags.Default)
	parser.Usage = "[OPTIONS] sourcedir outputdir" + parser.Usage

	args, err := parser.Parse()
	if err != nil {
		panic(err)
	}

	if len(args) != 2 {
		parser.WriteHelp(os.Stderr)
		os.Exit(1)
	}

	// TODO: this need to be clean up
	directoryExists := func(dir string) bool {
		if fi, err := os.Stat(dir); err == nil {
			return fi.IsDir()
		}
		return false
	}
	sourcedir := args[0]
	if !directoryExists(sourcedir) {
		fmt.Fprintf(os.Stderr, "The sourcedir must be an existent directory!")
		os.Exit(1)
	}
	outdir := args[1]
	if !directoryExists(outdir) {
		if err := os.Mkdir(outdir, os.ModePerm); err != nil {
			fmt.Fprintf(os.Stderr, "The outdir didn't exists and couldn't be created!")
		}
		time.Sleep(1 * time.Second) // Ugliest thing ever but it doesn't create the dir at time?
	}

	config, err := config.New(opts.Config)
	if err != nil {
		log.Panic(err)
	}
	if err := writeSite(*config, sourcedir, outdir); err != nil {
		log.Fatal(err)
	}

	if opts.StartDaemon {
		watcher, err := fsnotify.NewWatcher()
		if err != nil {
			log.Fatal(err)
		}
		defer watcher.Close()

		go func() {
			for {
				select {
				case event := <-watcher.Events:
					if event.Op != fsnotify.Chmod {
						log.Println("Rewriting the site")
						if err := writeSite(*config, sourcedir, outdir); err != nil {
							log.Fatal(err)
						}
					}
				case err := <-watcher.Errors:
					log.Fatal(err)
				}
			}
		}()

		paths, err := getAllSubdirectories(sourcedir)
		if err != nil {
			log.Fatal(err)
		}
		for _, path := range paths {
			watcher.Add(path)
		}

		addr := fmt.Sprintf("localhost:%d", opts.ServerPort)
		log.Printf("Static server running on http://%s\n", addr)
		log.Fatal(
			http.ListenAndServe(addr, http.FileServer(http.Dir(outdir))))
	}
}