Esempio n. 1
0
func main() {
	logging_int := flag.Int("l", int(logging.LOG_INFO),
		"Levels 0-4 0 == None, 4 == Debug")
	log_colors_flag := flag.Bool("log_color", false, "-log_color enables log coloring(mingw/linux only)")
	settings_file := flag.String("settings", DEFAULT_PROJECT_FILE,
		"Set the settings file to a non standard file...")

	flag.Parse()
	err, log_value := logging.IntToLogLevel(*logging_int)
	if err == nil {
		logging.SetLoggingLevel(log_value)
	} else {
		PrintHelp()
		os.Exit(1)
		return
	}

	logging.SetColorEnabled(*log_colors_flag)

	args := flag.Args()

	if len(args) < 1 {
		logging.Info("Need a subcommand")
		PrintHelp()
		os.Exit(1)
		return
	}

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		for {
			select {
			case <-c:
				os.Exit(0)
			}
		}
	}()

	logging.Debug("Using settings file: %s", *settings_file)
	projectSettings := ProcessSettings(*settings_file)
	logging.Debug("Settings found: %s", projectSettings)
	for _, cmd := range commands {
		if cmd.Name() == args[0] && cmd.Runnable() {
			cmd.Flag.Usage = func() { cmd.Usage() }
			for i, s := range args {
				logging.Debug("Left Args: %d:%s", i, s)
			}
			if cmd.CustomFlags {
				args = args[0:]
			} else if len(args) > 2 {
				cmd.Flag.Parse(args[0:])
				args = cmd.Flag.Args()
			}
			cmd.settings = projectSettings
			cmd.Run(cmd, args)
			return
		}
	}
}
Esempio n. 2
0
func main() {
	logging_int := flag.Int("l", int(logging.LOG_INFO),
		"Levels 0-4 0 == None, 4 == Debug")
	log_colors_flag := flag.Bool("log_color", false, "-log_color enables log coloring(mingw/linux only)")
	settings_file := flag.String("settings", DEFAULT_PROJECT_FILE,
		"Set the settings file to a non standard file...")
	gen_settings_flag := flag.Bool("g", false, "-g generates a default settings file if one is not found")

	flag.Parse()
	err, log_value := logging.IntToLogLevel(*logging_int)
	if err == nil {
		logging.SetLoggingLevel(log_value)
	} else {
		PrintHelp()
		os.Exit(1)
		return
	}

	logging.SetColorEnabled(*log_colors_flag)

	args := flag.Args()

	if len(args) < 1 {
		logging.Info("Need a subcommand")
		PrintHelp()
		os.Exit(1)
		return
	}

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		for {
			select {
			case <-c:
				os.Exit(0)
			}
		}
	}()

	var projectSettings ProjectSettings
	logging.Debug("Using settings file: %s", *settings_file)
	if _, err := os.Stat(*settings_file); err != nil {
		logging.Info("Did not find the settings file %s", *settings_file)
		if *settings_file != DEFAULT_PROJECT_FILE {
			logging.Fatal("Could not load non default settings file...")
			os.Exit(1)
		}

		if *gen_settings_flag {
			projectSettings, err = GenerateDefaultSettingsFile(*settings_file)
			if err != nil {
				os.Exit(1)
			}
		} else {
			os.Exit(1)
		}
	} else {
		projectSettings, err = LoadSettings(*settings_file)
		if err != nil {
			logging.Info("Failed to load settings file: %s", *settings_file)
			os.Exit(1)
		}
	}

	logging.Debug("Settings found: %s", projectSettings)
	for _, cmd := range commands {
		if cmd.Name() == args[0] && cmd.Runnable() {
			cmd.Flag.Usage = func() { cmd.Usage() }
			for i, s := range args {
				logging.Debug("Left Args: %d:%s", i, s)
			}
			if cmd.CustomFlags {
				args = args[0:]
			} else if len(args) > 2 {
				cmd.Flag.Parse(args[0:])
				args = cmd.Flag.Args()
			}
			cmd.settings = projectSettings
			cmd.Run(cmd, args)
			return
		}
	}
}