Example #1
0
func main() {
	var gameName string
	flag.StringVar(&gameName, "game", "", "Name of the game to store in the database")
	var databaseName string
	flag.StringVar(&databaseName, "dbname", "meeple_mover_test", "Name of the database")
	var dryRun bool
	flag.BoolVar(&dryRun, "dry-run", false, "Run without creating any records")
	flag.Parse()

	if "" == gameName {
		flag.Usage()
		os.Exit(1)
	}

	fmt.Printf("Searching for %s...\n", gameName)

	connectString := fmt.Sprintf("user=ralph dbname=%s sslmode=disable", databaseName)
	db, err := sql.Open("postgres", connectString)
	if nil != err {
		fmt.Print(err)
	}
	defer db.Close()

	shelf := collection.NewCollection().Games
	for _, game := range shelf {
		if gameName == game.Name {
			fmt.Printf("Found %s in the collection\n", gameName)
			rec := record.NewGameRecord(game)
			if !dryRun {
				err := rec.Create(db)
				if nil != err {
					fmt.Println(err)
				} else {
					fmt.Printf("Stored %s as ID %d\n", gameName, rec.Game.Id)
				}
			}
		}
	}
}
Example #2
0
func main() {
	var gameName string
	flag.StringVar(&gameName, "game", "", "Name of the game to print a dot file for")
	var useTestDB bool
	flag.BoolVar(&useTestDB, "test-db", false, "Load games from the test database")
	var verbose bool
	flag.BoolVar(&verbose, "verbose", false, "Print messages about progress to stdout")

	flag.Parse()
	if "" == gameName {
		flag.Usage()
		os.Exit(1)
	}

	if verbose {
		fmt.Printf("Searching for %s...\n", gameName)
	}
	var game *game.Game

	if useTestDB {
		if verbose {
			fmt.Println("Loading from the test database...")
		}

		connectString := fmt.Sprintf("user=ralph dbname=meeple_mover_test sslmode=disable")
		db, err := sql.Open("postgres", connectString)
		if nil != err {
			fmt.Print(err)
		}
		defer db.Close()

		rec := record.NewEmptyGameRecord()
		// FIXME: This prints messages to stdout, which breaks redirecting output straight to a dot file
		err = rec.FindByName(db, gameName)
		if nil != err {
			fmt.Println(err)
		} else {
			game = rec.Game
		}
	} else {
		if verbose {
			fmt.Println("Loading from the collection...")
		}

		shelf := collection.NewCollection().Games
		for _, g := range shelf {
			if gameName == g.Name {
				if verbose {
					fmt.Printf("Found %s in the collection\n", gameName)
				}
				game = g
				break
			}
		}
	}

	if nil != game {
		PrintDot(game)
	} else {
		fmt.Fprintf(os.Stderr, "Error: No such game")
	}
}