示例#1
0
文件: zeus.go 项目: keathley/zeus
func main() {
	for Args = os.Args[1:]; Args != nil && len(Args) > 0 && Args[0][0] == '-'; Args = Args[1:] {
		switch Args[0] {
		case "--no-color":
			color = false
			slog.DisableColor()
		case "--log":
			tracefile, err := os.OpenFile(Args[1], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
			if err == nil {
				slog.TraceLogger = slog.NewTraceLogger(tracefile)
				Args = Args[1:]
			} else {
				fmt.Printf("Could not open trace file %s", Args[1])
				return
			}
		case "--file-change-delay":
			if len(Args) > 1 {
				delay, err := time.ParseDuration(Args[1])
				if err != nil {
					execManPage("zeus")
				}
				Args = Args[1:]
				restarter.FileChangeWindow = delay
			} else {
				execManPage("zeus")
			}
		case "--version":
			printVersion()
			return
		}
	}
	if len(Args) == 0 {
		execManPage("zeus")
	}

	if generalHelpRequested(Args) {
		execManPage("zeus")
	} else if Args[0] == "help" {
		commandSpecificHelp(Args)
	} else if Args[0] == "version" {
		printVersion()
	} else if Args[0] == "start" {
		zeusmaster.Run()
	} else if Args[0] == "init" {
		zeusInit()
	} else if Args[0] == "commands" {
		zeusCommands()
	} else {
		tree := config.BuildProcessTree()
		for _, name := range tree.AllCommandsAndAliases() {
			if Args[0] == name {
				zeusclient.Run()
				return
			}
		}

		commandNotFound(Args[0])
	}
}
示例#2
0
文件: zeus.go 项目: burke/zeus
func main() {
	args := os.Args[1:]
	configFile := "zeus.json"
	fileChangeDelay := filemonitor.DefaultFileChangeDelay

	for ; args != nil && len(args) > 0 && args[0][0] == '-'; args = args[1:] {
		switch args[0] {
		case "--no-color":
			color = false
			slog.DisableColor()
		case "--log":
			tracefile, err := os.OpenFile(args[1], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
			if err == nil {
				slog.SetTraceLogger(slog.NewTraceLogger(tracefile))
				args = args[1:]
			} else {
				fmt.Printf("Could not open trace file %s\n", args[1])
				return
			}
		case "--file-change-delay":
			if len(args) > 1 {
				delay, err := time.ParseDuration(args[1])
				if err != nil {
					execManPage("zeus")
				}
				args = args[1:]
				fileChangeDelay = delay
			} else {
				execManPage("zeus")
			}
		case "--config":
			_, err := os.Stat(args[1])
			if err != nil {
				fmt.Printf("Config file doesn't exist: %s (%e)\n", args[1], err)
				return
			}
			configFile = args[1]
			args = args[1:]
		case "--version":
			printVersion()
			return
		}
	}
	if len(args) == 0 {
		execManPage("zeus")
		return
	}

	if generalHelpRequested(args) {
		execManPage("zeus")
	} else if args[0] == "help" {
		commandSpecificHelp(args)
	} else if args[0] == "version" {
		printVersion()
	} else if args[0] == "start" {
		os.Exit(zeusmaster.Run(configFile, fileChangeDelay))
	} else if args[0] == "init" {
		zeusInit()
	} else if args[0] == "commands" {
		zeusCommands(configFile)
	} else {
		tree := config.BuildProcessTree(configFile, nil)
		for _, name := range tree.AllCommandsAndAliases() {
			if args[0] == name {
				// Don't confuse the master by sending *full* args to
				// it; just those that are not zeus-specific.
				os.Exit(zeusclient.Run(args, os.Stdin, os.Stdout))
			}
		}

		commandNotFound(args[0])
	}
}
示例#3
0
文件: zeus.go 项目: shalecraig/zeus
func main() {
	for Args = os.Args[1:]; Args != nil && len(Args) > 0 && Args[0][0] == '-'; Args = Args[1:] {
		switch Args[0] {
		case "--no-color":
			color = false
			slog.DisableColor()
		case "--log":
			tracefile, err := os.OpenFile(Args[1], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
			if err == nil {
				slog.SetTraceLogger(slog.NewTraceLogger(tracefile))
				Args = Args[1:]
			} else {
				fmt.Printf("Could not open trace file %s\n", Args[1])
				return
			}
		case "--file-change-delay":
			if len(Args) > 1 {
				delay, err := time.ParseDuration(Args[1])
				if err != nil {
					execManPage("zeus")
				}
				Args = Args[1:]
				restarter.FileChangeWindow = delay
			} else {
				execManPage("zeus")
			}
		case "--config":
			_, err := os.Stat(Args[1])
			if err != nil {
				fmt.Printf("Config file doesn't exist: %s (%e)\n", Args[1], err)
				return
			}
			config.ConfigFile = Args[1]
			Args = Args[1:]
		case "--version":
			printVersion()
			return
		}
	}
	if len(Args) == 0 {
		execManPage("zeus")
	}

	// Don't confuse the master by sending *full* args to it; just those that are
	// not zeus-specific.
	config.Args = Args

	if generalHelpRequested(Args) {
		execManPage("zeus")
	} else if Args[0] == "help" {
		commandSpecificHelp(Args)
	} else if Args[0] == "version" {
		printVersion()
	} else if Args[0] == "start" {
		zeusmaster.Run()
	} else if Args[0] == "init" {
		zeusInit()
	} else if Args[0] == "commands" {
		zeusCommands()
	} else {
		tree := config.BuildProcessTree()
		for _, name := range tree.AllCommandsAndAliases() {
			if Args[0] == name {
				zeusclient.Run()
				return
			}
		}

		commandNotFound(Args[0])
	}
}
示例#4
0
func enableTracing() {
	slog.SetTraceLogger(slog.NewTraceLogger(os.Stderr))
}
示例#5
0
func TestFileListener(t *testing.T) {
	ln, err := net.ListenTCP("tcp", &net.TCPAddr{
		IP:   net.ParseIP("127.0.0.1"),
		Port: 0,
	})
	if err != nil {
		t.Fatal(err)
	}

	slog.SetTraceLogger(slog.NewTraceLogger(os.Stderr))
	fl := filemonitor.NewFileListener(filemonitor.DefaultFileChangeDelay, ln)
	defer fl.Close()

	// We should be able to add a file without connecting anything
	if err := fl.Add("foo"); err != nil {
		t.Fatal(err)
	}

	conn, err := net.DialTCP("tcp", nil, ln.Addr().(*net.TCPAddr))
	if err != nil {
		t.Fatal(err)
	}
	defer conn.Close()
	scanner := bufio.NewScanner(conn)

	// Can write files
	files := fl.Listen()
	if err := checkWrite(files, conn); err != nil {
		t.Fatal(err)
	}

	// Can read a file add operation
	want := "bar"
	if err := fl.Add(want); err != nil {
		t.Fatal(err)
	}
	conn.SetReadDeadline(time.Now().Add(time.Second))
	if err := checkScan(scanner, want); err != nil {
		t.Fatal(err)
	}

	// Can create a second connection
	conn2, err := net.DialTCP("tcp", nil, ln.Addr().(*net.TCPAddr))
	if err != nil {
		t.Fatal(err)
	}
	defer conn2.Close()
	scanner2 := bufio.NewScanner(conn2)

	// Can write a file to the second connection
	if err := checkWrite(files, conn2); err != nil {
		t.Fatal(err)
	}

	// Can read the same file Add from two connections
	want = "baz"
	if err := fl.Add(want); err != nil {
		t.Fatal(err)
	}

	conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
	conn2.SetReadDeadline(time.Now().Add(100 * time.Millisecond))

	for i, s := range []*bufio.Scanner{scanner, scanner2} {
		if err := checkScan(s, want); err != nil {
			t.Errorf("%d: %v", i, err)
		}
	}

	// Can shutdown properly
	if err := fl.Close(); err != nil {
		t.Fatal(err)
	}

	for i, c := range []net.Conn{conn, conn2} {
		buf := make([]byte, 10)
		c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
		if b, err := c.Read(buf); err == nil {
			t.Fatalf("%d: expected EOF reading closed connection but read %d bytes: %s", i, b, buf)
		} else if err != io.EOF {
			t.Fatalf("%d: expected EOF but got %v", i, err)
		}
	}
}