diff --git a/cmd/probe.go b/cmd/client/probe.go similarity index 97% rename from cmd/probe.go rename to cmd/client/probe.go index 6212845..2d6f19d 100644 --- a/cmd/probe.go +++ b/cmd/client/probe.go @@ -1,4 +1,4 @@ -package cmd +package client import ( "bytes" @@ -30,19 +30,15 @@ func newProber(cfg *config.App) *proberClient { return &proberClient{config: cfg} } -var probeCmd = &cobra.Command{ +var ProbeCmd = &cobra.Command{ Use: "probe", - Short: "Run Monero node prober", - Run: func(_ *cobra.Command, _ []string) { - runProbe() + Short: "Probe remote nodes", + Run: func(cmd *cobra.Command, args []string) { + RunProbe() }, } -func init() { - rootCmd.AddCommand(probeCmd) -} - -func runProbe() { +func RunProbe() { cfg := config.AppCfg() if cfg.ServerEndpoint == "" { fmt.Println("Please set SERVER_ENDPOINT in .env") diff --git a/cmd/root.go b/cmd/cmd.go similarity index 65% rename from cmd/root.go rename to cmd/cmd.go index b00cbf6..4663c02 100644 --- a/cmd/root.go +++ b/cmd/cmd.go @@ -2,26 +2,26 @@ package cmd import ( "os" - "xmr-remote-nodes/internal/config" + "xmr-remote-nodes/cmd/client" "github.com/spf13/cobra" ) const AppVer = "0.0.1" -var rootCmd = &cobra.Command{ +var Root = &cobra.Command{ Use: "xmr-nodes", Short: "XMR Nodes", Version: AppVer, } func Execute() { - err := rootCmd.Execute() + err := Root.Execute() if err != nil { os.Exit(1) } } func init() { - config.LoadAll(".env") + Root.AddCommand(client.ProbeCmd) } diff --git a/cmd/admin.go b/cmd/server/admin.go similarity index 93% rename from cmd/admin.go rename to cmd/server/admin.go index b232517..d6b31e5 100644 --- a/cmd/admin.go +++ b/cmd/server/admin.go @@ -1,4 +1,4 @@ -package cmd +package server import ( "bufio" @@ -13,7 +13,7 @@ import ( "golang.org/x/term" ) -var adminCmd = &cobra.Command{ +var AdminCmd = &cobra.Command{ Use: "admin", Short: "Create Admin", Long: `Create an admin account for WebUI access.`, @@ -37,10 +37,6 @@ var adminCmd = &cobra.Command{ }, } -func init() { - rootCmd.AddCommand(adminCmd) -} - func createAdmin() error { admin := repo.NewAdminRepo(database.GetDB()) a := repo.Admin{ diff --git a/cmd/import.go b/cmd/server/import.go similarity index 96% rename from cmd/import.go rename to cmd/server/import.go index f16ef54..7fce9f6 100644 --- a/cmd/import.go +++ b/cmd/server/import.go @@ -1,4 +1,4 @@ -package cmd +package server import ( "encoding/json" @@ -25,7 +25,7 @@ type importData struct { DateEntered int `json:"date_entered"` } -var importCmd = &cobra.Command{ +var ImportCmd = &cobra.Command{ Use: "import", Short: "Import Monero nodes from old API", Long: `Import Monero nodes from old API. @@ -72,10 +72,6 @@ This command only available during migration process and will be removed in futu }, } -func init() { - rootCmd.AddCommand(importCmd) -} - func (i *importClient) processData(node importData) error { query := `SELECT id FROM tbl_node WHERE hostname = ? AND port = ? AND protocol = ?` row := i.db.QueryRow(query, node.Hostname, node.Port, node.Protocol) diff --git a/cmd/probers.go b/cmd/server/probers.go similarity index 97% rename from cmd/probers.go rename to cmd/server/probers.go index cf0fa1f..b4ab49f 100644 --- a/cmd/probers.go +++ b/cmd/server/probers.go @@ -1,4 +1,4 @@ -package cmd +package server import ( "fmt" @@ -6,6 +6,7 @@ import ( "strings" "text/tabwriter" "time" + "xmr-remote-nodes/cmd" "xmr-remote-nodes/internal/database" "xmr-remote-nodes/internal/repo" @@ -80,7 +81,7 @@ var addProbersCmd = &cobra.Command{ } func init() { - rootCmd.AddCommand(probersCmd) + cmd.Root.AddCommand(probersCmd) probersCmd.AddCommand(listProbersCmd) probersCmd.AddCommand(addProbersCmd) listProbersCmd.Flags().StringP("sort-by", "s", "last_submit_ts", "Sort by column name, can be id or last_submit_ts") diff --git a/cmd/serve.go b/cmd/server/serve.go similarity index 94% rename from cmd/serve.go rename to cmd/server/serve.go index a06b208..c91b8f5 100644 --- a/cmd/serve.go +++ b/cmd/server/serve.go @@ -1,4 +1,4 @@ -package cmd +package server import ( "fmt" @@ -21,7 +21,7 @@ import ( "github.com/spf13/cobra" ) -var serveCmd = &cobra.Command{ +var ServeCmd = &cobra.Command{ Use: "serve", Short: "Serve the WebUI", Long: `This command will run HTTP server for APIs and WebUI.`, @@ -30,10 +30,6 @@ var serveCmd = &cobra.Command{ }, } -func init() { - rootCmd.AddCommand(serveCmd) -} - func serve() { appCfg := config.AppCfg() // connect to DB @@ -105,6 +101,6 @@ func fiberConfig() fiber.Config { return fiber.Config{ Prefork: config.AppCfg().Prefork, ProxyHeader: config.AppCfg().ProxyHeader, - AppName: "ditatompel's XMR Nodes HTTP server " + AppVer, + AppName: "ditatompel's XMR Nodes HTTP server", } } diff --git a/main.go b/main.go index ec552ae..a4e9242 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,11 @@ package main -import "xmr-remote-nodes/cmd" +import ( + "xmr-remote-nodes/cmd" + "xmr-remote-nodes/internal/config" +) func main() { + config.LoadAll(".env") cmd.Execute() } diff --git a/server.go b/server.go new file mode 100644 index 0000000..e535bd0 --- /dev/null +++ b/server.go @@ -0,0 +1,14 @@ +//go:build server + +package main + +import ( + "xmr-remote-nodes/cmd" + "xmr-remote-nodes/cmd/server" +) + +func init() { + cmd.Root.AddCommand(server.AdminCmd) + cmd.Root.AddCommand(server.ServeCmd) + cmd.Root.AddCommand(server.ImportCmd) +}