Skip to content

Commit 70933e7

Browse files
committed
feat: wire up initializeApp for local
1 parent 820aa35 commit 70933e7

File tree

2 files changed

+62
-52
lines changed

2 files changed

+62
-52
lines changed

models/weaveinit/run_l1_node.go

+46-52
Original file line numberDiff line numberDiff line change
@@ -693,78 +693,72 @@ func (m *InitializingAppLoading) View() string {
693693

694694
func initializeApp(state *RunL1NodeState) tea.Cmd {
695695
return func() tea.Msg {
696-
// TODO: Separate this logic depending on the network
697696
userHome, err := os.UserHomeDir()
698697
if err != nil {
699698
panic(fmt.Sprintf("failed to get user home directory: %v", err))
700699
}
700+
weaveDataPath := filepath.Join(userHome, utils.WeaveDataDirectory)
701+
tarballPath := filepath.Join(weaveDataPath, "initia.tar.gz")
701702

702-
var result map[string]interface{}
703-
err = utils.MakeGetRequest(strings.ToLower(state.network), "lcd", "/cosmos/base/tendermint/v1beta1/node_info", nil, &result)
704-
if err != nil {
705-
panic(err)
706-
}
707-
708-
if applicationVersion, ok := result["application_version"].(map[string]interface{}); ok {
709-
nodeVersion := applicationVersion["version"].(string)
710-
711-
goos := runtime.GOOS
712-
goarch := runtime.GOARCH
703+
var nodeVersion, extractedPath, binaryPath, url string
713704

714-
url := getBinaryURL(nodeVersion, goos, goarch)
715-
if url == "" {
716-
panic("unsupported OS or architecture")
705+
switch state.network {
706+
case string(Local):
707+
nodeVersion = state.initiadVersion
708+
extractedPath = filepath.Join(weaveDataPath, fmt.Sprintf("initia@%s", nodeVersion))
709+
url = state.initiadEndpoint
710+
case string(Mainnet), string(Testnet):
711+
var result map[string]interface{}
712+
err = utils.MakeGetRequest(strings.ToLower(state.network), "lcd", "/cosmos/base/tendermint/v1beta1/node_info", nil, &result)
713+
if err != nil {
714+
panic(err)
717715
}
718716

719-
weaveDataPath := filepath.Join(userHome, utils.WeaveDataDirectory)
720-
tarballPath := filepath.Join(weaveDataPath, "initia.tar.gz")
721-
extractedPath := filepath.Join(weaveDataPath, fmt.Sprintf("initia@%s", nodeVersion))
722-
binaryPath := filepath.Join(extractedPath, "initiad")
723-
724-
if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
725-
if _, err := os.Stat(extractedPath); os.IsNotExist(err) {
726-
err := os.MkdirAll(extractedPath, os.ModePerm)
727-
if err != nil {
728-
panic(fmt.Sprintf("failed to create weave data directory: %v", err))
729-
}
730-
}
731-
732-
err = utils.DownloadFile(url, tarballPath)
733-
if err != nil {
734-
panic(fmt.Sprintf("failed to download file: %v", err))
735-
}
717+
applicationVersion, ok := result["application_version"].(map[string]interface{})
718+
if !ok {
719+
panic("failed to get node version")
720+
}
736721

737-
err = utils.ExtractTarGz(tarballPath, extractedPath)
738-
if err != nil {
739-
panic(fmt.Sprintf("failed to extract tarball: %v", err))
740-
}
722+
nodeVersion = applicationVersion["version"].(string)
723+
goos := runtime.GOOS
724+
goarch := runtime.GOARCH
725+
url = getBinaryURL(nodeVersion, goos, goarch)
726+
extractedPath = filepath.Join(weaveDataPath, fmt.Sprintf("initia@%s", nodeVersion))
727+
default:
728+
panic("unsupported network")
729+
}
741730

742-
err = os.Remove(tarballPath)
731+
binaryPath = filepath.Join(extractedPath, "initiad")
732+
if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
733+
if _, err := os.Stat(extractedPath); os.IsNotExist(err) {
734+
err := os.MkdirAll(extractedPath, os.ModePerm)
743735
if err != nil {
744-
panic(fmt.Sprintf("failed to remove tarball file: %v", err))
736+
panic(fmt.Sprintf("failed to create weave data directory: %v", err))
745737
}
738+
}
746739

747-
err = os.Chmod(binaryPath, os.ModePerm)
748-
if err != nil {
749-
panic(fmt.Sprintf("failed to set permissions for binary: %v", err))
750-
}
740+
if err = utils.DownloadAndExtractTarGz(url, tarballPath, extractedPath); err != nil {
741+
panic(fmt.Sprintf("failed to download and extract binary: %v", err))
751742
}
752743

753-
err = os.Setenv("DYLD_LIBRARY_PATH", extractedPath)
744+
err = os.Chmod(binaryPath, os.ModePerm)
754745
if err != nil {
755-
panic(fmt.Sprintf("failed to set DYLD_LIBRARY_PATH: %v", err))
746+
panic(fmt.Sprintf("failed to set permissions for binary: %v", err))
756747
}
748+
}
757749

758-
// TODO: Continue from this
759-
runCmd := exec.Command(binaryPath)
760-
if err := runCmd.Run(); err != nil {
761-
panic(fmt.Sprintf("failed to run binary: %v", err))
762-
}
750+
err = os.Setenv("DYLD_LIBRARY_PATH", extractedPath)
751+
if err != nil {
752+
panic(fmt.Sprintf("failed to set DYLD_LIBRARY_PATH: %v", err))
753+
}
763754

764-
return utils.EndLoading{}
765-
} else {
766-
panic("failed to get node version")
755+
// TODO: Continue from this
756+
runCmd := exec.Command(binaryPath)
757+
if err := runCmd.Run(); err != nil {
758+
panic(fmt.Sprintf("failed to run binary: %v", err))
767759
}
760+
761+
return utils.EndLoading{}
768762
}
769763
}
770764

utils/filesystem.go

+16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ func FileOrFolderExists(path string) bool {
1616
return !os.IsNotExist(err)
1717
}
1818

19+
func DownloadAndExtractTarGz(url, tarballPath, extractedPath string) error {
20+
if err := DownloadFile(url, tarballPath); err != nil {
21+
return err
22+
}
23+
24+
if err := ExtractTarGz(tarballPath, extractedPath); err != nil {
25+
return err
26+
}
27+
28+
if err := os.Remove(tarballPath); err != nil {
29+
return fmt.Errorf("failed to remove tarball file: %v", err)
30+
}
31+
32+
return nil
33+
}
34+
1935
func DownloadFile(url, filepath string) error {
2036
resp, err := http.Get(url)
2137
if err != nil {

0 commit comments

Comments
 (0)