From 79ef44e3b799370c48e8fade52a98a39ae519f6f Mon Sep 17 00:00:00 2001 From: GrailFinder Date: Sun, 12 Feb 2023 10:11:19 +0300 Subject: Feat (metadata): return empty map when no file --- main.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index a0fe70a..61e0515 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "flag" "fmt" "io/ioutil" - "log" "os" "path" "path/filepath" @@ -68,7 +67,7 @@ func linesToUtterances(lines []string, fd *FileData) []*Utterance { func readLines(filepath string) []string { file, err := os.Open(filepath) if err != nil { - log.Fatal(err) + panic(err) } defer file.Close() @@ -79,7 +78,7 @@ func readLines(filepath string) []string { } if err := scanner.Err(); err != nil { - log.Fatal(err) + panic(err) } return resp } @@ -102,11 +101,14 @@ func writeLines(lines []string, path string) error { } func readJson(filepath string) map[string]string { - plan, _ := ioutil.ReadFile(filepath) data := make(map[string]string) - err := json.Unmarshal(plan, &data) + plan, err := ioutil.ReadFile(filepath) if err != nil { - log.Fatal(err) + return data + } + err = json.Unmarshal(plan, &data) + if err != nil { + panic(err) } return data } @@ -115,7 +117,7 @@ func writeJson(data map[string]string) { metadataJson, _ := json.MarshalIndent(data, "", " ") err := ioutil.WriteFile(metadataPath, metadataJson, 0644) if err != nil { - log.Fatal(err) + panic(err) } } @@ -139,7 +141,7 @@ func utterancesToFileTextMap(utterances []*Utterance) map[string]string { func oneFileRun(filepath string) []*Utterance { fd := NewFileData(filepath) - fmt.Println("working with:", fd) + fmt.Println("working with:", filepath) lines := readLines(fd.VttPath) utterances := linesToUtterances(lines, fd) @@ -158,7 +160,7 @@ func dirRun(dirpath string) []*Utterance { resp := []*Utterance{} vttFiles := getVttList(dirpath) for _, vtt := range vttFiles { - utterances := oneFileRun(vtt) + utterances := oneFileRun(path.Join(dirpath, vtt)) resp = append(resp, utterances...) } return resp @@ -184,8 +186,8 @@ func getVttList(dirpath string) []string { } func main() { - vttFilepath := flag.String("vttfile", "", "path to a vtt file") - vttDir := flag.String("vttdir", "", "path to a vtt dir") + vttFilepath := flag.String("f", "", "path to a vtt file") + vttDir := flag.String("d", "", "path to a vtt dir") // vttFilepath := os.Args[1] flag.Parse() -- cgit v1.2.3