Skip to content

Commit d429551

Browse files
committed
feat: genesis enpoint validate and working toml
1 parent 1784560 commit d429551

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

models/weaveinit/run_l1_node.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -636,13 +636,15 @@ type GenesisEndpointInput struct {
636636
utils.TextInput
637637
state *RunL1NodeState
638638
question string
639+
err error
639640
}
640641

641642
func NewGenesisEndpointInput(state *RunL1NodeState) *GenesisEndpointInput {
642643
return &GenesisEndpointInput{
643644
TextInput: utils.NewTextInput(),
644645
state: state,
645646
question: "Please specify the endpoint to fetch genesis.json",
647+
err: nil,
646648
}
647649
}
648650

@@ -658,9 +660,13 @@ func (m *GenesisEndpointInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
658660
input, cmd, done := m.TextInput.Update(msg)
659661
if done {
660662
m.state.genesisEndpoint = input.Text
661-
m.state.weave.PushPreviousResponse(styles.RenderPreviousResponse(styles.DotsSeparator, m.GetQuestion(), []string{"endpoint"}, input.Text))
662-
newLoader := NewInitializingAppLoading(m.state)
663-
return newLoader, newLoader.Init()
663+
dns := utils.CleanString(input.Text)
664+
m.err = utils.ValidateURL(dns)
665+
if m.err == nil {
666+
m.state.weave.PushPreviousResponse(styles.RenderPreviousResponse(styles.DotsSeparator, m.GetQuestion(), []string{"endpoint"}, dns))
667+
newLoader := NewInitializingAppLoading(m.state)
668+
return newLoader, newLoader.Init()
669+
}
664670
}
665671
m.TextInput = input
666672
return m, cmd
@@ -671,6 +677,9 @@ func (m *GenesisEndpointInput) View() string {
671677
if !m.state.existingApp {
672678
preText += styles.RenderPrompt("There is no config/genesis.json available. You will need to enter the required information to proceed.\n", []string{"config/genesis.json"}, styles.Information)
673679
}
680+
if m.IsEntered && m.err != nil {
681+
return m.state.weave.Render() + preText + styles.RenderPrompt(m.GetQuestion(), []string{"endpoint"}, styles.Question) + m.TextInput.ViewErr(m.err)
682+
}
674683
return m.state.weave.Render() + preText + styles.RenderPrompt(m.GetQuestion(), []string{"endpoint"}, styles.Question) + m.TextInput.View()
675684
}
676685

utils/toml.go

+20-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import (
88
)
99

1010
// Utility function to clean the string by trimming spaces and removing ^M characters
11-
func cleanString(input string) string {
11+
func CleanString(input string) string {
1212
return strings.TrimSpace(strings.ReplaceAll(input, "\r", ""))
1313
}
1414

1515
// UpdateTomlValue updates a TOML file based on the provided key and value.
1616
// The key can be a field in a section (e.g., "api.enable") or a top-level field (e.g., "minimum-gas-prices").
1717
func UpdateTomlValue(filePath, key, value string) error {
18-
value = cleanString(value)
18+
value = CleanString(value)
1919
// Open the TOML file for reading
2020
file, err := os.Open(filePath)
2121
if err != nil {
@@ -83,15 +83,27 @@ func getSectionName(header string) string {
8383
return strings.Trim(header, "[]")
8484
}
8585

86-
// shouldModifyField checks if the current line should be modified.
86+
// shouldModifyField checks if the current line should be modified by splitting and trimming the key and value.
8787
func shouldModifyField(inTargetSection bool, currentSection, field, line string) bool {
88-
// If there is no section (top-level), and the line starts with the field, modify it
89-
if currentSection == "" && strings.HasPrefix(strings.TrimSpace(line), field) {
90-
return true
88+
trimmedLine := strings.TrimSpace(line)
89+
90+
// Check if the line contains the '=' delimiter
91+
if !strings.Contains(trimmedLine, "=") {
92+
// No '=' found, so we don't need to modify this line
93+
return false
94+
}
95+
96+
// Split the line by '=' into key and value pair
97+
parts := strings.SplitN(trimmedLine, "=", 2)
98+
key := strings.TrimSpace(parts[0])
99+
100+
// Check if the key matches the target field
101+
if key != field {
102+
return false
91103
}
92104

93-
// If we are in the target section and the line starts with the field, modify it
94-
if inTargetSection && strings.HasPrefix(strings.TrimSpace(line), field) {
105+
// If we are at the top-level or in the target section, return true
106+
if currentSection == "" || inTargetSection {
95107
return true
96108
}
97109

utils/validate.go

+13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"math/big"
77
"net"
8+
"net/url"
89
"regexp"
910
"strings"
1011

@@ -147,6 +148,18 @@ func ValidateMnemonic(mnemonic string) error {
147148
return nil
148149
}
149150

151+
// Function to validate if a string is a valid URL and return an error if invalid
152+
func ValidateURL(str string) error {
153+
u, err := url.Parse(str)
154+
if err != nil {
155+
return fmt.Errorf("invalid URL format: %v", err)
156+
}
157+
if u.Scheme == "" || u.Host == "" {
158+
return fmt.Errorf("URL is missing scheme or host")
159+
}
160+
return nil
161+
}
162+
150163
// IsValidDNS checks if a given string is a valid DNS name
151164
func IsValidDNS(dns string) bool {
152165
// Regular expression for validating DNS names

0 commit comments

Comments
 (0)