-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/spf13/cobra" | ||
"os" | ||
"path/filepath" | ||
"razor/core" | ||
"razor/utils" | ||
) | ||
|
||
// importEndpointsCmd represents the importEndpoints command | ||
var importEndpointsCmd = &cobra.Command{ | ||
Use: "importEndpoints", | ||
Short: "imports the list of endpoints locally", | ||
Long: `Imports the list of endpoints to $HOME/.rzr directory allowing the user to access/edit it easily. | ||
Example: | ||
./razor importEndpoints `, | ||
Run: initialiseImportEndpoints, | ||
} | ||
|
||
func initialiseImportEndpoints(cmd *cobra.Command, args []string) { | ||
cmdUtils.ExecuteImportEndpoints() | ||
} | ||
|
||
// ExecuteImportEndpoints imports the list of endpoints from core/endpoints.go to $HOME/.razor directory locally. | ||
func (*UtilsStruct) ExecuteImportEndpoints() { | ||
defaultPath, err := pathUtils.GetDefaultPath() | ||
utils.CheckError("Error in getting default path: ", err) | ||
|
||
// Define the target path for endpoints.json | ||
destFilePath := filepath.Join(defaultPath, "endpoints.json") | ||
|
||
// Serialize the default endpoints to JSON | ||
endpointsData, err := json.MarshalIndent(core.DefaultEndpoints, "", " ") | ||
utils.CheckError("Error in serializing the endpoints: %w", err) | ||
|
||
// Write the JSON to the destination file | ||
err = os.WriteFile(destFilePath, endpointsData, 0644) | ||
utils.CheckError("Error in writing endpoints.json: %w", err) | ||
|
||
log.Infof("Default endpoints successfully imported to %s", destFilePath) | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(importEndpointsCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/stretchr/testify/assert" | ||
"os" | ||
"path/filepath" | ||
"razor/core" | ||
"testing" | ||
) | ||
|
||
func TestExecuteImportEndpoints_WithMockedPath(t *testing.T) { | ||
// Create a temporary directory to mock the default path | ||
tempDir, err := os.MkdirTemp("", "test_default_path") | ||
assert.NoError(t, err, "Temporary directory should be created") | ||
defer os.RemoveAll(tempDir) // Clean up after the test | ||
|
||
SetUpMockInterfaces() | ||
|
||
pathMock.On("GetDefaultPath").Return(tempDir, nil) | ||
|
||
// Call ExecuteImportEndpoints | ||
utils := &UtilsStruct{} | ||
utils.ExecuteImportEndpoints() | ||
|
||
// Verify the file was created in the mocked directory | ||
destFilePath := filepath.Join(tempDir, "endpoints.json") | ||
_, err = os.Stat(destFilePath) | ||
assert.NoError(t, err, "endpoints.json should be created in the mocked directory") | ||
|
||
// Verify the content of the file matches core.DefaultEndpoints | ||
data, err := os.ReadFile(destFilePath) | ||
assert.NoError(t, err, "File should be readable") | ||
|
||
var importedEndpoints []string | ||
err = json.Unmarshal(data, &importedEndpoints) | ||
assert.NoError(t, err, "JSON should be valid") | ||
assert.Equal(t, core.DefaultEndpoints, importedEndpoints, "Imported endpoints should match the defaults") | ||
|
||
// Delete the file after verification | ||
err = os.Remove(destFilePath) | ||
assert.NoError(t, err, "endpoints.json should be deleted successfully") | ||
|
||
// Verify logs | ||
log.Infof("Default endpoints successfully imported to %s", destFilePath) | ||
pathMock.AssertExpectations(t) | ||
|
||
// Confirm the file no longer exists | ||
_, err = os.Stat(destFilePath) | ||
assert.Error(t, err, "endpoints.json should not exist after deletion") | ||
assert.True(t, os.IsNotExist(err), "Error should indicate file does not exist") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package core | ||
|
||
var DefaultEndpoints = []string{ | ||
"https://andromeda02.skale.prod.chorus1.net:10072", | ||
"https://skale-figment-9.skale.figment.io:10008", | ||
"https://skale-figment-2.skale.figment.io:10072", | ||
"https://node03.skale.prod.chorus1.net:10072", | ||
"https://skale.tuku.dev:10136", | ||
"https://signia-node-8.skale.bdnodes.net:10072", | ||
"https://skale.infstones.io:10136", | ||
"https://skale-figment-8.skale.figment.io:10200", | ||
"https://node05.skale.prod.chorus1.net:10136", | ||
"https://block-node-5.skale.bdnodes.net:10008", | ||
"https://skale-figment-11.skale.figment.io:10072", | ||
"https://skale-jupiter-2.skale.figment.io:10072", | ||
"https://signia-node-5.skale.bdnodes.net:10328", | ||
"https://block-node-8.skale.bdnodes.net:10008", | ||
"https://skale-mainnet-1a.stakin-nodes.com:10072", | ||
"https://skale2.anonstake.com:10008", | ||
} |