Example #1
0
File: main.go Project: mattn/modd
func main() {
	kingpin.Version(watch.Version)
	kingpin.Parse()

	log := termlog.NewLog()
	if *debug {
		log.Enable("debug")
		watch.Logger = log
	}

	ret, err := ioutil.ReadFile(*file)
	if err != nil {
		kingpin.Fatalf("%s", err)
	}
	cnf, err := conf.Parse(*file, string(ret))
	if err != nil {
		kingpin.Fatalf("%s", err)
	}
	watchfile := *file
	if *noconf {
		watchfile = ""
	}

	for {
		cnf = run(log, cnf, watchfile)
		if cnf == nil {
			break
		}
	}
}
Example #2
0
func main() {
	l := termlog.NewLog()
	testpatt(l)

	g := l.Group()
	g.Say("This is a group...")
	testpatt(g)
	g.Done()

	g = l.Group()
	g.Say("Here are some composed colours...")
	g.Say(
		"%s %s %s",
		color.RedString("red"),
		color.GreenString("green"),
		color.BlueString("blue"),
	)
	g.Done()

	l.Enable("demo")
	g = l.Group()
	g.SayAs("disabled", "These don't show...")
	g.SayAs("demo", "Some named log entries...")
	g.SayAs("demo", "This is a test")
	g.SayAs("disabled", "This is a test")
	g.Done()

	g = l.Group()
	g.Say("Disabled colour output...")
	l.NoColor()
	testpatt(g)
	g.Done()
}
Example #3
0
// ServeFile replies to the request with the contents of the named file or directory.
func ServeFile(w http.ResponseWriter, r *http.Request, name string) {
	dir, file := filepath.Split(name)
	logger := termlog.NewLog()
	logger.Quiet()

	fs := FileServer{
		http.Dir(dir),
		inject.CopyInject{},
		ricetemp.MustMakeTemplates(rice.MustFindBox("../templates")),
	}
	fs.serveFile(logger, w, r, file, false)
}
Example #4
0
func TestDevdRouteHandler(t *testing.T) {
	logger := termlog.NewLog()
	logger.Quiet()
	r := Route{"", "/", fsEndpoint("./testdata")}
	templates := ricetemp.MustMakeTemplates(rice.MustFindBox("templates"))
	ci := inject.CopyInject{}

	devd := Devd{LivereloadRoutes: true}
	h := devd.WrapHandler(logger, r.Endpoint.Handler("", templates, ci))
	ht := handlerTester{t, h}

	AssertCode(t, ht.Request("GET", "/", nil), 200)
}
Example #5
0
func TestFilterFiles(t *testing.T) {
	logger := termlog.NewLog()
	logger.Quiet()
	for i, tt := range filterFilesTests {
		result := filterFiles(tt.files, []string{tt.pattern}, logger)
		if !reflect.DeepEqual(result, tt.expected) {
			t.Errorf(
				"Test %d (pattern %s), expected \"%v\" got \"%v\"",
				i, tt.pattern, tt.expected, result,
			)
		}
	}
}
Example #6
0
File: main.go Project: cortesi/modd
func main() {
	kingpin.CommandLine.HelpFlag.Short('h')
	kingpin.Version(modd.Version)
	kingpin.Parse()

	if *ignores {
		for _, patt := range modd.CommonExcludes {
			fmt.Println(patt)
		}
		os.Exit(0)
	}

	log := termlog.NewLog()
	if *debug {
		log.Enable("debug")
		moddwatch.Logger = log
	}

	notifiers := []notify.Notifier{}
	if *doNotify {
		n := notify.PlatformNotifier()
		if n == nil {
			log.Shout("Could not find a desktop notifier")
		} else {
			notifiers = append(notifiers, n)
		}
	}
	if *beep {
		notifiers = append(notifiers, &notify.BeepNotifier{})
	}

	mr, err := modd.NewModRunner(*file, log, notifiers, !(*noconf))
	if err != nil {
		log.Shout("%s", err)
		return
	}

	if *prep {
		err := mr.PrepOnly(true)
		if err != nil {
			log.Shout("%s", err)
		}
	} else {
		err = mr.Run()
		if err != nil {
			log.Shout("%s", err)
		}
	}
}
Example #7
0
func TestDevdHandler(t *testing.T) {
	logger := termlog.NewLog()
	logger.Quiet()
	templates := ricetemp.MustMakeTemplates(rice.MustFindBox("templates"))

	devd := Devd{LivereloadRoutes: true, WatchPaths: []string{"./"}}
	devd.AddRoutes([]string{"./"})
	h, err := devd.Router(logger, templates)
	if err != nil {
		t.Error(err)
	}
	ht := handlerTester{t, h}

	AssertCode(t, ht.Request("GET", "/", nil), 200)
	AssertCode(t, ht.Request("GET", "/nonexistent", nil), 404)
}
Example #8
0
File: devd.go Project: eticzon/devd
func main() {
	address := kingpin.Flag("address", "Address to listen on").
		Short('A').
		Default("127.0.0.1").
		String()

	allInterfaces := kingpin.Flag("all", "Listen on all addresses").
		Short('a').
		Bool()

	certFile := kingpin.Flag("cert", "Certificate bundle file - enables TLS").
		Short('c').
		PlaceHolder("PATH").
		ExistingFile()

	forceColor := kingpin.Flag("color", "Enable colour output, even if devd is not connected to a terminal").
		Short('C').
		Bool()

	downKbps := kingpin.Flag(
		"down",
		"Throttle downstream from the client to N kilobytes per second",
	).
		PlaceHolder("N").
		Short('d').
		Default("0").
		Uint()

	logHeaders := kingpin.Flag("logheaders", "Log headers").
		Short('H').
		Default("false").
		Bool()

	ignoreLogs := kingpin.Flag(
		"ignore",
		"Disable logging matching requests. Regexes are matched over 'host/path'",
	).
		Short('I').
		PlaceHolder("REGEX").
		Strings()

	livereloadNaked := kingpin.Flag("livereload", "Enable livereload").
		Short('L').
		Default("false").
		Bool()

	livereloadRoutes := kingpin.Flag("livewatch", "Enable livereload and watch for static file changes").
		Short('l').
		Default("false").
		Bool()

	moddMode := kingpin.Flag("modd", "Modd is our parent - synonym for -LCt").
		Short('m').
		Bool()

	latency := kingpin.Flag("latency", "Add N milliseconds of round-trip latency").
		PlaceHolder("N").
		Short('n').
		Default("0").
		Int()

	openBrowser := kingpin.Flag("open", "Open browser window on startup").
		Short('o').
		Default("false").
		Bool()

	port := kingpin.Flag(
		"port",
		"Port to listen on - if not specified, devd will auto-pick a sensible port",
	).
		Short('p').
		Int()

	credspec := kingpin.Flag(
		"password",
		"HTTP basic password protection",
	).
		PlaceHolder("USER:PASS").
		Short('P').
		String()

	quiet := kingpin.Flag("quiet", "Silence all logs").
		Short('q').
		Default("false").
		Bool()

	tls := kingpin.Flag("tls", "Serve TLS with auto-generated self-signed certificate (~/.devd.crt)").
		Short('s').
		Default("false").
		Bool()

	noTimestamps := kingpin.Flag("notimestamps", "Disable timestamps in output").
		Short('t').
		Default("false").
		Bool()

	logTime := kingpin.Flag("logtime", "Log timing").
		Short('T').
		Default("false").
		Bool()

	upKbps := kingpin.Flag(
		"up",
		"Throttle upstream from the client to N kilobytes per second",
	).
		PlaceHolder("N").
		Short('u').
		Default("0").
		Uint()

	watch := kingpin.Flag("watch", "Watch path to trigger livereload").
		PlaceHolder("PATH").
		Short('w').
		Strings()

	cors := kingpin.Flag("crossdomain", "Set the CORS header Access-Control-Allowed: *").
		Short('X').
		Default("false").
		Bool()

	excludes := kingpin.Flag("exclude", "Glob pattern for files to exclude from livereload").
		PlaceHolder("PATTERN").
		Short('x').
		Strings()

	debug := kingpin.Flag("debug", "Debugging for devd development").
		Default("false").
		Bool()

	routes := kingpin.Arg(
		"route",
		`Routes have the following forms:
			[SUBDOMAIN]/<PATH>=<DIR>
			[SUBDOMAIN]/<PATH>=<URL>
			<DIR>
			<URL>
		`,
	).Required().Strings()

	kingpin.Version(devd.Version)

	kingpin.Parse()

	if *moddMode {
		*forceColor = true
		*noTimestamps = true
		*livereloadNaked = true
	}

	realAddr := *address
	if *allInterfaces {
		realAddr = "0.0.0.0"
	}

	var creds *devd.Credentials
	if *credspec != "" {
		var err error
		creds, err = devd.CredentialsFromSpec(*credspec)
		if err != nil {
			kingpin.Fatalf("%s", err)
			return
		}
	}

	hdrs := make(http.Header)
	if *cors {
		hdrs.Set("Access-Control-Allow-Origin", "*")
	}

	dd := devd.Devd{
		// Shaping
		Latency:  *latency,
		DownKbps: *downKbps,
		UpKbps:   *upKbps,

		AddHeaders: &hdrs,

		// Livereload
		LivereloadRoutes: *livereloadRoutes,
		Livereload:       *livereloadNaked,
		WatchPaths:       *watch,
		Excludes:         *excludes,

		Credentials: creds,
	}

	if err := dd.AddRoutes(*routes); err != nil {
		kingpin.Fatalf("%s", err)
	}

	if err := dd.AddIgnores(*ignoreLogs); err != nil {
		kingpin.Fatalf("%s", err)
	}

	logger := termlog.NewLog()
	if *quiet {
		logger.Quiet()
	}
	if *debug {
		logger.Enable("debug")
	}
	if *logTime {
		logger.Enable("timer")
	}
	if *logHeaders {
		logger.Enable("headers")
	}
	if *forceColor {
		logger.Color(true)
	}
	if *noTimestamps {
		logger.TimeFmt = ""
	}

	if *tls {
		home, err := homedir.Dir()
		if err != nil {
			kingpin.Fatalf("Could not get user's homedir: %s", err)
		}
		dst := path.Join(home, ".devd.cert")
		if _, err := os.Stat(dst); os.IsNotExist(err) {
			err := devd.GenerateCert(dst)
			if err != nil {
				kingpin.Fatalf("Could not generate cert: %s", err)
			}
		}
		*certFile = dst
	}

	err := dd.Serve(
		realAddr,
		*port,
		*certFile,
		logger,
		func(url string) {
			if *openBrowser {
				err := webbrowser.Open(url)
				if err != nil {
					kingpin.Fatalf("Failed to open browser: %s", err)
				}
			}
		},
	)
	if err != nil {
		kingpin.Fatalf("%s", err)
	}
}
Example #9
0
func defaultLogger() termlog.Logger {
	l := termlog.NewLog()
	l.Quiet()
	return l
}
Example #10
0
func main() {
	kingpin.Version(modd.Version)
	kingpin.Parse()

	if *ignores {
		for _, patt := range modd.CommonExcludes {
			fmt.Println(patt)
		}
		os.Exit(0)
	}

	log := termlog.NewLog()
	if *debug {
		log.Enable("debug")
		watch.Logger = log
	}

	ret, err := ioutil.ReadFile(*file)
	if err != nil {
		kingpin.Fatalf("%s", err)
	}
	cnf, err := conf.Parse(*file, string(ret))
	if err != nil {
		kingpin.Fatalf("%s", err)
	}
	watchfile := *file
	if *noconf {
		watchfile = ""
	}

	notifiers := []notify.Notifier{}
	if *doNotify {
		n := notify.PlatformNotifier()
		if n == nil {
			log.Shout("Could not find a desktop notifier")
		} else {
			notifiers = append(notifiers, n)
		}
	}
	if *beep {
		notifiers = append(notifiers, &notify.BeepNotifier{})
	}

	if *prep {
		err := modd.PrepOnly(log, cnf, notifiers)
		if err != nil {
			log.Shout("%s", err)
		}
	} else {
		for {
			cnf.CommonExcludes(modd.CommonExcludes)
			cnf, err = modd.Run(log, cnf, watchfile, notifiers)
			if err != nil {
				log.Shout("%s", err)
				break
			}
			if cnf == nil {
				break
			}
		}
	}
}