Пример #1
0
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
func initFolder(folder string) {

	//opens the folder and defers closing the folder until this function exits
	dir, err := os.Open(folder)
	lib.CheckErr(err)
	defer dir.Close()

	// from the folder object created, return all the files at the top level
	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())
			}
		}
	}

	// Print to screen/log files that were found
	log.Println("Initializing Folder: ", folder)

	// Table of Contents
	for _, file := range filenames {
		// Only process .txt and .csv files
		if lib.FileExtension(file) == ".txt" || lib.FileExtension(file) == ".csv" {
			// adds description file if it does not exist
			if lib.Exists(folder + "/" + lib.FileLessExtension(file) + ".desc") {
				log.Println(lib.FileLessExtension(file) + ".desc" + " found.")
			} else {
				// Create a logic file where one doesn't exist
				newLogicFile, _ := os.Create(folder + "/" + lib.FileLessExtension(file) + ".desc")
				newLogicFile.WriteString("// This is a description file, it allows you to document your analysis of specific files\n")
				log.Println("No Description File, One Created: " + newLogicFile.Name())
			}
			// adds logic file if it does not exist
			if lib.Exists(folder + "/" + lib.FileLessExtension(file) + ".logic") {
				log.Println(lib.FileLessExtension(file) + ".logic" + " found.")
			} else {
				log.Println(lib.FileLessExtension(file) + ".logic" + " not found! Template file created")
				newLogicFile, _ := os.Create(folder + "/" + lib.FileLessExtension(file) + ".logic")
				newLogicFile.WriteString("// Template file created, these are comments\n")
				newLogicFile.WriteString("# Sample Flow Name\n")
				newLogicFile.WriteString("! Sample Operator Name\n")
				newLogicFile.WriteString("- X + Y = Z Sample Calculation for Operator\n")
			}
		}
	}
	return
}
Пример #3
0
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))
		}
	}
}