Skip to content

Commit

Permalink
Implement hd commands and app.go
Browse files Browse the repository at this point in the history
  • Loading branch information
huy-nodeset committed Jan 10, 2025
1 parent ed6caf9 commit 0dfa3de
Show file tree
Hide file tree
Showing 14 changed files with 578 additions and 0 deletions.
65 changes: 65 additions & 0 deletions adapter/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package app

import (
"fmt"
"os"

"github.com/nodeset-org/hyperdrive-stakewise/adapter/config"
"github.com/nodeset-org/hyperdrive-stakewise/adapter/utils"
"github.com/nodeset-org/hyperdrive-stakewise/shared"
"github.com/urfave/cli/v2"
)

func CreateApp() *cli.App {
// Initialize application
app := cli.NewApp()

// Set application info
app.Name = "Hyperdrive Stakewise Adapter"
app.Usage = "Adapter for the Hyperdrive Stakewise module"
app.Version = shared.StakewiseVersion
app.Authors = []*cli.Author{
{
Name: "Nodeset",
Email: "[email protected]",
},
}
app.Copyright = "(c) 2024 NodeSet LLC"

// Enable Bash Completion
app.EnableBashCompletion = true

// Set application flags
app.Flags = []cli.Flag{
utils.ConfigDirFlag,
utils.LogDirFlag,
utils.KeyFileFlag,
}

// Register commands
config.RegisterCommands(app)

app.Before = func(c *cli.Context) error {
// Make the authenticator
auth, err := utils.NewAuthenticator(c)
if err != nil {
return err
}
c.App.Metadata[utils.AuthenticatorMetadataKey] = auth

return nil
}
app.BashComplete = func(c *cli.Context) {
// Load the context and flags prior to autocomplete
err := app.Before(c)
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}

// Run the default autocomplete
cli.DefaultAppComplete(c)
}

return app
}
121 changes: 121 additions & 0 deletions adapter/hd-module/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package hdmodule

import (
"github.com/nodeset-org/hyperdrive-stakewise/adapter/utils"
"github.com/urfave/cli/v2"
)

// Handles `hd-module` commands
func RegisterCommands(app *cli.App) {
app.Commands = append(app.Commands, &cli.Command{
Name: "hd-module",
Aliases: []string{"hd"},
Usage: "Handle Hyperdrive module commands",
Subcommands: []*cli.Command{
{
Name: "version",
Aliases: []string{"v"},
Flags: []cli.Flag{},
Usage: "Print the module version.",
Action: func(c *cli.Context) error {
// Validate args
utils.ValidateArgCount(c, 0)

// Run
return version()
},
},
{
Name: "get-log-file",
Aliases: []string{"l"},
Flags: []cli.Flag{},
Usage: "Get the path to a log file.",
Action: func(c *cli.Context) error {
// Validate args
utils.ValidateArgCount(c, 0)

// Run
return getLogFile(c)
},
},
{
Name: "get-config-metadata",
Aliases: []string{"c"},
Flags: []cli.Flag{},
Usage: "Get the metadata for the module's configuration, representing how to present the parameters to the user.",
Action: func(c *cli.Context) error {
// Validate args
utils.ValidateArgCount(c, 0)

// Run
return getConfigMetadata(c)
},
},
{
Name: "get-config-instance",
Aliases: []string{"i"},
Flags: []cli.Flag{},
Usage: "Get the instance of the module's configuration, with the values all set.",
Action: func(c *cli.Context) error {
// Validate args
utils.ValidateArgCount(c, 0)

// Run
return getConfigInstance(c)
},
},
{
Name: "process-config",
Aliases: []string{"p"},
Flags: []cli.Flag{},
Usage: "Process the module's configuration, validating it without saving.",
Action: func(c *cli.Context) error {
// Validate args
utils.ValidateArgCount(c, 0)

// Run
return processConfig(c)
},
},
{
Name: "set-config",
Aliases: []string{"s"},
Flags: []cli.Flag{},
Usage: "Sets the module's configuration, saving it to disk.",
Action: func(c *cli.Context) error {
// Validate args
utils.ValidateArgCount(c, 0)

// Run
return setConfig(c)
},
},
{
Name: "get-containers",
Aliases: []string{"t"},
Flags: []cli.Flag{},
Usage: "Get the list of containers owned by this module.",
Action: func(c *cli.Context) error {
// Validate args
utils.ValidateArgCount(c, 0)

// Run
return getContainers(c)
},
},
{
Name: "run",
Aliases: []string{"r"},
Flags: []cli.Flag{},
Usage: "Run a command.",
Action: func(c *cli.Context) error {
// Validate args
utils.ValidateArgCount(c, 0)

// Run
return run(c)
},
},
},
})
}
46 changes: 46 additions & 0 deletions adapter/hd-module/get-config-metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package hdmodule

import (
//"encoding/json"
"fmt"

"github.com/goccy/go-json"
"github.com/nodeset-org/hyperdrive-stakewise/adapter/config"
"github.com/nodeset-org/hyperdrive-stakewise/adapter/utils"
hdconfig "github.com/nodeset-org/hyperdrive/modules/config"
"github.com/urfave/cli/v2"
)

func getConfigMetadata(c *cli.Context) error {
// Get the request
_, err := utils.HandleKeyedRequest[*utils.KeyedRequest](c)
if err != nil {
return err
}

// Get the config
cfgMgr, err := config.NewAdapterConfigManager(c)
if err != nil {
return fmt.Errorf("error creating config manager: %w", err)
}
cfg, err := cfgMgr.LoadConfigFromDisk()
if err != nil {
return fmt.Errorf("error loading config: %w", err)
}

// Handle no config file by using the default
if cfg == nil {
cfg = config.NewExampleConfig()
}

// Create the response
cfgMap := hdconfig.MarshalConfigurationMetadataToMap(cfg)
bytes, err := json.Marshal(cfgMap)
if err != nil {
return fmt.Errorf("error marshalling config: %w", err)
}

// Print it
fmt.Println(string(bytes))
return nil
}
42 changes: 42 additions & 0 deletions adapter/hd-module/get-containers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package hdmodule

import (
"fmt"

"github.com/goccy/go-json"
"github.com/nodeset-org/hyperdrive-stakewise/adapter/utils"
"github.com/nodeset-org/hyperdrive-stakewise/shared"
"github.com/urfave/cli/v2"
)

// Response format for `get-containers`
type getContainersResponse struct {
// The list of containers owned by this module
Containers []string `json:"containers"`
}

// Handle the `get-containers` command
func getContainers(c *cli.Context) error {
// Get the request
_, err := utils.HandleKeyedRequest[*utils.KeyedRequest](c)
if err != nil {
return err
}

// Create the response
response := getContainersResponse{
Containers: []string{
shared.ServiceContainerName,
},
}

// Marshal it
bytes, err := json.Marshal(response)
if err != nil {
return fmt.Errorf("error marshalling get-containers response: %w", err)
}

// Print it
fmt.Println(string(bytes))
return nil
}
57 changes: 57 additions & 0 deletions adapter/hd-module/get-log-file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package hdmodule

import (
"fmt"

"github.com/goccy/go-json"
"github.com/nodeset-org/hyperdrive-stakewise/adapter/utils"
"github.com/nodeset-org/hyperdrive-stakewise/shared"
"github.com/urfave/cli/v2"
)

// Request format for `get-log-file`
type getLogFileRequest struct {
utils.KeyedRequest

// The log file source to retrieve
Source string `json:"source"`
}

// Response format for `get-log-file`
type getLogFileResponse struct {
// The path to the log file
Path string `json:"path"`
}

// Handle the `get-log-file` command
func getLogFile(c *cli.Context) error {
// Get the request
request, err := utils.HandleKeyedRequest[*getLogFileRequest](c)
if err != nil {
return err
}

// Get the path
path := ""
switch request.Source {
case "adapter":
path = utils.AdapterLogFile
case shared.ServiceContainerName:
path = shared.ServiceLogFile
}

// Create the response
response := getLogFileResponse{
Path: path,
}

// Marshal it
bytes, err := json.Marshal(response)
if err != nil {
return fmt.Errorf("error marshalling get-log-file response: %w", err)
}

// Print it
fmt.Println(string(bytes))
return nil
}
Loading

0 comments on commit 0dfa3de

Please sign in to comment.