Esempio n. 1
0
File: cli.go Progetto: japz/go-gmr
func main() {
	parsed := kingpin.MustParse(app.Parse(os.Args[1:]))

	colog.Register()
	colog.SetOutput(os.Stderr)
	colog.SetMinLevel(colog.LInfo)
	colog.SetDefaultLevel(colog.LInfo)
	if *debug {
		colog.SetMinLevel(colog.LDebug)
	}

	switch parsed {
	case auth_cmd.FullCommand():
		if *key == "" {
			fmt.Println("API key required")
			return
		}
		resp := gmr.Authenticate(*key)
		if resp == "" {
			log.Printf("error: Authentication unsuccessful")
		} else {
			log.Printf("info: Authentication successful (%s)", resp)
		}
		gmr.GetMyGames(*key)
	case games_cmd.FullCommand():
		games := gmr.GetGames(*games...)
		switch *format {
		case "text":
			fmt.Println(games)
		case "json":
			data, _ := json.Marshal(games)
			fmt.Println(string(data))
		case "xml":
			data, _ := xml.MarshalIndent(games, "", "  ")
			fmt.Println(string(data))
		}
	case download_cmd.FullCommand():
		if *key == "" {
			fmt.Println("API key required")
			return
		}
		gmr.GetSaveFile(*key, *d_game_id, *download_to, true)
	case submit_cmd.FullCommand():
		if *key == "" {
			fmt.Println("API key required")
			return
		}
		gmr.SubmitSaveFile(*key, *s_game_id, *submit_from, true)
	}
}
Esempio n. 2
0
func init() {
	if ProjectName == "" {
		panic("no project name set - did you use the Makefile to build?")
	}

	// Set defaults
	C.Environment = "debug"
	C.Host = "localhost"
	C.Port = 3001
	C.DbType = "sqlite3"
	C.DbConn = ":memory:"

	// Load the environment
	C.Environment = os.Getenv("ENVIRONMENT")

	// Register colog to handle the standard logger output
	colog.Register()
	colog.SetFlags(0)
	colog.ParseFields(true)

	// Set up logger
	if C.IsDebug() {
		colog.SetMinLevel(colog.LDebug)
	} else {
		colog.SetMinLevel(colog.LInfo)
		colog.SetFormatter(&colog.JSONFormatter{})
	}

	// Generate a random session secret.
	buf := make([]byte, 20)
	if _, err := rand.Read(buf); err != nil {
		log.Printf("error: could not generate random secret: %s\n", err)
		os.Exit(1)
		return
	}
	C.SessionSecret = hex.EncodeToString(buf)

	// Let the user override the config file path.
	if cpath := os.Getenv(strings.ToUpper(ProjectName) + "_CONFIG_PATH"); cpath != "" {
		ConfigPath = cpath
		configPathGiven = true
	}

	// Read the configuration file, if present.
	f, err := os.Open(ConfigPath)
	if err != nil {
		// We don't print an error if the user did not give a config path, and
		// the default config file does not exist.
		if !configPathGiven && os.IsNotExist(err) {
			// Do nothing
		} else {
			log.Printf("error: could not read configuration file `%s`: %s\n", ConfigPath, err)
		}
		return
	}
	defer f.Close()

	if err := json.NewDecoder(f).Decode(C); err != nil {
		log.Printf("error: could not decode configuration file: %s\n", err)
	}
}