Skip to content

Commit

Permalink
refactor: CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
labasubagia committed Sep 13, 2023
1 parent 5609e5c commit dc05356
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
go build -o app
- name: Run
run: nohup ./app start &
run: nohup ./app server &

- name: E2E
run: |
Expand Down
31 changes: 29 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,40 @@
"version": "0.2.0",
"configurations": [
{
"name": "Run Local Server",
"name": "Run Server",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"envFile": "${workspaceFolder}/.env",
"args": [
"start"
"server",
]
},
{
"name": "Run Server (Postgres)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"envFile": "${workspaceFolder}/.env",
"args": [
"server",
"-d",
"postgres"
]
},
{
"name": "Run Server (Mongo)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"envFile": "${workspaceFolder}/.env",
"args": [
"server",
"-d",
"mongo"
]
}
]
Expand Down
42 changes: 16 additions & 26 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
package cmd

import (
"fmt"
"log"

"github.com/labasubagia/realworld-backend/internal/core/util"
"github.com/spf13/cobra"
)

type Command struct {
config util.Config
rootCmd *cobra.Command
}
var config util.Config

func (c *Command) AddCommand(fnCommands ...func(config util.Config) *cobra.Command) {
commands := []*cobra.Command{}
for _, fn := range fnCommands {
commands = append(commands, fn(c.config))
}
c.rootCmd.AddCommand(commands...)
var rootCmd = &cobra.Command{
Use: "realworld",
Short: "Realworld backend app",
Long: "Realworld is an app about article similar medium.com and dev.to",
Run: func(cmd *cobra.Command, args []string) {},
}

func (c *Command) Execute() error {
return c.rootCmd.Execute()
}
func init() {
var err error

func NewCommand(config util.Config) *Command {
command := &Command{
config: config,
rootCmd: &cobra.Command{
Use: "app",
Short: "App about article",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("see help")
},
},
config, err = util.LoadConfig(".env")
if err != nil {
log.Fatal("failed to load env config", err)
}
command.AddCommand(startCmd)
return command
}

func Execute() error {
return rootCmd.Execute()
}
52 changes: 52 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cmd

import (
"fmt"
"log"
"strings"

"github.com/labasubagia/realworld-backend/internal/adapter/handler/restful"
"github.com/labasubagia/realworld-backend/internal/adapter/repository"
"github.com/labasubagia/realworld-backend/internal/core/service"
"github.com/spf13/cobra"
)

func init() {
dbOpts := []string{}
for option := range repository.RepoFnMap {
dbOpts = append(dbOpts, option)
}
dbOptStr := strings.Join(dbOpts, ",")

rootCmd.AddCommand(serverCmd)
serverCmd.Flags().StringP("database", "d", repository.DefaultRepoKey, fmt.Sprintf("select database in (%s)", dbOptStr))
}

var serverCmd = &cobra.Command{
Use: "server",
Short: "run http server",
Long: "Run gin server restful API",
Run: func(cmd *cobra.Command, args []string) {

dbType := cmd.Flag("database").Value.String()
dbType = strings.ToLower(dbType)
newRepo, exist := repository.RepoFnMap[dbType]
if !exist {
log.Fatal("invalid database", dbType)
}

repo, err := newRepo(config)
if err != nil {
log.Fatal("failed to load repository", err)
}
service, err := service.NewService(config, repo)
if err != nil {
log.Fatal("failed to load service", err)
}
server := restful.NewServer(config, service)

if server.Start(); err != nil {
log.Fatal("failed to load server", err)
}
},
}
32 changes: 0 additions & 32 deletions cmd/start.go

This file was deleted.

24 changes: 14 additions & 10 deletions internal/adapter/repository/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@ import (
"github.com/labasubagia/realworld-backend/internal/core/util"
)

const DefaultRepoKey = "default"

type FnNewRepo func(util.Config) (port.Repository, error)

var RepoFnMap = map[string]FnNewRepo{
DefaultRepoKey: sql.NewSQLRepository,
"postgres": sql.NewSQLRepository,
"mongo": mongo.NewMongoRepository,
}

func ListRepository(config util.Config) ([]port.Repository, error) {
type NewRepoFn func(util.Config) (port.Repository, error)
repoFns := []NewRepoFn{
mongo.NewMongoRepository,
sql.NewSQLRepository,
}
repos := make([]port.Repository, len(repoFns))
for i, fn := range repoFns {
repos := []port.Repository{}
for _, fn := range RepoFnMap {
repo, err := fn(config)
if err != nil {
return []port.Repository{}, err
}
repos[i] = repo
repos = append(repos, repo)
}
return repos, nil
}

func NewRepository(config util.Config) (port.Repository, error) {
return sql.NewSQLRepository(config)
// return mongo.NewMongoRepository(config)
return RepoFnMap[DefaultRepoKey](config)
}
9 changes: 1 addition & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@ import (
"log"

"github.com/labasubagia/realworld-backend/cmd"
"github.com/labasubagia/realworld-backend/internal/core/util"
)

func main() {
config, err := util.LoadConfig(".env")
if err != nil {
log.Fatal("failed to load config", err)
}
// run with go run main.go help
command := cmd.NewCommand(config)
if err := command.Execute(); err != nil {
if err := cmd.Execute(); err != nil {
log.Fatal("failed to run command", err)
}
}

0 comments on commit dc05356

Please sign in to comment.