Skip to content

Commit

Permalink
Improve package structure
Browse files Browse the repository at this point in the history
  • Loading branch information
bkuen committed Dec 27, 2023
1 parent 736d07d commit 741c142
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 78 deletions.
4 changes: 2 additions & 2 deletions cmd/bwizard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"bwizard/internal/pkg/ssh"
"bwizard/internal/pkg/wizard/application"
"bwizard/internal/pkg/wizard/infrastructure/mysql"
"bwizard/internal/pkg/wizard/infrastructure/postgres"
"bwizard/internal/pkg/wizardapi"
"github.com/rs/zerolog"
"os"
Expand Down Expand Up @@ -35,7 +35,7 @@ func main() {
os.Exit(1)
}

db, err := mysql.Connect(&logger)
db, err := postgres.Connect(&logger)
if err != nil {
logger.Error().Msg(err.Error())
os.Exit(1)
Expand Down
23 changes: 10 additions & 13 deletions internal/pkg/wizard/application/application.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package application

import (
deviceSvcImpl "bwizard/internal/pkg/wizard/application/svc/device"
"bwizard/internal/pkg/wizard/domain/entity/device"
deviceRepo "bwizard/internal/pkg/wizard/domain/repo/device"
deviceSvc "bwizard/internal/pkg/wizard/domain/svc/device"
deviceValue "bwizard/internal/pkg/wizard/domain/valueobject/device"
deviceRepoImpl "bwizard/internal/pkg/wizard/infrastructure/mysql/repo/device"
deviceSvc "bwizard/internal/pkg/wizard/application/device"
"bwizard/internal/pkg/wizard/domain/device"
deviceRepo "bwizard/internal/pkg/wizard/infrastructure/postgres/device"
"context"
"errors"
_ "github.com/doug-martin/goqu/v9/dialect/mysql"
Expand All @@ -16,21 +13,21 @@ import (

type Application interface {
// SetupDevice setups a new device
SetupDevice(ctx context.Context, ips []string, kind deviceValue.Kind, name *string) (*device.Device, error)
SetupDevice(ctx context.Context, ips []string, kind device.Kind, name *string) (*device.Device, error)
}

type Impl struct {
logger *zerolog.Logger
DeviceRepo deviceRepo.Repository
DeviceInspectionSvc deviceSvc.InspectionService
DeviceRepo device.Repository
DeviceInspectionSvc device.InspectionService
}

var _ Application = &Impl{}

// NewApplication returns a new application
func NewApplication(logger *zerolog.Logger, db *sqlx.DB) *Impl {
deviceRepository := deviceRepoImpl.NewRepository(logger, db)
deviceInspectionSvc := deviceSvcImpl.NewInspectionSvc(logger)
deviceRepository := deviceRepo.NewRepository(logger, db)
deviceInspectionSvc := deviceSvc.NewInspectionSvc(logger)

return &Impl{
logger: logger,
Expand All @@ -40,7 +37,7 @@ func NewApplication(logger *zerolog.Logger, db *sqlx.DB) *Impl {
}

// SetupDevice setups a new device
func (i *Impl) SetupDevice(ctx context.Context, ips []string, kind deviceValue.Kind, name *string) (*device.Device, error) {
func (i *Impl) SetupDevice(ctx context.Context, ips []string, kind device.Kind, name *string) (*device.Device, error) {
inspection, err := i.DeviceInspectionSvc.Inspect(ips)
if err != nil {
return nil, err
Expand All @@ -61,7 +58,7 @@ func (i *Impl) SetupDevice(ctx context.Context, ips []string, kind deviceValue.K
Name(*name).
Hostname(inspection.Hostname).
Kind(kind).
Protection(deviceValue.ProtectionStatusOk).
Protection(device.ProtectionStatusOk).
CPU(inspection.CPU).
Ram(inspection.Ram).
Disks(inspection.Disks).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package device
import (
"bwizard/internal/pkg/inspect"
"bwizard/internal/pkg/ssh"
deviceSvc "bwizard/internal/pkg/wizard/domain/svc/device"
deviceValue "bwizard/internal/pkg/wizard/domain/valueobject/device"
"bwizard/internal/pkg/wizard/domain/device"
"github.com/rs/zerolog"
)

Expand All @@ -16,7 +15,7 @@ type InspectionSvcImpl struct {
logger *zerolog.Logger
}

var _ deviceSvc.InspectionService = &InspectionSvcImpl{}
var _ device.InspectionService = &InspectionSvcImpl{}

func NewInspectionSvc(logger *zerolog.Logger) *InspectionSvcImpl {
return &InspectionSvcImpl{
Expand All @@ -25,7 +24,7 @@ func NewInspectionSvc(logger *zerolog.Logger) *InspectionSvcImpl {
}

// Inspect gains information from a backup device such like the operating system or the agent
func (i *InspectionSvcImpl) Inspect(ips []string) (*deviceValue.Inspection, error) {
func (i *InspectionSvcImpl) Inspect(ips []string) (*device.Inspection, error) {
credentials := ssh.Credentials{
Username: "root",
PrivateKeyPath: privateKeyPath,
Expand Down Expand Up @@ -57,7 +56,7 @@ func (i *InspectionSvcImpl) Inspect(ips []string) (*deviceValue.Inspection, erro
return nil, err
}

return deviceValue.NewInspectionFromDeviceInspection(inspection), nil
return device.NewInspectionFromDeviceInspection(inspection), nil
}

defer func(clients []*ssh.Client) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package device

import (
"bwizard/internal/pkg/wizard/domain/valueobject/device"
"github.com/gofrs/uuid/v5"
"time"
)
Expand Down Expand Up @@ -34,13 +33,13 @@ func (b *Builder) Hostname(v string) *Builder {
return b
}

func (b *Builder) Kind(v device.Kind) *Builder {
func (b *Builder) Kind(v Kind) *Builder {
b.Device.Kind = v

return b
}

func (b *Builder) Protection(v device.ProtectionStatus) *Builder {
func (b *Builder) Protection(v ProtectionStatus) *Builder {
b.Device.Protection = v

return b
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package device

import (
"bwizard/internal/pkg/wizard/domain/valueobject/device"
"github.com/gofrs/uuid/v5"
"time"
)
Expand All @@ -10,8 +9,8 @@ type Device struct {
ID uuid.UUID
Name string
Hostname string
Kind device.Kind
Protection device.ProtectionStatus
Kind Kind
Protection ProtectionStatus
LastBackup *time.Time
CPU string
Ram string
Expand Down
12 changes: 12 additions & 0 deletions internal/pkg/wizard/domain/device/repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package device

import (
"context"
"github.com/google/uuid"
)

type Repository interface {
GetDeviceByID(ctx context.Context, id uuid.UUID) (*Device, error)
GetDevices(ctx context.Context) ([]*Device, error)
SaveDevice(ctx context.Context, device *Device) error
}
5 changes: 5 additions & 0 deletions internal/pkg/wizard/domain/device/svc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package device

type InspectionService interface {
Inspect(ips []string) (*Inspection, error)
}
13 changes: 0 additions & 13 deletions internal/pkg/wizard/domain/repo/device/device.go

This file was deleted.

9 changes: 0 additions & 9 deletions internal/pkg/wizard/domain/svc/device/device.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mysql
package postgres

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package device

import (
"bwizard/internal/pkg/wizard/domain/entity/device"
deviceValue "bwizard/internal/pkg/wizard/domain/valueobject/device"
"bwizard/internal/pkg/wizard/domain/device"
"github.com/gofrs/uuid/v5"
"github.com/lib/pq"
"time"
)

type Device struct {
ID uuid.UUID `db:"id"`
Name string `db:"name"`
Hostname string `db:"hostname"`
Kind deviceValue.Kind `db:"kind"`
Protection deviceValue.ProtectionStatus `db:"protection"`
LastBackup *time.Time `db:"last_backup" goqu:"omitempty"`
CPU string `db:"cpu"`
Ram string `db:"ram"`
Disks pq.StringArray `db:"disks"`
IPs pq.StringArray `db:"ips"`
OperatingSystem string `db:"operating_system"`
Agent string `db:"agent"`
CreatedAt time.Time `db:"created_at" goqu:"skipinsert,skipupdate"`
UpdatedAt time.Time `db:"updated_at" goqu:"skipinsert"`
ID uuid.UUID `db:"id"`
Name string `db:"name"`
Hostname string `db:"hostname"`
Kind device.Kind `db:"kind"`
Protection device.ProtectionStatus `db:"protection"`
LastBackup *time.Time `db:"last_backup" goqu:"omitempty"`
CPU string `db:"cpu"`
Ram string `db:"ram"`
Disks pq.StringArray `db:"disks"`
IPs pq.StringArray `db:"ips"`
OperatingSystem string `db:"operating_system"`
Agent string `db:"agent"`
CreatedAt time.Time `db:"created_at" goqu:"skipinsert,skipupdate"`
UpdatedAt time.Time `db:"updated_at" goqu:"skipinsert"`
}

func MapModel(deviceEntity *Device) *device.Device {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package device

import (
"bwizard/internal/pkg/wizard/domain/entity/device"
device2 "bwizard/internal/pkg/wizard/domain/repo/device"
deviceModel "bwizard/internal/pkg/wizard/infrastructure/mysql/model/device"
"bwizard/internal/pkg/wizard/domain/device"
"context"
"database/sql"
"errors"
Expand All @@ -24,7 +22,7 @@ type Impl struct {
db *sqlx.DB
}

var _ device2.Repository = &Impl{}
var _ device.Repository = &Impl{}

// NewRepository returns a new mysql device repository
func NewRepository(logger *zerolog.Logger, db *sqlx.DB) *Impl {
Expand Down Expand Up @@ -65,7 +63,7 @@ func (i *Impl) GetDevices(ctx context.Context) ([]*device.Device, error) {
query, _, err := goqu.
Dialect(Dialect).
From(TableName).
Select(&deviceModel.Device{}).
Select(&Device{}).
ToSQL()

if err != nil {
Expand All @@ -84,12 +82,12 @@ func (i *Impl) GetDevices(ctx context.Context) ([]*device.Device, error) {
devices := make([]*device.Device, 0)

for rows.Next() {
var model deviceModel.Device
var model Device
if scanErr := rows.StructScan(&model); scanErr != nil {
return nil, scanErr
}

deviceEntity := deviceModel.MapModel(&model)
deviceEntity := MapModel(&model)
devices = append(devices, deviceEntity)
}

Expand All @@ -100,7 +98,7 @@ func (i *Impl) GetDevices(ctx context.Context) ([]*device.Device, error) {
func (i *Impl) SaveDevice(ctx context.Context, device *device.Device) error {
i.logger.Debug().Msgf("try to save device %s", device.ID.String())

model := deviceModel.MapDevice(device)
model := MapDevice(device)

insertSQL, _, _ := goqu.
Dialect(Dialect).
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/wizardapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package wizardapi
import (
api "bwizard/api/openapi/wizard"
"bwizard/internal/pkg/wizard/application"
deviceRepo "bwizard/internal/pkg/wizard/domain/repo/device"
deviceRepo "bwizard/internal/pkg/wizard/domain/device"
"flag"
"github.com/labstack/echo/v4"
echoMiddleware "github.com/labstack/echo/v4/middleware"
Expand Down
5 changes: 2 additions & 3 deletions internal/pkg/wizardapi/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package wizardapi
import (
"bwizard/api/openapi/wizard"
"bwizard/internal/pkg/wizard/application"
deviceRepo "bwizard/internal/pkg/wizard/domain/repo/device"
deviceValue "bwizard/internal/pkg/wizard/domain/valueobject/device"
deviceRepo "bwizard/internal/pkg/wizard/domain/device"
"bwizard/internal/pkg/wizardapi/mappings"
"context"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -71,7 +70,7 @@ func (h *Handler) FindDevices(ctx context.Context, request wizard.FindDevicesReq
}

func (h *Handler) RegisterDevice(ctx context.Context, request wizard.RegisterDeviceRequestObject) (wizard.RegisterDeviceResponseObject, error) {
device, err := h.app.SetupDevice(ctx, request.Body.Ips, deviceValue.Kind(request.Body.Kind), request.Body.Name)
device, err := h.app.SetupDevice(ctx, request.Body.Ips, deviceRepo.Kind(request.Body.Kind), request.Body.Name)
if err != nil {
h.logger.Error().Msgf("%s", err.Error())

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/wizardapi/mappings/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package mappings

import (
"bwizard/api/openapi/wizard"
"bwizard/internal/pkg/wizard/domain/entity/device"
"bwizard/internal/pkg/wizard/domain/device"
"github.com/google/uuid"
)

Expand Down

0 comments on commit 741c142

Please sign in to comment.