Closed cuducos closed 2 years ago
$ minha-receita check
Esse comando:
--directory
0
1
Exemplo de código:
package main import ( "archive/zip" "bufio" "fmt" "os" "path/filepath" ) func main() { fails, err := CheckZipFiles() if err != nil { panic(err) } for _, fail := range fails { fmt.Println(fail) } var status int if len(fails) > 0 { status = 1 } os.Exit(status) } func CheckZipFiles() ([]string, error) { ls, err := filepath.Glob("*.zip") if err != nil { return []string{}, fmt.Errorf("error listing zip files: %w", err) } if len(ls) == 0 { return []string{}, fmt.Errorf("no zip files found") } fmt.Println(fmt.Sprintf("Checking %d files…", len(ls))) type result struct { path string err error } ch := make(chan result) defer close(ch) for _, pth := range ls { go func(pth string) { ch <- result{path: pth, err: CheckZipFile(pth)} }(pth) } fails := []string{} for range ls { r := <-ch if r.err != nil { fails = append(fails, fmt.Sprintf("%s\tFAILED with\t%s", r.path, r.err.Error())) } } return fails, nil } func CheckZipFile(pth string) error { r, err := zip.OpenReader(pth) if err != nil { return fmt.Errorf("error opening %s: %w", pth, err) } defer r.Close() for _, f := range r.File { if f.FileInfo().IsDir() { continue } r, err := f.Open() if err != nil { return fmt.Errorf("error opening %s in %s: %w", f.Name, pth, err) } s := bufio.NewScanner(r) for s.Scan() { continue } if err := s.Err(); err != nil { return fmt.Errorf("error reading %s in %s: %w", f.Name, pth, err) } r.Close() } return nil }
Em andamento na branch zip-check ; )
zip-check
O que falta:
--delete
Esse comando:
--directory
0
quando não houver erros, ou1
quando houverExemplo de código: