From fe5a2642a7a767ebc26c939f9135b9ab92b36d00 Mon Sep 17 00:00:00 2001 From: Suryansh Prajapati <58465650+drk1rd@users.noreply.github.com> Date: Mon, 27 Jan 2025 05:16:25 +0000 Subject: [PATCH] refactored for modularity --- cli/cmd/{ => compute}/compute.go | 40 +++++++++++++++++--------------- cli/cmd/config.go | 26 ++++++++++----------- cli/cmd/root.go | 32 +++++++++++++++---------- 3 files changed, 54 insertions(+), 44 deletions(-) rename cli/cmd/{ => compute}/compute.go (94%) diff --git a/cli/cmd/compute.go b/cli/cmd/compute/compute.go similarity index 94% rename from cli/cmd/compute.go rename to cli/cmd/compute/compute.go index 56747a3..48ffce0 100644 --- a/cli/cmd/compute.go +++ b/cli/cmd/compute/compute.go @@ -1,4 +1,4 @@ -package cmd +package compute import ( "encoding/json" @@ -9,10 +9,23 @@ import ( docker "github.com/fsouza/go-dockerclient" "github.com/spf13/cobra" + ) const stateFile = "compute.json" +func init() { + // Load saved instances + loadInstances() + + // Add subcommands to the compute command + ComputeCmd.AddCommand(createCmd) + ComputeCmd.AddCommand(listCmd) + ComputeCmd.AddCommand(startCmd) + ComputeCmd.AddCommand(enterCmd) + ComputeCmd.AddCommand(stopCmd) + ComputeCmd.AddCommand(deleteCmd) +} // Instance represents a compute instance with its configuration and status type Instance struct { ID string `json:"id"` @@ -37,26 +50,15 @@ var ( ) // Root command for compute operations -var computeCmd = &cobra.Command{ - Use: "compute", - Short: "Manage compute resources with Docker", - Run: func(cmd *cobra.Command, args []string) { - cmd.Help() - }, +var ComputeCmd = &cobra.Command{ + Use: "compute", + Short: "Manage compute resources", + Long: "The compute command allows you to manage compute resources like VMs, containers, etc.", + Run: func(cmd *cobra.Command, args []string) { + cmd.Help() + }, } -func init() { - rootCmd.AddCommand(computeCmd) - loadInstances() - - // Add subcommands to the compute command - computeCmd.AddCommand(createCmd) - computeCmd.AddCommand(listCmd) - computeCmd.AddCommand(startCmd) - computeCmd.AddCommand(enterCmd) - computeCmd.AddCommand(stopCmd) - computeCmd.AddCommand(deleteCmd) -} // saveInstances saves the current state of instances to a JSON file func saveInstances() { diff --git a/cli/cmd/config.go b/cli/cmd/config.go index 59de669..d3e656d 100644 --- a/cli/cmd/config.go +++ b/cli/cmd/config.go @@ -115,17 +115,17 @@ var configCmd = &cobra.Command{ } func init() { - home, err := os.UserHomeDir() - if err != nil { - panic(err) - } - // Set the path to the config file in the user's home directory - configFile = filepath.Join(home, ".homecloud_config.json") - - // Add subcommands to the config command - configCmd.AddCommand(setConfigCmd) - configCmd.AddCommand(getConfigCmd) - - // Add the config command to the root command - rootCmd.AddCommand(configCmd) + home, err := os.UserHomeDir() + if err != nil { + panic(err) + } + // Set the path to the config file in the user's home directory + configFile = filepath.Join(home, ".homecloud_config.json") + + // Add subcommands to the config command + configCmd.AddCommand(setConfigCmd) + configCmd.AddCommand(getConfigCmd) + + // Add the config command to the RootCmd + RootCmd.AddCommand(configCmd) // Use RootCmd here } diff --git a/cli/cmd/root.go b/cli/cmd/root.go index cadf67a..38a004e 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -1,23 +1,31 @@ package cmd import ( - "fmt" + "fmt" - "github.com/spf13/cobra" + "github.com/spf13/cobra" + "github.com/drk1rd/homecloud/cli/cmd/compute" ) -var rootCmd = &cobra.Command{ - Use: "homecloud", - Short: "HomeCloud CLI for managing self-hosted cloud services", - Long: `HomeCloud CLI provides tools to deploy, manage, and monitor +// RootCmd is the base command for the CLI, exported for use in other modules. +var RootCmd = &cobra.Command{ + Use: "homecloud", + Short: "HomeCloud CLI for managing self-hosted cloud services", + Long: `HomeCloud CLI provides tools to deploy, manage, and monitor self-hosted cloud infrastructure.`, - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Welcome to HomeCloud CLI!") - }, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("Welcome to HomeCloud CLI!") + }, } +// Execute runs the root command, entry point for the CLI. func Execute() { - if err := rootCmd.Execute(); err != nil { - fmt.Println(err) - } + if err := RootCmd.Execute(); err != nil { + fmt.Println(err) + } } + +// Register all individual comments here. +func init() { + RootCmd.AddCommand(compute.ComputeCmd) +} \ No newline at end of file