Skip to content

Commit 85bf4cf

Browse files
committed
feat: minitia launch check existing minitia app
1 parent 5de371d commit 85bf4cf

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

models/minitia/launch.go

+59-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package minitia
22

33
import (
4+
"fmt"
45
"os"
6+
"path/filepath"
57
"time"
68

79
tea "github.com/charmbracelet/bubbletea"
810

11+
"github.com/initia-labs/weave/styles"
912
"github.com/initia-labs/weave/utils"
1013
)
1114

@@ -27,27 +30,78 @@ func (m *ExistingMinitiaChecker) Init() tea.Cmd {
2730

2831
func WaitExistingMinitiaChecker(state *LaunchState) tea.Cmd {
2932
return func() tea.Msg {
30-
_, err := os.UserHomeDir()
33+
homeDir, err := os.UserHomeDir()
3134
if err != nil {
35+
fmt.Printf("[error] Failed to get user home directory: %v\n", err)
3236
return utils.ErrorLoading{Err: err}
3337
}
3438

35-
// TODO: Implement this
39+
minitiaPath := filepath.Join(homeDir, utils.MinitiaDirectory)
3640
time.Sleep(1500 * time.Millisecond)
37-
return utils.EndLoading{}
3841

42+
if !utils.FileOrFolderExists(minitiaPath) {
43+
state.existingMinitiaApp = false
44+
return utils.EndLoading{}
45+
} else {
46+
state.existingMinitiaApp = true
47+
return utils.EndLoading{}
48+
}
3949
}
4050
}
4151

4252
func (m *ExistingMinitiaChecker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
4353
loader, cmd := m.loading.Update(msg)
4454
m.loading = loader
4555
if m.loading.Completing {
46-
// TODO: Continue
56+
if !m.state.existingMinitiaApp {
57+
// TODO: Continue
58+
} else {
59+
return NewDeleteExistingMinitiaInput(m.state), nil
60+
}
4761
}
4862
return m, cmd
4963
}
5064

5165
func (m *ExistingMinitiaChecker) View() string {
52-
return m.state.weave.Render() + "\n" + m.loading.View()
66+
return styles.Text("🪢 For launching Minitia, once all required configurations are complete, \nit will run for a few blocks to set up neccesary components.\nPlease note that this may take a moment, and your patience is appreciated!\n\n", styles.Ivory) + m.loading.View()
67+
}
68+
69+
type DeleteExistingMinitiaInput struct {
70+
utils.TextInput
71+
state *LaunchState
72+
question string
73+
}
74+
75+
func NewDeleteExistingMinitiaInput(state *LaunchState) *DeleteExistingMinitiaInput {
76+
model := &DeleteExistingMinitiaInput{
77+
TextInput: utils.NewTextInput(),
78+
state: state,
79+
question: "Please type `delete existing minitia` to delete the .minitia folder and proceed with weave minitia launch",
80+
}
81+
model.WithPlaceholder("Type `delete existing minitia` to delete, Ctrl+C or q to keep the folder and quit this command.")
82+
model.WithValidatorFn(utils.ValidateExactString("delete existing minitia"))
83+
return model
84+
}
85+
86+
func (m *DeleteExistingMinitiaInput) GetQuestion() string {
87+
return m.question
88+
}
89+
90+
func (m *DeleteExistingMinitiaInput) Init() tea.Cmd {
91+
return nil
92+
}
93+
94+
func (m *DeleteExistingMinitiaInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
95+
input, cmd, done := m.TextInput.Update(msg)
96+
if done {
97+
// TODO: Continue
98+
}
99+
m.TextInput = input
100+
return m, cmd
101+
}
102+
103+
func (m *DeleteExistingMinitiaInput) View() string {
104+
return styles.RenderPrompt("🚨 Existing .minitia folder detected.\nTo proceed with weave minitia launch, you must confirm the deletion of the .minitia folder.\nIf you do not confirm the deletion, the command will not run, and you will be returned to the homepage.\n\n", []string{".minitia", "weave minitia launch"}, styles.Empty) +
105+
styles.Text("Please note that once you delete, all configurations, state, keys, and other data will be \n", styles.Yellow) + styles.BoldText("permanently deleted and cannot be reversed.\n", styles.Yellow) +
106+
styles.RenderPrompt(m.GetQuestion(), []string{"`delete existing minitia`", ".minitia", "weave minitia launch"}, styles.Question) + m.TextInput.View()
53107
}

models/minitia/state.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package minitia
33
import "github.com/initia-labs/weave/types"
44

55
type LaunchState struct {
6-
weave types.WeaveState
6+
weave types.WeaveState
7+
existingMinitiaApp bool
78
}
89

910
func NewLaunchState() *LaunchState {

utils/constants.go

+2
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ const (
1313

1414
RunL1NodeDarwinServiceName = "com.initia.daemon"
1515
RunL1NodeLinuxServiceName = "initia"
16+
17+
MinitiaDirectory = ".minitia"
1618
)

utils/validate.go

+9
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,12 @@ func IsValidPeerOrSeed(addresses string) error {
238238

239239
return nil
240240
}
241+
242+
func ValidateExactString(expect string) func(s string) error {
243+
return func(s string) error {
244+
if s != expect {
245+
return fmt.Errorf("please type `%s` to proceed", expect)
246+
}
247+
return nil
248+
}
249+
}

0 commit comments

Comments
 (0)