-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutil.go
95 lines (79 loc) · 2.27 KB
/
util.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
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
)
// extractTitle extracts H1 (Title) from the body of the Post
func extractTitle(body string) (string, error) {
scanner := bufio.NewScanner(strings.NewReader(body))
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "# ") {
return line[2:], nil
}
}
if err := scanner.Err(); err != nil {
return "", err
}
return "", fmt.Errorf("no H1 header found")
}
// removeTitle removes the first occurrence of the Title from the body of the Post
func removeTitle(body, title string) string {
t := "# " + title + "\n"
return strings.Replace(body, t, "", 1)
}
// removeWhitespace loops over a slice of tags (strings) and truncates each tag
// by removing whitespace
// ie, []string{"example spaced tag", "correct_tag"} -> []string{"examplespacedtag", "correct_tag"}
func removeWhitespace(tags []string) []string {
t := make([]string, len(tags))
for i, tag := range tags {
t[i] = strings.Replace(tag, " ", "", -1)
}
return t
}
// findFilePaths gathers the file paths for specific file types
func findFilePaths(startPath string) ([]string, error) {
filePaths := make([]string, 0)
walkFn := func(path string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("error accessing file: %w", err)
}
// Check if the file is a dir
if info.IsDir() {
return nil
}
if info.Name() == "README.md" {
filePaths = append(filePaths, path)
}
return nil
}
// Walk the directory root recursively
if walkErr := filepath.Walk(startPath, walkFn); walkErr != nil {
return nil, fmt.Errorf("unable to walk directory, %w", walkErr)
}
return filePaths, nil
}
// askForConfirmation asks the user if they're sure that they want to post to a specific chain
func askForConfirmation(chainID string, keyName string) bool {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Printf("Are you sure you want interact with chain with id `%s` with key `%s`? (y/n):", chainID, keyName)
input, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Error reading input:", err)
continue
}
input = strings.TrimSpace(strings.ToLower(input))
if input == "y" {
return true
} else if input == "n" {
return false
} else {
fmt.Println("Please enter 'y' or 'n'.")
}
}
}