Skip to content

Commit

Permalink
cli: check run supportedModes
Browse files Browse the repository at this point in the history
Signed-off-by: Weifeng Wang <qclaogui@gmail.com>
qclaogui committed Jan 18, 2024
1 parent 3caa8fa commit 47993b8
Showing 8 changed files with 50 additions and 31 deletions.
15 changes: 13 additions & 2 deletions cmd/lgtmp/main.go
Original file line number Diff line number Diff line change
@@ -4,8 +4,19 @@

package main

import "github.com/qclaogui/codelab-monitoring/internal/cmd"
import (
"github.com/qclaogui/codelab-monitoring/internal/cmd"
"os"
)

// Example:
//
// go run cmd/lgtmp/main.go up metrics
// go run cmd/lgtmp/main.go up metrics --mode microservices-mode

func main() {
cmd.Execute()
rootCmd := cmd.NewCmdRoot()
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
9 changes: 7 additions & 2 deletions internal/cmd/all/all.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ package all

import (
"fmt"
"slices"
"strings"

"github.com/MakeNowJust/heredoc"
@@ -14,7 +15,7 @@ import (
"github.com/spf13/cobra"
)

var supportedDeploymentModes = []string{"monolithic-mode"}
var supportedModes = []string{"monolithic-mode"}
var mode string

func NewCmdAll() *cobra.Command {
@@ -30,6 +31,10 @@ func NewCmdAll() *cobra.Command {
`),

RunE: func(cmd *cobra.Command, _ []string) error {
if !slices.Contains(supportedModes, mode) {
return fmt.Errorf("unsupported mode: %s", mode)
}

// up-monolithic-mode-all-in-one Run monolithic-mode all-in-one
// deploy-monolithic-mode-all-in-one Deploy monolithic-mode all-in-one
action := cmd.Parent().Use
@@ -42,7 +47,7 @@ func NewCmdAll() *cobra.Command {
}

allCmd.Flags().StringVarP(&mode, "mode", "m", "monolithic-mode",
fmt.Sprintf("deployment mode for all-in-one. Supported modes are: %s.", strings.Join(supportedDeploymentModes, ", ")))
fmt.Sprintf("deployment mode for all-in-one. Supported modes are: %s.", strings.Join(supportedModes, ", ")))

return allCmd
}
9 changes: 7 additions & 2 deletions internal/cmd/logs/logs.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ package logs

import (
"fmt"
"slices"
"strings"

"github.com/MakeNowJust/heredoc"
@@ -14,7 +15,7 @@ import (
"github.com/spf13/cobra"
)

var supportedDeploymentModes = []string{"monolithic-mode", "read-write-mode", "microservices-mode"}
var supportedModes = []string{"monolithic-mode", "read-write-mode", "microservices-mode"}
var mode string

func NewCmdLogs() *cobra.Command {
@@ -30,6 +31,10 @@ func NewCmdLogs() *cobra.Command {
`),

RunE: func(cmd *cobra.Command, _ []string) error {
if !slices.Contains(supportedModes, mode) {
return fmt.Errorf("unsupported mode: %s", mode)
}

// up-monolithic-mode-logs Run monolithic-mode logs
// deploy-monolithic-mode-logs Deploy monolithic-mode logs
action := cmd.Parent().Use
@@ -42,7 +47,7 @@ func NewCmdLogs() *cobra.Command {
}

logsCmd.Flags().StringVarP(&mode, "mode", "m", "monolithic-mode",
fmt.Sprintf("deployment mode for logs. Supported modes are: %s.", strings.Join(supportedDeploymentModes, ", ")))
fmt.Sprintf("deployment mode for logs. Supported modes are: %s.", strings.Join(supportedModes, ", ")))

return logsCmd
}
9 changes: 7 additions & 2 deletions internal/cmd/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ package metrics

import (
"fmt"
"slices"
"strings"

"github.com/MakeNowJust/heredoc"
@@ -14,7 +15,7 @@ import (
"github.com/spf13/cobra"
)

var supportedDeploymentModes = []string{"monolithic-mode", "read-write-mode", "microservices-mode"}
var supportedModes = []string{"monolithic-mode", "read-write-mode", "microservices-mode"}
var mode string

func NewCmdMetrics() *cobra.Command {
@@ -30,6 +31,10 @@ func NewCmdMetrics() *cobra.Command {
`),

RunE: func(cmd *cobra.Command, _ []string) error {
if !slices.Contains(supportedModes, mode) {
return fmt.Errorf("unsupported mode: %s", mode)
}

// up-monolithic-mode-metrics Run monolithic-mode metrics
// deploy-monolithic-mode-metrics Deploy monolithic-mode metrics
action := cmd.Parent().Use
@@ -42,7 +47,7 @@ func NewCmdMetrics() *cobra.Command {
}

metricsCmd.Flags().StringVarP(&mode, "mode", "m", "monolithic-mode",
fmt.Sprintf("deployment mode for metrics. Supported modes are: %s.", strings.Join(supportedDeploymentModes, ", ")))
fmt.Sprintf("deployment mode for metrics. Supported modes are: %s.", strings.Join(supportedModes, ", ")))

return metricsCmd
}
9 changes: 7 additions & 2 deletions internal/cmd/profiles/profiles.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ package profiles

import (
"fmt"
"slices"
"strings"

"github.com/MakeNowJust/heredoc"
@@ -14,7 +15,7 @@ import (
"github.com/spf13/cobra"
)

var supportedDeploymentModes = []string{"monolithic-mode", "microservices-mode"}
var supportedModes = []string{"monolithic-mode", "microservices-mode"}
var mode string

func NewCmdProfiles() *cobra.Command {
@@ -30,6 +31,10 @@ func NewCmdProfiles() *cobra.Command {
`),

RunE: func(cmd *cobra.Command, _ []string) error {
if !slices.Contains(supportedModes, mode) {
return fmt.Errorf("unsupported mode: %s", mode)
}

// up-monolithic-mode-profiles Run monolithic-mode profiles
// deploy-monolithic-mode-profiles Deploy monolithic-mode profiles
action := cmd.Parent().Use
@@ -42,7 +47,7 @@ func NewCmdProfiles() *cobra.Command {
}

profilesCmd.Flags().StringVarP(&mode, "mode", "m", "monolithic-mode",
fmt.Sprintf("deployment mode for profiles. Supported modes are: %s.", strings.Join(supportedDeploymentModes, ", ")))
fmt.Sprintf("deployment mode for profiles. Supported modes are: %s.", strings.Join(supportedModes, ", ")))

return profilesCmd
}
12 changes: 0 additions & 12 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
@@ -5,8 +5,6 @@
package cmd

import (
"os"

"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
)
@@ -42,13 +40,3 @@ func NewCmdRoot() *cobra.Command {

return cmd
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
cmd := NewCmdRoot()
err := cmd.Execute()
if err != nil {
os.Exit(1)
}
}
9 changes: 7 additions & 2 deletions internal/cmd/traces/traces.go
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ package traces

import (
"fmt"
"slices"
"strings"

"github.com/MakeNowJust/heredoc"
@@ -14,7 +15,7 @@ import (
"github.com/spf13/cobra"
)

var supportedDeploymentModes = []string{"monolithic-mode", "microservices-mode"}
var supportedModes = []string{"monolithic-mode", "microservices-mode"}
var mode string

func NewCmdTraces() *cobra.Command {
@@ -30,6 +31,10 @@ func NewCmdTraces() *cobra.Command {
`),

RunE: func(cmd *cobra.Command, _ []string) error {
if !slices.Contains(supportedModes, mode) {
return fmt.Errorf("unsupported mode: %s", mode)
}

// up-monolithic-mode-traces Run monolithic-mode traces
// deploy-monolithic-mode-traces Deploy monolithic-mode traces
action := cmd.Parent().Use
@@ -42,7 +47,7 @@ func NewCmdTraces() *cobra.Command {
}

tracesCmd.Flags().StringVarP(&mode, "mode", "m", "monolithic-mode",
fmt.Sprintf("deployment mode for traces. Supported modes are: %s.", strings.Join(supportedDeploymentModes, ", ")))
fmt.Sprintf("deployment mode for traces. Supported modes are: %s.", strings.Join(supportedModes, ", ")))

return tracesCmd
}
9 changes: 2 additions & 7 deletions internal/common.go
Original file line number Diff line number Diff line change
@@ -5,9 +5,7 @@
package internal

import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
@@ -17,18 +15,15 @@ import (

func ExecuteCommand(command string, args ...string) error {
cmd := exec.Command(command, args...)
var stderr bytes.Buffer
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Start(); err != nil {
log.Fatalf("Error: %s\n", stderr.String())
return err
}

// make target is the last argment
target := args[len(args)-1]
if strings.HasPrefix(target, "down-") || strings.HasPrefix(target, "delete-") {
err := cmd.Wait()
if err != nil {
if err := cmd.Wait(); err != nil {
return err
}
} else {

0 comments on commit 47993b8

Please sign in to comment.