Line data Source code
1 : // Copyright 2021 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 : // of this source code is governed by a BSD-style license that can be found in 3 : // the LICENSE file. 4 : 5 : package main 6 : 7 : import ( 8 : "encoding/json" 9 : "log" 10 : "os" 11 : "path/filepath" 12 : ) 13 : 14 1 : func prettyJSON(v interface{}) []byte { 15 1 : data, err := json.MarshalIndent(v, "", "\t") 16 1 : if err != nil { 17 0 : log.Fatal(err) 18 0 : } 19 1 : return data 20 : } 21 : 22 : // walkDir recursively walks the given directory (following any symlinks it may 23 : // encounter), running the handleFn on each file. 24 1 : func walkDir(dir string, handleFn func(path, pathRel string, info os.FileInfo) error) error { 25 1 : var walkFn filepath.WalkFunc 26 1 : walkFn = func(path string, info os.FileInfo, err error) error { 27 1 : if err != nil { 28 0 : return err 29 0 : } 30 1 : if info == nil { 31 0 : return nil 32 0 : } 33 1 : if !info.Mode().IsRegular() && !info.Mode().IsDir() { 34 1 : info, err = os.Stat(path) 35 1 : if err == nil && info.Mode().IsDir() { 36 1 : _ = filepath.Walk(path+string(os.PathSeparator), walkFn) 37 1 : } 38 1 : return nil 39 : } 40 : 41 1 : rel, err := filepath.Rel(dir, path) 42 1 : if err != nil { 43 0 : return err 44 0 : } 45 : 46 1 : return handleFn(path, rel, info) 47 : } 48 1 : return filepath.Walk(dir, walkFn) 49 : }