Skip to content

Commit

Permalink
remove init functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mirzakhany committed Sep 12, 2022
1 parent 18b3536 commit f5228f5
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 84 deletions.
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
TAG_NAME?=$(shell git describe --tags)
SHORT_SHA?=$(shell git rev-parse --short HEAD)
VERSION?=$(TAG_NAME)-$(SHORT_SHA)
LDFLAGS=-ldflags "-X=cmd.version=$(VERSION)"
LDFLAGS=-ldflags "-X=main.version=$(VERSION)"
GOCMD?=CGO_ENABLED=0 go
GO_MAIN_SRC?=main.go
GO_MAIN_SRC?=.

##@ General

Expand Down Expand Up @@ -43,8 +43,9 @@ vendor: ## Reset the main module's vendor directory to include all packages.

.PHONY: build
build: ## Build service binary.
$(GOCMD) build -mod vendor $(LDFLAGS) -o dbctl .
$(GOCMD) build -mod vendor $(LDFLAGS) -o dbctl $(GO_MAIN_SRC)

.PHONY: install
install: ## build and install the dbctl
$(GOCMD) install -mod vendor $(LDFLAGS) .
$(GOCMD) install -mod vendor $(LDFLAGS) $(GO_MAIN_SRC)

24 changes: 0 additions & 24 deletions cmd/root.go

This file was deleted.

17 changes: 0 additions & 17 deletions cmd/start.go

This file was deleted.

23 changes: 0 additions & 23 deletions cmd/version.go

This file was deleted.

28 changes: 14 additions & 14 deletions cmd/pg.go → internal/cmd/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ import (
"github.com/spf13/cobra"
)

// pgCmd represents the pg command
var pgCmd = &cobra.Command{
Use: "pg",
Short: "Run a postgres instance",
RunE: runPostgres,
}
// GetPgCmd represents the pg command
func GetPgCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "pg",
Short: "Run a postgres instance",
RunE: runPostgres,
}

func init() {
startCmd.AddCommand(pgCmd)
cmd.Flags().Uint32P("port", "p", 15432, "postgres default port")
cmd.Flags().StringP("user", "u", "postgres", "Database username")
cmd.Flags().String("pass", "postgres", "Database password")
cmd.Flags().StringP("name", "n", "postgres", "Database name")
cmd.Flags().StringP("version", "v", "", "Database version, default for native 14.3.0 and 14.3.2 for docker engine")
cmd.Flags().StringP("migrations", "m", "", "Relative path to migration files, will be applied if provided")

pgCmd.Flags().Uint32P("port", "p", 15432, "postgres default port")
pgCmd.Flags().StringP("user", "u", "postgres", "Database username")
pgCmd.Flags().String("pass", "postgres", "Database password")
pgCmd.Flags().StringP("name", "n", "postgres", "Database name")
pgCmd.Flags().StringP("version", "v", "", "Database version, default for native 14.3.0 and 14.3.2 for docker engine")
pgCmd.Flags().StringP("migrations", "m", "", "Relative path to migration files, will be applied if provided")
return cmd
}

func runPostgres(cmd *cobra.Command, args []string) error {
Expand Down
18 changes: 18 additions & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"github.com/spf13/cobra"
)

// GetRootCmd represents the base command when called without any subcommands
func GetRootCmd(version string) *cobra.Command {
cmd := &cobra.Command{
Use: "dbctl",
Version: version,
Short: "Your swish knife of testing databases",
Long: `Dbctl is a command line tools, providing simple
command to run and manage databases for tests proposes`,
}

return cmd
}
19 changes: 19 additions & 0 deletions internal/cmd/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"github.com/spf13/cobra"
)

// GetStartCmd represents the start command
func GetStartCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: "Start a database instance",
}

cmd.PersistentFlags().BoolP("detach", "d", false, "Detached mode: Run database in the background")
cmd.PersistentFlags().Bool("use-docker", true, "Use Docker to run databases")

cmd.AddCommand(GetPgCmd())
return cmd
}
18 changes: 18 additions & 0 deletions internal/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// GetVersionCmd represents the version command
func GetVersionCmd(version string) *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Show the dbctl version information",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("dbctl version %s\n", version)
},
}
}
19 changes: 17 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,23 @@ Copyright © 2022 Mohsen Mirzakhani <[email protected]>
*/
package main

import "github.com/mirzakhany/dbctl/cmd"
import (
"fmt"
"os"

"github.com/mirzakhany/dbctl/internal/cmd"
)

// version will be populated by the build script with the sha of the last git commit.
var version = "snapshot"

func main() {
cmd.Execute()
root := cmd.GetRootCmd(version)
root.SetVersionTemplate(fmt.Sprintf("dbctl version %s\n", version))

root.AddCommand(cmd.GetStartCmd())

if err := root.Execute(); err != nil {
os.Exit(1)
}
}

0 comments on commit f5228f5

Please sign in to comment.