github.com/cockroachdb/pebble@v1.1.2/internal/mkbench/util.go (about)

     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  func prettyJSON(v interface{}) []byte {
    15  	data, err := json.MarshalIndent(v, "", "\t")
    16  	if err != nil {
    17  		log.Fatal(err)
    18  	}
    19  	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  func walkDir(dir string, handleFn func(path, pathRel string, info os.FileInfo) error) error {
    25  	var walkFn filepath.WalkFunc
    26  	walkFn = func(path string, info os.FileInfo, err error) error {
    27  		if err != nil {
    28  			return err
    29  		}
    30  		if info == nil {
    31  			return nil
    32  		}
    33  		if !info.Mode().IsRegular() && !info.Mode().IsDir() {
    34  			info, err = os.Stat(path)
    35  			if err == nil && info.Mode().IsDir() {
    36  				_ = filepath.Walk(path+string(os.PathSeparator), walkFn)
    37  			}
    38  			return nil
    39  		}
    40  
    41  		rel, err := filepath.Rel(dir, path)
    42  		if err != nil {
    43  			return err
    44  		}
    45  
    46  		return handleFn(path, rel, info)
    47  	}
    48  	return filepath.Walk(dir, walkFn)
    49  }