Skip to content

Commit

Permalink
chore: closes #30 move StepLogger to deployment domain
Browse files Browse the repository at this point in the history
  • Loading branch information
YuukanOO committed Nov 6, 2023
1 parent 3564106 commit 030d731
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 76 deletions.
20 changes: 15 additions & 5 deletions internal/deployment/domain/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ package domain

import "context"

// Backend service used to run an application services.
type Backend interface {
Run(context.Context, Deployment) (Services, error) // Launch a deployment through the backend and return services that has been deployed
Cleanup(context.Context, App) error // Cleanup an application, which means removing every possible stuff related to it
}
type (
// Backend service used to run an application services.
Backend interface {
Run(context.Context, Deployment) (Services, error) // Launch a deployment through the backend and return services that has been deployed
Cleanup(context.Context, App) error // Cleanup an application, which means removing every possible stuff related to it
}

// Specific logger interface use by deployment jobs to document the deployment process.
StepLogger interface {
Stepf(string, ...any)
Warnf(string, ...any)
Infof(string, ...any)
Error(error)
}
)
4 changes: 2 additions & 2 deletions internal/deployment/domain/deployment_id.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package domain

// The deployment unique identifier is a composite key
// based on the app id and the deployment number.
type (
// The deployment unique identifier is a composite key
// based on the app id and the deployment number.
DeploymentNumber int

DeploymentID struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/deployment/domain/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Meta struct {
}

// Builds a new deployment meta from a kind and any additional data.
// You should never call it outside a Trigger implementation since
// You should never call it outside a Source implementation since
// it will be picked by a job to actually initiate the deployment.
func NewMeta(kind Kind, data string) Meta {
return Meta{kind, data}
Expand Down
11 changes: 6 additions & 5 deletions internal/deployment/infra/backend/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/YuukanOO/seelf/internal/deployment/domain"
"github.com/YuukanOO/seelf/internal/deployment/infra"
"github.com/YuukanOO/seelf/pkg/log"
"github.com/YuukanOO/seelf/pkg/ostools"
"github.com/compose-spec/compose-go/cli"
Expand Down Expand Up @@ -169,7 +170,7 @@ func (d *docker) Run(ctx context.Context, depl domain.Deployment) (domain.Servic

defer logfile.Close()

logger := log.NewStepLogger(logfile)
logger := infra.NewStepLogger(logfile)
logger.Stepf("configuring seelf docker project for environment: %s", depl.Config().Environment())

project, services, err := d.generateProject(depl, logger)
Expand All @@ -185,7 +186,7 @@ func (d *docker) Run(ctx context.Context, depl domain.Deployment) (domain.Servic
d.cli.Apply(command.WithStandardStreams())
}()

logger.Step("launching docker compose project (pulling, building and running)")
logger.Stepf("launching docker compose project (pulling, building and running)")

if err = d.compose.Up(ctx, project, api.UpOptions{
Create: api.CreateOptions{
Expand All @@ -201,7 +202,7 @@ func (d *docker) Run(ctx context.Context, depl domain.Deployment) (domain.Servic
}

if d.options.Domain().UseSSL() {
logger.Info("you may have to wait for certificates to be generated before your app is available")
logger.Infof("you may have to wait for certificates to be generated before your app is available")
}

// Remove dangling images
Expand All @@ -219,7 +220,7 @@ func (d *docker) Run(ctx context.Context, depl domain.Deployment) (domain.Servic
}
} else {
// If there's an error, we just log it and go on since it's not a critical one
logger.Warn(err.Error())
logger.Warnf(err.Error())
}

return services, nil
Expand Down Expand Up @@ -364,7 +365,7 @@ func (d *docker) setupComposeIfNeeded() error {
//
// The goal is really for the user to provide a docker-compose file which runs fine locally
// and we should do our best to expose it accordingly without the user providing anything.
func (d *docker) generateProject(depl domain.Deployment, logger log.StepLogger) (*types.Project, domain.Services, error) {
func (d *docker) generateProject(depl domain.Deployment, logger domain.StepLogger) (*types.Project, domain.Services, error) {
var (
services domain.Services
buildDir = depl.Path(d.options.AppsDir())
Expand Down
37 changes: 37 additions & 0 deletions internal/deployment/infra/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package infra

import (
"fmt"
"io"

"github.com/YuukanOO/seelf/internal/deployment/domain"
)

type stepLogger struct {
writer io.Writer
}

// Instantiates a new step logger to provide a simple way to build a deployment logfile.
func NewStepLogger(writer io.Writer) domain.StepLogger {
return &stepLogger{writer}
}

func (l *stepLogger) Stepf(format string, args ...any) {
l.print("[STEP]", format, args)
}

func (l *stepLogger) Warnf(format string, args ...any) {
l.print("[WARN]", format, args)
}

func (l *stepLogger) Infof(format string, args ...any) {
l.print("[INFO]", format, args)
}

func (l *stepLogger) Error(err error) {
l.print("[ERROR]", err.Error(), nil)
}

func (l *stepLogger) print(prefix string, format string, args []any) {
l.writer.Write([]byte(fmt.Sprintf("%s %s\n", prefix, fmt.Sprintf(format, args...))))
}
4 changes: 2 additions & 2 deletions internal/deployment/infra/source/archive/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"path/filepath"

"github.com/YuukanOO/seelf/internal/deployment/domain"
"github.com/YuukanOO/seelf/internal/deployment/infra"
"github.com/YuukanOO/seelf/internal/deployment/infra/source"
"github.com/YuukanOO/seelf/pkg/log"
"github.com/YuukanOO/seelf/pkg/ostools"
)

Expand Down Expand Up @@ -94,7 +94,7 @@ func (t *service) Fetch(ctx context.Context, depl domain.Deployment) error {

defer logfile.Close()

logger := log.NewStepLogger(logfile)
logger := infra.NewStepLogger(logfile)

buildDir := depl.Path(t.options.AppsDir())

Expand Down
4 changes: 2 additions & 2 deletions internal/deployment/infra/source/git/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
sstrings "strings"

"github.com/YuukanOO/seelf/internal/deployment/domain"
"github.com/YuukanOO/seelf/internal/deployment/infra"
"github.com/go-git/go-git/v5/config"

"github.com/YuukanOO/seelf/internal/deployment/infra/source"
"github.com/YuukanOO/seelf/pkg/apperr"
"github.com/YuukanOO/seelf/pkg/log"
"github.com/YuukanOO/seelf/pkg/monad"
"github.com/YuukanOO/seelf/pkg/ostools"
"github.com/YuukanOO/seelf/pkg/validation"
Expand Down Expand Up @@ -115,7 +115,7 @@ func (s *service) Fetch(ctx context.Context, depl domain.Deployment) error {

defer logfile.Close()

logger := log.NewStepLogger(logfile)
logger := infra.NewStepLogger(logfile)

// Retrieve git url and token from the app
app, err := s.reader.GetByID(context.Background(), depl.ID().AppID())
Expand Down
4 changes: 2 additions & 2 deletions internal/deployment/infra/source/raw/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"path/filepath"

"github.com/YuukanOO/seelf/internal/deployment/domain"
"github.com/YuukanOO/seelf/internal/deployment/infra"
"github.com/YuukanOO/seelf/internal/deployment/infra/source"
"github.com/YuukanOO/seelf/pkg/log"
"github.com/YuukanOO/seelf/pkg/ostools"
"github.com/YuukanOO/seelf/pkg/validation"
"github.com/YuukanOO/seelf/pkg/validation/strings"
Expand Down Expand Up @@ -69,7 +69,7 @@ func (s *service) Fetch(ctx context.Context, depl domain.Deployment) error {

defer logfile.Close()

logger := log.NewStepLogger(logfile)
logger := infra.NewStepLogger(logfile)

buildDir := depl.Path(s.options.AppsDir())

Expand Down
57 changes: 0 additions & 57 deletions pkg/log/logger.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package log

import (
"fmt"
"io"

"go.uber.org/zap"
)

Expand Down Expand Up @@ -40,57 +37,3 @@ func NewLogger(verbose bool) Logger {

return logger.Sugar()
}

type (
// Specific logger interface use by deployment jobs to document the deployment process.
StepLogger interface {
Step(string)
Stepf(string, ...any)
Warn(string)
Warnf(string, ...any)
Info(string)
Infof(string, ...any)
Error(error)
}

stepLogger struct {
writer io.Writer
}
)

// Instantiates a new step logger to provide a simple way to build a deployment logfile.
func NewStepLogger(writer io.Writer) StepLogger {
return &stepLogger{writer}
}

func (l *stepLogger) Step(msg string) {
l.print("[STEP]", msg)
}

func (l *stepLogger) Stepf(format string, args ...any) {
l.Step(fmt.Sprintf(format, args...))
}

func (l *stepLogger) Warn(msg string) {
l.print("[WARN]", msg)
}

func (l *stepLogger) Warnf(format string, args ...any) {
l.Warn(fmt.Sprintf(format, args...))
}

func (l *stepLogger) Info(msg string) {
l.print("[INFO]", msg)
}

func (l *stepLogger) Infof(format string, args ...any) {
l.Info(fmt.Sprintf(format, args...))
}

func (l *stepLogger) Error(err error) {
l.print("[ERROR]", err)
}

func (l *stepLogger) print(prefix string, msg any) {
l.writer.Write([]byte(fmt.Sprintf("%s %s\n", prefix, msg)))
}

0 comments on commit 030d731

Please sign in to comment.