Ejemplo n.º 1
0
func Example_flag() {
	builder := dialect.FromCommandLine()

	flag.Parse()

	dialect, err := builder.Dialect()
	if err != nil {
		panic(err)
	}

	reader := csv.NewDialectWriter(os.Stdout, *dialect)
	reader.Write([]string{"Hello", "World"})
	reader.Flush()

	// Output:
	// Hello	World
}
Ejemplo n.º 2
0
func main() {
	dialectBuilder := dialect.FromCommandLine()

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [options] [table#1 table#2 ... table#N]\n\n", os.Args[0])
		fmt.Fprintln(os.Stderr, "If no tables are specified, all tables are dumped.\n")
		fmt.Fprintln(os.Stderr, "Options:")
		flag.PrintDefaults()
	}

	dbUser := flag.String("user", "root", "database user")
	dbPassword := flag.String("password", "", "database password")
	dbHost := flag.String("hostname", "", "database host")
	dbPort := flag.Int("port", 3306, "database port")
	outputDir := flag.String("outdir", "", "where output will be stored")
	compressFiles := flag.Bool("compress-file", false, "whether compress connection or not")
	useTransaction := flag.Bool("single-transaction", true, "whether to wrap everything in a transaction or not.")
	skipHeader := flag.Bool("skip-header", false, "whether column header should be included or not")

	flag.Parse()

	csvDialect, err := dialectBuilder.Dialect()
	if err != nil {
		fmt.Println(err.Error())
		flag.PrintDefaults()
		os.Exit(1)
	}

	args := flag.Args()
	if len(args) < 1 {
		fmt.Println("Database name must be defined.")
		flag.PrintDefaults()
		os.Exit(1)
	}
	dbName := args[0]

	dbUrl := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", *dbUser, *dbPassword, *dbHost, *dbPort, dbName)
	db, err := sql.Open("mysql", dbUrl)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Could not connect to server: %s\n", err)
	}
	defer db.Close()

	var q queryable
	if *useTransaction {
		tx, err := db.Begin()
		if err != nil {
			panic(err)
		}
		defer tx.Rollback()
		q = tx
	} else {
		q = db
	}

	var tables []string
	if len(args) > 1 {
		tables = args[1:]
	} else {
		tables, err = getTables(q)
	}

	err = dump(tables, q, *outputDir, *compressFiles, *skipHeader, csvDialect)
	if err != nil {
		panic(err)
	}
}