コード例 #1
0
ファイル: csv.go プロジェクト: kentgeorgeson/csvgo
func parseFolder(folder string) string {

	dir, err := os.Open(folder)
	lib.CheckErr(err)
	defer dir.Close()

	fi, err := dir.Stat()
	lib.CheckErr(err)

	filenames := make([]string, 0)
	if fi.IsDir() {
		fis, err := dir.Readdir(-1) // -1 means return all FileInfos
		lib.CheckErr(err)

		for _, fileinfo := range fis {
			if !fileinfo.IsDir() {
				filenames = append(filenames, fileinfo.Name())
			}
		}
	}

	log.Println("Parsing Files: ", filenames)

	// Table of Contents
	result := "# Table of Contents\n\n| Data Source | Description File | Sample File | Logic File |\n|---|---|---|\n"
	for _, file := range filenames {
		// Only process .txt and .csv files
		if lib.FileExtension(file) == ".txt" || lib.FileExtension(file) == ".csv" {
			result += "|[" + lib.FileLessExtension(file) + "](#" + lib.FileLessExtension(file) + ") |"
			result += lib.ParseDescFile(folder, file) + " | "
			// adds filename as Sample File field
			result += file + " |"
			// adds logic file if it exists
			result += lib.FileLessExtension(file) + ".logic |\n"
		}
	}
	result += "\n"

	// Parse Files
	for _, file := range filenames {
		if lib.FileExtension(file) == ".txt" {
			// Parse the data file
			log.Println(folder + "/" + file)
			result += lib.ParseDatafile(folder, file, "t", 1)
			// Parse the logic file
			log.Println(folder + "/" + file)
			result += lib.ParseLogic(folder, file)
		}
		if lib.FileExtension(file) == ".csv" {
			// Parse the data file
			log.Println(folder + "/" + file)
			result += lib.ParseDatafile(folder, file, "c", 1)
			// Parse the logic file
			log.Println(folder + "/" + file)
			result += lib.ParseLogic(folder, file)

		}
	}
	return result
}
コード例 #2
0
ファイル: csv.go プロジェクト: kentgeorgeson/csvgo
func main() {
	// command line parameters - param, default, help message
	// Folder - if provided, we will parse the entire folder, if we are provided a folder, we cannot accept a file name.
	ffolder := flag.String("d", "", "The directory to be parsed")
	// File Name - if provide, we will parse a single file.   A File name cannot be provided as well as a folder
	fname := flag.String("f", "", "The filename to be parsed")
	// Delimiter (File Only) - what delimiter do we expect in the file.  Currently we only support Tab or Comma delimited
	fdelim := flag.String("l", "c", "Delimiter: c = comma, t = tab, used only for single file operations")
	// Header Size (File Only) - If processing a single file, we can overwrite the number of rows for the header.
	fheader := flag.Int("s", 1, "Skip records for header")
	// Compiles all the command line options provided to enable us to reference with pointers.
	flag.Parse()

	// Create the initial files for configuration of all files in folder provided
	if *ffolder != "" && *fname == "" {
		initFolder(*ffolder)
		fmt.Println(parseFolder(*ffolder))
	}

	// Parse a Single File from Command Line
	if *fname != "" {
		log.Println(lib.FileExtension(*fname))
		if lib.FileExtension(*fname) == ".txt" || lib.FileExtension(*fname) == ".csv" {
			// Parse the data file
			fmt.Println(lib.ParseDatafile(*ffolder, *fname, *fdelim, *fheader))
		}
		if lib.FileExtension(*fname) == ".logic" {
			// Parse the logic file
			fmt.Println(lib.ParseLogic(*ffolder, *fname))
		}
		if lib.FileExtension(*fname) == ".flow" {
			// Parse the flow file
			fmt.Println(lib.ParseFlow(*ffolder, *fname))
		}
	}
}