feat: support curl to case

This commit is contained in:
buyuxiang
2022-07-22 18:18:21 +08:00
parent d1f0599617
commit 4b9705be22
10 changed files with 731 additions and 384 deletions
+35
View File
@@ -1,6 +1,7 @@
package builtin
import (
"bufio"
"bytes"
"encoding/csv"
builtinJSON "encoding/json"
@@ -450,6 +451,40 @@ func ReadFile(path string) ([]byte, error) {
return file, nil
}
func ReadCmdLines(path string) ([]string, error) {
var err error
path, err = filepath.Abs(path)
if err != nil {
log.Error().Err(err).Str("path", path).Msg("convert absolute path failed")
return nil, err
}
file, err := os.Open(path)
if err != nil {
log.Error().Err(err).Str("path", path).Msg("open file failed")
return nil, err
}
defer file.Close()
var line string
var lines []string
scanner := bufio.NewScanner(file)
// FIXME: resize scanner's capacity for lines over 64K
for scanner.Scan() {
text := strings.TrimSpace(scanner.Text())
if text == "" || text == "\n" {
continue
}
if strings.HasSuffix(text, "\\") {
line = line + strings.Trim(text, "\\")
continue
}
line = line + text
lines = append(lines, line)
line = ""
}
return lines, scanner.Err()
}
func GetFileNameWithoutExtension(path string) string {
base := filepath.Base(path)
ext := filepath.Ext(base)