forked from j3ssie/osmedeus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
959 lines (859 loc) · 18.9 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
package utils
import (
"archive/zip"
"bufio"
"bytes"
"context"
"crypto/sha1"
"encoding/base64"
"fmt"
"io"
"math/rand"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
//"syscall"
"text/template"
"time"
"github.com/mitchellh/go-homedir"
)
// CalcTimeout calculate timeout
func CalcTimeout(raw string) int {
raw = strings.ToLower(strings.TrimSpace(raw))
seconds := raw
multiply := 1
matched, _ := regexp.MatchString(`.*[a-z]`, raw)
if matched {
unitTime := fmt.Sprintf("%c", raw[len(raw)-1])
seconds = raw[:len(raw)-1]
switch unitTime {
case "s":
multiply = 1
break
case "m":
multiply = 60
break
case "h":
multiply = 3600
break
}
}
timeout, err := strconv.Atoi(seconds)
if err != nil {
return 0
}
return timeout * multiply
}
// GetDomain get domain from the URL
func GetDomain(raw string) (string, error) {
u, err := url.Parse(raw)
if err == nil {
return u.Hostname(), nil
}
return raw, err
}
// EmptyDir check if directory is empty or not
func EmptyDir(dir string) bool {
if !FolderExists(dir) {
return true
}
f, err := os.Open(NormalizePath(dir))
if err != nil {
return false
}
defer f.Close()
_, err = f.Readdirnames(1)
if err == io.EOF {
return true
}
return false
}
// EmptyFile check if file is empty or not
func EmptyFile(filename string, num int) bool {
filename = NormalizePath(filename)
if !FileExists(filename) {
return true
}
data := ReadingLines(filename)
if len(data) > num {
return false
}
return true
}
// StrToInt string to int
func StrToInt(data string) int {
i, err := strconv.Atoi(data)
if err != nil {
return 0
}
return i
}
// GetOSEnv get environment variable
func GetOSEnv(name string, defaultValue string) string {
variable, ok := os.LookupEnv(name)
if !ok {
if defaultValue != "" {
return defaultValue
}
return name
}
return variable
}
// MakeDir just make a folder
func MakeDir(folder string) {
folder = NormalizePath(folder)
os.MkdirAll(folder, 0750)
}
// GetCurrentDay get current day
func GetCurrentDay() string {
currentTime := time.Now()
return fmt.Sprintf("%v", currentTime.Format("2006-01-02_3:4:5"))
}
// NormalizePath the path
func NormalizePath(path string) string {
if strings.HasPrefix(path, "~") {
path, _ = homedir.Expand(path)
}
return path
}
// GetFileContent Reading file and return content of it
func GetFileContent(filename string) string {
var result string
if strings.Contains(filename, "~") {
filename, _ = homedir.Expand(filename)
}
file, err := os.Open(filename)
if err != nil {
return result
}
defer file.Close()
b, err := io.ReadAll(file)
if err != nil {
return result
}
return string(b)
}
// ReadingLines Reading file and return content as []string
func ReadingLines(filename string) []string {
var result []string
if strings.HasPrefix(filename, "~") {
filename, _ = homedir.Expand(filename)
}
file, err := os.Open(filename)
if err != nil {
return result
}
defer file.Close()
// scanner := bufio.NewScanner(file)
// for scanner.Scan() {
// val := strings.TrimSpace(scanner.Text())
// if val == "" {
// continue
// }
// result = append(result, val)
// }
// increase the buffer size
scanner := bufio.NewScanner(file)
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
for scanner.Scan() {
val := strings.TrimSpace(scanner.Text())
if val == "" {
continue
}
result = append(result, val)
}
if err := scanner.Err(); err != nil {
return result
}
return result
}
// Cat Reading file and return content as []string
func Cat(filename string) {
filename = NormalizePath(filename)
if !FileExists(filename) {
return
}
file, err := os.Open(filename)
if err != nil {
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
fmt.Println(line)
}
return
}
// ReadingFileUnique Reading file and return content as []string
func ReadingFileUnique(filename string) []string {
var result []string
if strings.Contains(filename, "~") {
filename, _ = homedir.Expand(filename)
}
file, err := os.Open(filename)
if err != nil {
return result
}
defer file.Close()
seen := make(map[string]bool)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
val := strings.TrimSpace(scanner.Text())
// unique stuff
if val == "" {
continue
}
if seen[val] {
continue
}
seen[val] = true
result = append(result, val)
}
if err := scanner.Err(); err != nil {
return result
}
return result
}
// WriteToFile write string to a file
func WriteToFile(filename string, data string) (string, error) {
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return "", err
}
defer file.Close()
_, err = io.WriteString(file, data+"\n")
if err != nil {
return "", err
}
return filename, file.Sync()
}
// AppendToContent append string to a file
func AppendToContent(filename string, data string) (string, error) {
// If the file doesn't exist, create it, or append to the file
f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return "", err
}
if _, err := f.Write([]byte(data + "\n")); err != nil {
return "", err
}
if err := f.Close(); err != nil {
return "", err
}
return filename, nil
}
// FileExists check if file is exist or not
func FileExists(filename string) bool {
filename = NormalizePath(filename)
_, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return true
}
// FolderExists check if file is exist or not
func FolderExists(foldername string) bool {
foldername = NormalizePath(foldername)
if _, err := os.Stat(foldername); os.IsNotExist(err) {
return false
}
return true
}
// FileLength count len of file
func FileLength(filename string) int {
filename = NormalizePath(filename)
if !FileExists(filename) {
return 0
}
return CountLines(filename)
}
// DirLength count len of file
func DirLength(dir string) int {
dir = NormalizePath(dir)
files, err := os.ReadDir(dir)
if err != nil {
dir = dir + "/"
files, err = os.ReadDir(dir)
if err == nil {
return len(files)
}
return 0
}
return len(files)
}
// Copy append content to a file
func Copy(src string, dest string) {
src = NormalizePath(src)
dest = NormalizePath(dest)
if !FileExists(src) || FileLength(src) <= 0 {
return
}
input, _ := os.ReadFile(src)
os.WriteFile(dest, input, 0644)
}
// GetTS get current timestamp and return a string
func GetTS() string {
return strconv.FormatInt(time.Now().Unix(), 10)
}
// GenHash gen SHA1 hash from string
func GenHash(text string) string {
h := sha1.New()
h.Write([]byte(text))
hashed := h.Sum(nil)
return fmt.Sprintf("%x", hashed)
}
// GetFileSize get file size of a file in GB
func GetFileSize(src string) float64 {
var sizeGB float64
fi, err := os.Stat(NormalizePath(src))
if err != nil {
return sizeGB
}
// get the size
size := fi.Size()
sizeGB = float64(size) / (1024 * 1024 * 1024)
return sizeGB
}
// RandomString return a random string with length
func RandomString(n int) string {
var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))
var letter = []rune("abcdefghijklmnopqrstuvwxyz")
b := make([]rune, n)
for i := range b {
b[i] = letter[seededRand.Intn(len(letter))]
}
return string(b)
}
// runCmdWithOutput just run os command
func runCmdWithOutput(cmd string) string {
DebugF("Execute: %s", cmd)
command := []string{
"bash",
"-c",
cmd,
}
realCmd := exec.Command(command[0], command[1:]...)
// output command output to std too
output, _ := realCmd.CombinedOutput()
return string(output)
}
// RunCmdWithOutput run command with timeout
func RunCmdWithOutput(command string, timeoutRaw ...string) string {
if len(timeoutRaw) == 0 {
return runCmdWithOutput(command)
}
timeout := CalcTimeout(timeoutRaw[0])
DebugF("Run command with %v seconds timeout", timeout)
var out string
c := context.Background()
deadline := time.Now().Add(time.Duration(timeout) * time.Second)
c, cancel := context.WithDeadline(c, deadline)
defer cancel()
go func() {
out = RunCmdWithOutput(command)
cancel()
}()
select {
case <-c.Done():
return out
case <-time.After(time.Duration(timeout) * time.Second):
return out + "\n[err] command got timeout"
}
}
func runCommandWithError(cmd string) (string, error) {
DebugF("Execute: %s", cmd)
command := []string{
"bash",
"-c",
cmd,
}
var output string
realCmd := exec.Command(command[0], command[1:]...)
// output command output to std too
cmdReader, _ := realCmd.StdoutPipe()
errReader, _ := realCmd.StderrPipe()
scanner := bufio.NewScanner(cmdReader)
errScanner := bufio.NewScanner(errReader)
go func() {
for scanner.Scan() {
out := scanner.Text()
DebugF(out)
output += out + "\n"
}
}()
go func() {
for errScanner.Scan() {
DebugF(errScanner.Text())
}
}()
if err := realCmd.Start(); err != nil {
return output, err
}
if err := realCmd.Wait(); err != nil {
return output, err
}
return output, nil
}
// RunCommandWithErr Run a command
func RunCommandWithErr(command string, timeoutRaw ...string) (string, error) {
if len(timeoutRaw) == 0 {
return runCommandWithError(command)
}
var output string
var err error
timeout := CalcTimeout(timeoutRaw[0])
DebugF("Run command with %v seconds timeout", timeout)
var out string
c := context.Background()
deadline := time.Now().Add(time.Duration(timeout) * time.Second)
c, cancel := context.WithDeadline(c, deadline)
defer cancel()
go func() {
out, err = runCommandWithError(command)
cancel()
}()
select {
case <-c.Done():
return output, err
case <-time.After(time.Duration(timeout) * time.Second):
return out, fmt.Errorf("command got timeout")
}
}
func RunCommandSteamOutput(cmd string) (string, error) {
DebugF("Execute: %s", cmd)
command := []string{
"bash",
"-c",
cmd,
}
var output string
realCmd := exec.Command(command[0], command[1:]...)
// output command output to std too
cmdReader, _ := realCmd.StdoutPipe()
errReader, _ := realCmd.StderrPipe()
scanner := bufio.NewScanner(cmdReader)
errScanner := bufio.NewScanner(errReader)
go func() {
for scanner.Scan() {
out := scanner.Text()
fmt.Println(out)
output += out
}
}()
go func() {
for errScanner.Scan() {
DebugF(errScanner.Text())
}
}()
if err := realCmd.Start(); err != nil {
return output, err
}
if err := realCmd.Wait(); err != nil {
return output, err
}
return output, nil
}
func RunOSCommand(cmd string) (string, error) {
DebugF("Execute: %s", cmd)
command := []string{
"bash",
"-c",
cmd,
}
var output string
realCmd := exec.Command(command[0], command[1:]...)
// output command output to std too
cmdReader, _ := realCmd.StdoutPipe()
errReader, _ := realCmd.StderrPipe()
scanner := bufio.NewScanner(cmdReader)
errScanner := bufio.NewScanner(errReader)
go func() {
for scanner.Scan() {
out := scanner.Text()
DebugF(out)
output += out
}
}()
go func() {
for errScanner.Scan() {
DebugF(errScanner.Text())
}
}()
if err := realCmd.Start(); err != nil {
return output, err
}
if err := realCmd.Wait(); err != nil {
return output, err
}
return output, nil
}
func RunOSCommandStream(cmd, std string) error {
DebugF("Execute: %s", cmd)
command := []string{
"bash",
"-c",
cmd,
}
file, _ := os.OpenFile(std, os.O_WRONLY, os.ModeAppend)
realCmd := exec.Command(command[0], command[1:]...)
// realCmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
// output command output to std too
realCmd.Stdout = file
realCmd.Stderr = file
if err := realCmd.Start(); err != nil {
return err
}
if err := realCmd.Wait(); err != nil {
return err
}
return nil
}
// RunCommandWithoutOutput Run a command
func RunCommandWithoutOutput(cmd string) error {
command := []string{
"bash",
"-c",
cmd,
}
DebugF("[Exec] %v", command)
realCmd := exec.Command(command[0], command[1:]...)
cmdReader, _ := realCmd.StdoutPipe()
errReader, _ := realCmd.StderrPipe()
scanner := bufio.NewScanner(cmdReader)
errScanner := bufio.NewScanner(errReader)
go func() {
for scanner.Scan() {
InforF(scanner.Text())
}
}()
go func() {
for errScanner.Scan() {
DebugF(errScanner.Text())
}
}()
if err := realCmd.Start(); err != nil {
return err
}
if err := realCmd.Wait(); err != nil {
return err
}
return nil
}
// StripPath just Base64 Encode
func StripPath(raw string) string {
raw = NormalizePath(raw)
raw = strings.Replace(raw, "/", "_", -1)
raw = strings.Replace(raw, "..", "_", -1)
return raw
}
// ZippedFolder zip a folder
func ZippedFolder(src string, dest string) error {
baseDest := path.Base(dest)
if FileExists(baseDest) {
os.RemoveAll(baseDest)
}
file, err := os.Create(dest + ".zip")
if err != nil {
return err
}
defer file.Close()
w := zip.NewWriter(file)
defer w.Close()
walker := func(asbPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
file, err := os.Open(asbPath)
if err != nil {
return err
}
defer file.Close()
relPath := strings.Replace(asbPath, src, baseDest, -1)
f, err := w.Create(relPath)
if err != nil {
return err
}
_, err = io.Copy(f, file)
if err != nil {
return err
}
return nil
}
err = filepath.Walk(src, walker)
if err != nil {
return err
}
return nil
}
// CountLines Return the lines amount of the file
func CountLines(filename string) int {
var amount int
if strings.HasPrefix(filename, "~") {
filename, _ = homedir.Expand(filename)
}
file, err := os.Open(filename)
if err != nil {
return amount
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
val := strings.TrimSpace(scanner.Text())
if val == "" {
continue
}
amount++
}
if err := scanner.Err(); err != nil {
return amount
}
return amount
}
// CleanPath get environment variable
func CleanPath(raw string) string {
var out string
raw = NormalizePath(raw)
base := raw
if FileExists(base) {
base = filepath.Base(raw)
}
if strings.Count(base, "/") > 2 {
base = base[strings.LastIndex(base, "/")+1:]
if strings.TrimSpace(base) == "" {
domain, err := GetDomain(raw)
if err == nil {
base = domain
} else {
base = RandomString(8)
}
}
}
out = strings.ReplaceAll(base, "/", "_")
out = strings.ReplaceAll(out, ":", "_")
// DebugF("CleanPath: %s -- %s", raw, out)
return out
}
func IsFile(src string) bool {
fi, err := os.Stat(NormalizePath(src))
if err != nil {
return false
}
switch mode := fi.Mode(); {
case mode.IsDir():
return false
case mode.IsRegular():
if FileLength(src) > 0 {
return true
}
return false
}
return false
}
func FolderLength(dir string) int {
dir = NormalizePath(dir)
var length int
if FileExists(dir) {
dir = path.Dir(dir)
}
files, err := os.ReadDir(dir)
if err != nil {
return length
}
length = len(files)
return length
}
// ImageAsBase64 read image file as a string
func ImageAsBase64(src string) string {
src = NormalizePath(src)
f, err := os.Open(src)
if err != nil {
ErrorF("File not found: %v", src)
return ""
}
// Read entire JPG into byte slice.
reader := bufio.NewReader(f)
content, err := io.ReadAll(reader)
if err != nil {
return ""
}
// Encode as base64.
encoded := base64.StdEncoding.EncodeToString(content)
return encoded
}
// Base64Encode read image file as a string
func Base64Encode(raw string) string {
return base64.StdEncoding.EncodeToString([]byte(raw))
}
const bufSize = 1024
// OffsetRange represents a content block of a file.
type OffsetRange struct {
File string
Start int64
Stop int64
}
// SplitLineChunks splits file into chunks.
// The whole line are guaranteed to be split in the same chunk.
func SplitLineChunks(filename string, chunks int) ([]OffsetRange, error) {
info, err := os.Stat(filename)
if err != nil {
return nil, err
}
if chunks <= 1 {
return []OffsetRange{
{
File: filename,
Start: 0,
Stop: info.Size(),
},
}, nil
}
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var ranges []OffsetRange
var offset int64
// avoid the last chunk too few bytes
preferSize := info.Size()/int64(chunks) + 1
for {
if offset+preferSize >= info.Size() {
ranges = append(ranges, OffsetRange{
File: filename,
Start: offset,
Stop: info.Size(),
})
break
}
offsetRange, err := nextRange(file, offset, offset+preferSize)
if err != nil {
return nil, err
}
ranges = append(ranges, offsetRange)
if offsetRange.Stop < info.Size() {
offset = offsetRange.Stop
} else {
break
}
}
return ranges, nil
}
func nextRange(file *os.File, start, stop int64) (OffsetRange, error) {
offset, err := skipPartialLine(file, stop)
if err != nil {
return OffsetRange{}, err
}
return OffsetRange{
File: file.Name(),
Start: start,
Stop: offset,
}, nil
}
func skipPartialLine(file *os.File, offset int64) (int64, error) {
for {
skipBuf := make([]byte, bufSize)
n, err := file.ReadAt(skipBuf, offset)
if err != nil && err != io.EOF {
return 0, err
}
if n == 0 {
return 0, io.EOF
}
for i := 0; i < n; i++ {
if skipBuf[i] != '\r' && skipBuf[i] != '\n' {
offset++
} else {
for ; i < n; i++ {
if skipBuf[i] == '\r' || skipBuf[i] == '\n' {
offset++
} else {
return offset, nil
}
}
return offset, nil
}
}
}
}
// A RangeReader is used to read a range of content from a file.
type RangeReader struct {
file *os.File
start int64
stop int64
}
// NewRangeReader returns a RangeReader, which will read the range of content from file.
func NewRangeReader(file *os.File, start, stop int64) *RangeReader {
return &RangeReader{
file: file,
start: start,
stop: stop,
}
}
// Read reads the range of content into p.
func (rr *RangeReader) Read(p []byte) (n int, err error) {
stat, err := rr.file.Stat()
if err != nil {
return 0, err
}
if rr.stop < rr.start || rr.start >= stat.Size() {
return 0, fmt.Errorf("exceed file size")
}
if rr.stop-rr.start < int64(len(p)) {
p = p[:rr.stop-rr.start]
}
n, err = rr.file.ReadAt(p, rr.start)
if err != nil {
return n, err
}
rr.start += int64(n)
return
}
func Move(src string, dest string) error {
src = NormalizePath(src)
if !IsFile(src) && DirLength(src) == 0 {
return fmt.Errorf("source does not exist: %v", src)
}
dest = NormalizePath(dest)
os.RemoveAll(dest)
DebugF("Moving %v --> %v", src, dest)
return os.Rename(src, dest)
}
func IsWritable(filename string) (isWritable bool, err error) {
isWritable = false
info, err := os.Stat(filename)
if err != nil {
return
}
err = nil
if !info.IsDir() {
return
}
// Check if the user write bit is enabled in file permission
if info.Mode().Perm()&0200 == 0 {
return
}
isWritable = true
return
}
// RenderText resolve template from signature file
func RenderText(format string, data map[string]string) string {
t := template.Must(template.New("").Parse(format))
buf := &bytes.Buffer{}
err := t.Execute(buf, data)
if err != nil {
return format
}
return buf.String()
}