Skip to content

Commit

Permalink
chore: refactor v2 cmds by removing duplicate code
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Jain <shubhamkjain@outlook.com>
  • Loading branch information
slayerjain committed Feb 15, 2024
1 parent 590b58a commit 1786447
Show file tree
Hide file tree
Showing 39 changed files with 989 additions and 1,192 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ RUN go build -ldflags="-X main.dsn=$SENTRY_DSN_DOCKER -X main.version=$VERSION"
# === Runtime Stage ===
FROM debian:bookworm-slim

ENV IS_DOCKER_CMD=true
ENV KEPLOY_INDOCKER=true

# Update the package lists and install required packages
RUN apt-get update && \
Expand Down
26 changes: 21 additions & 5 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
package cmd

import (
"context"
"github.com/spf13/cobra"
"go.keploy.io/server/config"
updateSvc "go.keploy.io/server/pkg/service/update"
"go.uber.org/zap"
)

// create interface for cobra commands and subcommands
type Cmd interface {
GetCmd(*zap.Logger) *cobra.Command
type HookFunc func(context.Context, *zap.Logger, *config.Config, Services) *cobra.Command

// Registered holds the registered command hooks
var Registered map[string]HookFunc

func Register(name string, f HookFunc) {
if Registered == nil {
Registered = make(map[string]HookFunc)
}
Registered[name] = f
}

// create global array of registered commands
var RegisteredCmds []Cmd
// Services holds the services required by the commands
type Services struct {
Updater updateSvc.Updater
}

func NewServices(updater updateSvc.Updater) Services {
return Services{Updater: updater}
}
19 changes: 6 additions & 13 deletions cmd/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import (
"fmt"

"github.com/spf13/cobra"
"go.uber.org/zap"
)

func NewCmdExample(logger *zap.Logger) *Example {
return &Example{
logger: logger,
}
func init() {
Register("example", Example)
}

var customHelpTemplate = `
Expand Down Expand Up @@ -94,11 +91,7 @@ Docker
keploy test -c "docker run -p 8080:8080 --name <containerName> --network <networkName> <applicationImage>" --delay 1 --buildDelay 1m
`

type Example struct {
logger *zap.Logger
}

func (e *Example) GetCmd() *cobra.Command {
func Example() *cobra.Command {
var customSetup bool
var exampleCmd = &cobra.Command{
Use: "example",
Expand All @@ -111,10 +104,10 @@ func (e *Example) GetCmd() *cobra.Command {
}
if customSetup {
fmt.Println(examples)
} else {
fmt.Println(exampleOneClickInstall)
fmt.Println(withoutexampleOneClickInstall)
return nil
}
fmt.Println(exampleOneClickInstall)
fmt.Println(withoutexampleOneClickInstall)
return nil
},
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/generateConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func (g *GenerateConfig) GetCmd() *cobra.Command {
Use: "generate-config",
Short: "generate the keploy configuration file",
Example: "keploy generate-config --path /path/to/localdir",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// override root command's persistent pre run
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {

configPath, err := cmd.Flags().GetString("path")
Expand Down
Loading

0 comments on commit 1786447

Please sign in to comment.