Skip to content

Commit 179e081

Browse files
committed
chore: begin refactor of strfry29 install
1 parent d11da62 commit 179e081

File tree

3 files changed

+72
-53
lines changed

3 files changed

+72
-53
lines changed

cmd/install.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ var installCmd = &cobra.Command{
248248
strfry29.ConfigureNginxHttps(relayDomain)
249249
}
250250

251-
// Step 9: Download and install the relay binary
252-
strfry29.InstallRelayBinary()
251+
// Step 9: Download and install the relay binaries
252+
strfry29.InstallRelayBinaries()
253253

254254
// Step 10: Set up the relay data directory
255255
strfry29.SetUpRelayDataDir()

pkg/relays/strfry29/install.go

+62-51
Original file line numberDiff line numberDiff line change
@@ -12,84 +12,95 @@ import (
1212
"path/filepath"
1313
)
1414

15-
// Function to download and make the binary and plugin binary executable
16-
func InstallRelayBinary() {
17-
downloadSpinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Downloading %s binaries...", RelayName))
15+
// TODO
16+
// Abstract this even more
1817

18+
func cloneTmpGitRepo() {
1919
// Check for and remove existing git repository
2020
directories.RemoveDirectory(GitRepoTmpDirPath)
2121

2222
// Download git repository
2323
git.Clone(GitRepoBranch, GitRepoURL, GitRepoTmpDirPath)
2424

2525
directories.SetPermissions(GitRepoTmpDirPath, 0755)
26+
}
2627

27-
// Install
28-
// Determine the file name from the URL
29-
tmpBinaryFileName := filepath.Base(DownloadURL)
28+
// Determine the temporary file name from the provided path
29+
func tmpFilePathFromFilePath(path string) string {
30+
tmpFileName := filepath.Base(path)
3031

31-
// Temporary file path
32-
tmpBinaryFilePath := fmt.Sprintf("%s/%s", relays.TmpDirPath, tmpBinaryFileName)
32+
tmpFilePath := fmt.Sprintf("%s/%s", relays.TmpDirPath, tmpFileName)
3333

34-
// Check if the temporary file exists and remove it if it does
35-
files.RemoveFile(tmpBinaryFilePath)
34+
return tmpFilePath
35+
}
3636

37-
// Download and copy the file
38-
files.DownloadAndCopyFile(tmpBinaryFilePath, DownloadURL)
37+
func installRelayBinary(compressedBinaryFilePath, binaryName string) {
38+
// Extract relay binary
39+
files.ExtractFile(compressedBinaryFilePath, relays.BinaryDestDir)
3940

40-
// Determine the file name from the URL
41-
tmpBinaryPluginFileName := filepath.Base(BinaryPluginDownloadURL)
41+
// TODO
42+
// Currently, the downloaded binary is expected to have a name that matches the binaryName variable
43+
// Ideally, the extracted binary file should be renamed to match the binaryName variable
4244

43-
// Temporary file path
44-
tmpBinaryPluginFilePath := fmt.Sprintf("%s/%s", relays.TmpDirPath, tmpBinaryPluginFileName)
45+
// Define the final destination path
46+
destPath := filepath.Join(relays.BinaryDestDir, binaryName)
4547

46-
// Check if the temporary file exists and remove it if it does
47-
files.RemoveFile(tmpBinaryPluginFilePath)
48+
// Make the file executable
49+
files.SetPermissions(destPath, 0755)
50+
}
4851

49-
// Download and copy the file
50-
files.DownloadAndCopyFile(tmpBinaryPluginFilePath, BinaryPluginDownloadURL)
52+
// Function to download and make the binary and plugin binary executable
53+
func InstallRelayBinaries() {
54+
pterm.Println()
55+
relayBinaryCheckSpinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Checking for existing %s binary...", BinaryName))
5156

52-
downloadSpinner.Success(fmt.Sprintf("%s binaries downloaded", RelayName))
57+
cloneTmpGitRepo()
5358

54-
// Verify relay binary
55-
verification.VerifyRelayBinary(BinaryName, tmpBinaryFilePath)
59+
// Check if the service file exists and disable and stop the service if it does
60+
systemd.DisableAndStopService(ServiceFilePath, ServiceName)
5661

57-
// Verify relay binary plugin
58-
verification.VerifyRelayBinary(fmt.Sprintf("%s plugin", RelayName), tmpBinaryPluginFilePath)
62+
// Check if relay binary exists
63+
if !files.FileExists(BinaryFilePath) {
64+
relayBinaryCheckSpinner.Info(fmt.Sprintf("%s binary not found", BinaryName))
65+
pterm.Println()
5966

60-
installSpinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Installing %s binaries...", RelayName))
67+
// Determine the temporary file path
68+
tmpCompressedBinaryFilePath := tmpFilePathFromFilePath(DownloadURL)
6169

62-
// Check if the service file exists and disable and stop the service if it does
63-
if files.FileExists(ServiceFilePath) {
64-
// Disable and stop the Nostr relay service
65-
installSpinner.UpdateText("Disabling and stopping service...")
66-
systemd.DisableService(ServiceName)
67-
systemd.StopService(ServiceName)
68-
} else {
69-
installSpinner.UpdateText("Service file not found...")
70-
}
70+
// Check if the temporary file exists and remove it if it does
71+
files.RemoveFile(tmpCompressedBinaryFilePath)
7172

72-
// Extract relay binary
73-
files.ExtractFile(tmpBinaryFilePath, relays.BinaryDestDir)
73+
// Download and copy the file
74+
downloadSpinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Downloading %s binary...", BinaryName))
75+
files.DownloadAndCopyFile(tmpCompressedBinaryFilePath, DownloadURL)
76+
downloadSpinner.Success(fmt.Sprintf("%s binary downloaded", BinaryName))
7477

75-
// Extract relay binary plugin
76-
files.ExtractFile(tmpBinaryPluginFilePath, relays.BinaryDestDir)
78+
// Verify relay binary
79+
verification.VerifyRelayBinary(BinaryName, tmpCompressedBinaryFilePath)
7780

78-
// TODO
79-
// Currently, the downloaded binary is expected to have a name that matches the BinaryName variable
80-
// Ideally, the extracted binary file should be renamed to match the BinaryName variable
81+
installSpinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Installing %s binary...", BinaryName))
82+
installRelayBinary(tmpCompressedBinaryFilePath, BinaryName)
83+
installSpinner.Success(fmt.Sprintf("%s binary installed", BinaryName))
84+
} else {
85+
relayBinaryCheckSpinner.Info(fmt.Sprintf("%s binary found", BinaryName))
86+
pterm.Println()
87+
}
8188

82-
// Define the final destination path
83-
destPath := filepath.Join(relays.BinaryDestDir, BinaryName)
89+
// Determine the temporary file path
90+
tmpCompressedBinaryPluginFilePath := tmpFilePathFromFilePath(BinaryPluginDownloadURL)
8491

85-
// Make the file executable
86-
files.SetPermissions(destPath, 0755)
92+
// Check if the temporary file exists and remove it if it does
93+
files.RemoveFile(tmpCompressedBinaryPluginFilePath)
8794

88-
// Define the final destination path
89-
destPath = filepath.Join(relays.BinaryDestDir, BinaryPluginName)
95+
// Download and copy the file
96+
binaryPluginDownloadSpinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Downloading %s plugin binary...", BinaryPluginName))
97+
files.DownloadAndCopyFile(tmpCompressedBinaryPluginFilePath, BinaryPluginDownloadURL)
98+
binaryPluginDownloadSpinner.Success(fmt.Sprintf("%s plugin binary downloaded", BinaryPluginName))
9099

91-
// Make the file executable
92-
files.SetPermissions(destPath, 0755)
100+
// Verify relay binary plugin
101+
verification.VerifyRelayBinary(fmt.Sprintf("%s plugin", BinaryPluginName), tmpCompressedBinaryPluginFilePath)
93102

94-
installSpinner.Success(fmt.Sprintf("%s binaries installed", RelayName))
103+
binaryPluginInstallSpinner, _ := pterm.DefaultSpinner.Start(fmt.Sprintf("Installing %s plugin binary...", BinaryPluginName))
104+
installRelayBinary(tmpCompressedBinaryPluginFilePath, BinaryPluginName)
105+
binaryPluginInstallSpinner.Success(fmt.Sprintf("%s plugin binary installed", BinaryPluginName))
95106
}

pkg/utils/systemd/utils.go

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package systemd
22

33
import (
44
"fmt"
5+
"github.com/nodetec/rwz/pkg/utils/files"
56
"github.com/pterm/pterm"
67
"os"
78
"os/exec"
@@ -100,3 +101,10 @@ func RestartService(name string) {
100101
os.Exit(1)
101102
}
102103
}
104+
105+
func DisableAndStopService(path, name string) {
106+
if files.FileExists(path) {
107+
DisableService(name)
108+
StopService(name)
109+
}
110+
}

0 commit comments

Comments
 (0)