1// Command corpussmoke fully validates every native .atlas file in a real2// library. Legacy files are failures after the hard cutover.3package main45import (6 "flag"7 "fmt"8 "os"9 "path/filepath"1011 "github.com/FelineStateMachine/atlas/format/vnext"12)1314const (15 bundlesDirEnv = "ATLAS_BUNDLES_DIR"16 appIdentifier = "dev.felinestatemachine.atlas"17)1819func main() {20 bundles := flag.String("bundles", "", "the library of .atlas files (default $"+bundlesDirEnv+", else the application's own data directory)")21 flag.Parse()22 dir, err := libraryDir(*bundles)23 if err != nil {24 fmt.Fprintln(os.Stderr, "corpussmoke:", err)25 os.Exit(1)26 }27 failed, err := smoke(dir)28 if err != nil {29 fmt.Fprintln(os.Stderr, "corpussmoke:", err)30 os.Exit(1)31 }32 if failed > 0 {33 os.Exit(1)34 }35}3637func smoke(dir string) (failed int, err error) {38 paths, err := filepath.Glob(filepath.Join(dir, "*.atlas"))39 if err != nil {40 return 0, err41 }42 if len(paths) == 0 {43 return 0, fmt.Errorf("%s holds no .atlas files", dir)44 }45 for _, path := range paths {46 if problem := check(path); problem != nil {47 failed++48 fmt.Printf("FAIL %s\n %v\n", filepath.Base(path), problem)49 } else {50 fmt.Printf("ok %s\n", filepath.Base(path))51 }52 }53 fmt.Printf("%d native bundles: %d ok, %d failed\n", len(paths), len(paths)-failed, failed)54 return failed, nil55}5657func check(path string) error {58 reader, err := vnext.OpenFile(path, vnext.StandardSchema())59 if err != nil {60 return err61 }62 defer reader.Close()63 return reader.Validate()64}6566func libraryDir(flagged string) (string, error) {67 if flagged != "" {68 return flagged, nil69 }70 if fromEnv := os.Getenv(bundlesDirEnv); fromEnv != "" {71 return fromEnv, nil72 }73 base, err := os.UserConfigDir()74 if err != nil {75 return "", fmt.Errorf("no -bundles given and no config directory to fall back on: %w", err)76 }77 return filepath.Join(base, appIdentifier, "bundles"), nil78}