From e9b3b64a7593e20455e2180ff644f6e3888ffaca Mon Sep 17 00:00:00 2001 From: Luxxgit2k4 Date: Sun, 16 Feb 2025 21:30:14 +0530 Subject: [PATCH 1/3] Installed Cobra Cli, updated go.mod and go.sum Signed-off-by: Luxxgit2k4 --- go.mod | 6 ++++++ go.sum | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/go.mod b/go.mod index ab91059..5bb1ce0 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,9 @@ module github.com/microcks/microcks-cli go 1.21.3 + +require ( + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/spf13/cobra v1.9.0 // indirect + github.com/spf13/pflag v1.0.6 // indirect +) diff --git a/go.sum b/go.sum index e69de29..87c48aa 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.9.0 h1:Py5fIuq/lJsRYxcxfOtsJqpmwJWCMOUy2tMJYV8TNHE= +github.com/spf13/cobra v1.9.0/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0= +github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o= +github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 40ab4a7c798c7c2ac2aece00704193fe21b9236b Mon Sep 17 00:00:00 2001 From: Luxxgit2k4 Date: Sun, 16 Feb 2025 22:38:55 +0530 Subject: [PATCH 2/3] Added root.go, start.go and stop.go for starting and stopping the server. Signed-off-by: Luxxgit2k4 --- cmd/root.go | 28 ++++++++++++++++++++++++++++ cmd/start.go | 31 +++++++++++++++++++++++++++++++ cmd/stop.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 cmd/root.go create mode 100644 cmd/start.go create mode 100644 cmd/stop.go diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..9da2c3b --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,28 @@ +package cmd + +import ( + "fmt" + "os" + "github.com/spf13/cobra" +) + +//rootCmd represents the base command when called without any subcommands +var RootCmd = &cobra.Command{ + Use: "microcks-cli", + Short: "A brief description of your application", + Long: `A longer description that spans multiple lines and likely contains +examples and usage of using your application. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + // Uncomment the following line if your bare application + // has an action associated with it: + // Run: func(cmd *cobra.Command, args []string) { }, +} +func Execute() { + if err := RootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/cmd/start.go b/cmd/start.go new file mode 100644 index 0000000..31a347d --- /dev/null +++ b/cmd/start.go @@ -0,0 +1,31 @@ +package cmd + +import ( + "fmt" + "os" + "os/exec" + + "github.com/spf13/cobra" +) + +var startCmd = &cobra.Command{ + Use: "start", + Short: "Start the Microcks server", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("Starting Microcks server...") + + startCmd := exec.Command("docker", "run", "-d", "-p", "8585:8080", "--rm", "--name", "microcks-server", "quay.io/microcks/microcks-uber:latest-native") + + startCmd.Stdout = nil + startCmd.Stderr = os.Stderr + + if err := startCmd.Run(); err != nil { + fmt.Printf("Error starting Microcks: %v\n", err) + os.Exit(1) + } + }, +} + +func init() { + RootCmd.AddCommand(startCmd) +} diff --git a/cmd/stop.go b/cmd/stop.go new file mode 100644 index 0000000..8322277 --- /dev/null +++ b/cmd/stop.go @@ -0,0 +1,31 @@ +package cmd + +import ( + "fmt" + "os/exec" + + "github.com/spf13/cobra" +) + +var stopCmd = &cobra.Command{ + Use: "stop", + Short: "Stop the Microcks server", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("Stopping Microcks server...") + + stopCmd := exec.Command("docker", "stop", "microcks-server") + + stopCmd.Stdout = cmd.OutOrStdout() + stopCmd.Stderr = cmd.OutOrStderr() + + if err := stopCmd.Run(); err != nil { + fmt.Printf("Error stopping Microcks: %v\n", err) + } else { + fmt.Println("Microcks server stopped.") + } + }, +} + +func init() { + RootCmd.AddCommand(stopCmd) +} From d58cab76890c0ee3a3a1375a4d1de1fb65a42700 Mon Sep 17 00:00:00 2001 From: Luxxgit2k4 Date: Sun, 16 Feb 2025 23:30:18 +0530 Subject: [PATCH 3/3] Added start and stop command in main.go by preventing the overwriting of the existing commands Signed-off-by: Luxxgit2k4 --- main.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/main.go b/main.go index 92427da..67e1d02 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,11 @@ func main() { cmd.NewHelpCommand().Execute() os.Exit(1) } + // start and stop command using cobra by preventing overwriting of the previous command + if os.Args[1] == "start" || os.Args[1] == "stop" { + cmd.Execute() + return + } switch os.Args[1] { case "version":