github.com/gofiber/fiber-cli@v0.0.3/cmd/helpers.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"io/ioutil"
     9  	"os"
    10  	"os/exec"
    11  	"path"
    12  	"path/filepath"
    13  	"time"
    14  )
    15  
    16  var (
    17  	homeDir string
    18  
    19  	execLookPath = exec.LookPath
    20  	execCommand  = exec.Command
    21  	osExit       = os.Exit
    22  )
    23  
    24  func init() {
    25  	homeDir, _ = os.UserHomeDir()
    26  }
    27  
    28  func runCmd(cmd *exec.Cmd) (err error) {
    29  
    30  	var (
    31  		stderr io.ReadCloser
    32  		stdout io.ReadCloser
    33  	)
    34  
    35  	if stderr, err = cmd.StderrPipe(); err != nil {
    36  		return
    37  	}
    38  	defer func() {
    39  		_ = stderr.Close()
    40  	}()
    41  	go func() { _, _ = io.Copy(os.Stderr, stderr) }()
    42  
    43  	if stdout, err = cmd.StdoutPipe(); err != nil {
    44  		return
    45  	}
    46  	defer func() {
    47  		_ = stdout.Close()
    48  	}()
    49  	go func() { _, _ = io.Copy(os.Stdout, stdout) }()
    50  
    51  	if err = cmd.Run(); err != nil {
    52  		err = fmt.Errorf("failed to run %s", cmd.String())
    53  	}
    54  
    55  	return
    56  }
    57  
    58  // replaces matching file patterns in a path, including subdirectories
    59  func replace(path, pattern, old, new string) error {
    60  	return filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
    61  		if err != nil {
    62  			return err
    63  		}
    64  		if info.IsDir() {
    65  			return nil
    66  		}
    67  		return replaceWalkFn(path, info, pattern, []byte(old), []byte(new))
    68  	})
    69  }
    70  
    71  func replaceWalkFn(path string, info os.FileInfo, pattern string, old, new []byte) (err error) {
    72  	var matched bool
    73  	if matched, err = filepath.Match(pattern, info.Name()); err != nil {
    74  		return
    75  	}
    76  
    77  	if matched {
    78  		var oldContent []byte
    79  		if oldContent, err = ioutil.ReadFile(filepath.Clean(path)); err != nil {
    80  			return
    81  		}
    82  
    83  		if err = ioutil.WriteFile(path, bytes.Replace(oldContent, old, new, -1), 0); err != nil {
    84  			return
    85  		}
    86  	}
    87  
    88  	return
    89  }
    90  
    91  func createFile(filePath, content string) (err error) {
    92  	var f *os.File
    93  	if f, err = os.Create(filePath); err != nil {
    94  		return
    95  	}
    96  
    97  	defer func() { _ = f.Close() }()
    98  
    99  	_, err = f.WriteString(content)
   100  
   101  	return
   102  }
   103  
   104  func formatLatency(d time.Duration) time.Duration {
   105  	switch {
   106  	case d > time.Second:
   107  		return d.Truncate(time.Second / 100)
   108  	case d > time.Millisecond:
   109  		return d.Truncate(time.Millisecond / 100)
   110  	case d > time.Microsecond:
   111  		return d.Truncate(time.Microsecond / 100)
   112  	default:
   113  		return d
   114  	}
   115  }
   116  
   117  func loadConfig() (err error) {
   118  	configFilePath := configFilePath()
   119  
   120  	if fileExist(configFilePath) {
   121  		if err = loadJson(configFilePath, &rc); err != nil {
   122  			return
   123  		}
   124  	}
   125  
   126  	return
   127  }
   128  
   129  func storeConfig() {
   130  	_ = storeJson(configFilePath(), rc)
   131  }
   132  
   133  func configFilePath() string {
   134  	if homeDir == "" {
   135  		return configName
   136  	}
   137  
   138  	return fmt.Sprintf("%s%c%s", homeDir, os.PathSeparator, configName)
   139  }
   140  
   141  var fileExist = func(filename string) bool {
   142  	if _, err := os.Stat(filename); os.IsNotExist(err) {
   143  		return false
   144  	}
   145  	return true
   146  }
   147  
   148  func storeJson(filename string, v interface{}) error {
   149  	b, err := json.MarshalIndent(v, "", "  ")
   150  	if err != nil {
   151  		return err
   152  	}
   153  
   154  	return ioutil.WriteFile(filename, b, 0600)
   155  }
   156  
   157  func loadJson(filename string, v interface{}) error {
   158  	b, err := ioutil.ReadFile(path.Clean(filename))
   159  	if err != nil {
   160  		return err
   161  	}
   162  
   163  	return json.Unmarshal(b, v)
   164  }