Skip to content

Commit

Permalink
Merge branch 'main' into config-prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
spatocode authored Sep 17, 2023
2 parents 96ab2d5 + 825b1ab commit e2e8423
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cloud/aws/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *S3) Upload(filePath string) error {
defer file.Close()

fileName := filepath.Base(filePath)
log.Debug(fmt.Sprintf("uploading file %s...\n", fileName))
log.Debug(fmt.Sprintf("uploading file %s...", fileName))
_, err = client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(s.config.Bucket),
Key: aws.String(fileName),
Expand Down
2 changes: 1 addition & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var deployCmd = &cobra.Command{
Short: "Deploy an application",
Long: "Deploy an application",
Run: func(cmd *cobra.Command, args []string) {
// verbose, _ := cmd.Flags().GetBool("verbose")
jerm.Verbose(cmd)

cfg, err := jerm.ReadConfig(jerm.DefaultConfigFile)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions cmd/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var statusCmd = &cobra.Command{
Short: "Show deployment logs",
Long: "Show deployment logs",
Run: func(cmd *cobra.Command, args []string) {
jerm.Verbose(cmd)

config, err := jerm.ReadConfig(jerm.DefaultConfigFile)
if err != nil {
var pErr *os.PathError
Expand Down
1 change: 1 addition & 0 deletions cmd/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var rollbackCmd = &cobra.Command{
Long: "Rolls back to the previous versions of the deployment",
Run: func(cmd *cobra.Command, args []string) {
steps, _ := cmd.Flags().GetInt("steps")
jerm.Verbose(cmd)

config, err := jerm.ReadConfig(jerm.DefaultConfigFile)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Enable verbose mode")
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "enable verbose mode")
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.jerm.yaml)")

// Cobra also supports local flags, which will only run
Expand Down
2 changes: 2 additions & 0 deletions cmd/undeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var undeployCmd = &cobra.Command{
Short: "Undeploy a deployed application",
Long: "Undeploy a deployed application",
Run: func(cmd *cobra.Command, args []string) {
jerm.Verbose(cmd)

config, err := jerm.ReadConfig(jerm.DefaultConfigFile)
if err != nil {
var pErr *os.PathError
Expand Down
123 changes: 123 additions & 0 deletions internal/log/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package log

import (
"context"
"fmt"
"io"
"log/slog"
"runtime"
"strconv"
"sync"
"time"

"github.com/fatih/color"
)

type Handler struct {
opts slog.HandlerOptions
prefix string
preformat string
mu *sync.Mutex
w io.Writer
}

func New(w io.Writer, opts *slog.HandlerOptions) *Handler {
h := &Handler{w: w, mu: &sync.Mutex{}}
if opts != nil {
h.opts = *opts
}
if h.opts.Level == nil {
h.opts.Level = slog.LevelInfo
}
return h
}

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool {
return level >= h.opts.Level.Level()
}

func (h *Handler) Handle(ctx context.Context, r slog.Record) error {
level := r.Level.String()
buf := make([]byte, 0, 1024)

if !r.Time.IsZero() {
buf = r.Time.AppendFormat(buf, time.RFC3339)
buf = append(buf, ' ')
}

switch r.Level {
case slog.LevelDebug:
level = color.MagentaString(level)
case slog.LevelInfo:
level = color.CyanString(level)
case slog.LevelWarn:
level = color.YellowString(level)
case slog.LevelError:
level = color.RedString(level)
}

buf = append(buf, level...)
buf = append(buf, ' ')
if h.opts.AddSource && r.PC != 0 {
fr := runtime.CallersFrames([]uintptr{r.PC})
f, _ := fr.Next()
buf = append(buf, f.File...)
buf = append(buf, ':')
buf = strconv.AppendInt(buf, int64(f.Line), 10)
buf = append(buf, ' ')
}
buf = append(buf, color.CyanString(r.Message)...)
buf = append(buf, h.preformat...)
r.Attrs(func(a slog.Attr) bool {
buf = h.appendAttr(buf, h.prefix, a)
return true
})
buf = append(buf, '\n')
h.mu.Lock()
defer h.mu.Unlock()
_, err := h.w.Write(buf)
return err
}

func (h *Handler) appendAttr(buf []byte, prefix string, a slog.Attr) []byte {
if a.Equal(slog.Attr{}) {
return buf
}
if a.Value.Kind() != slog.KindGroup {
buf = append(buf, ' ')
buf = append(buf, prefix...)
buf = append(buf, a.Key...)
buf = append(buf, '=')
return fmt.Appendf(buf, "%v", a.Value.Any())
}

if a.Key != "" {
prefix += a.Key + "."
}
for _, a := range a.Value.Group() {
buf = h.appendAttr(buf, prefix, a)
}
return buf
}

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler {
var buf []byte
for _, a := range attrs {
buf = h.appendAttr(buf, h.prefix, a)
}
return &Handler{
w: h.w,
opts: h.opts,
prefix: h.prefix,
preformat: h.preformat + string(buf),
}
}

func (h *Handler) WithGroup(name string) slog.Handler {
return &Handler{
w: h.w,
opts: h.opts,
preformat: h.preformat,
prefix: h.prefix + name + ".",
}
}
138 changes: 138 additions & 0 deletions internal/log/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package log

import (
"bytes"
"context"
"time"

"log/slog"
"testing"

"github.com/stretchr/testify/assert"
)

var testTime = time.Date(2023, time.September, 17, 06, 44, 13, 0, time.UTC)

func TestInfoLevelOutput(t *testing.T) {
assert := assert.New(t)
var buf bytes.Buffer

h := New(&buf, nil)
logger := slog.New(logtimeHandler{testTime, h})
logger.Log(context.Background(), slog.LevelInfo, "testing jerm custom slog handler")

actual := buf.String()
actual = actual[:len(actual)-1]
expected := "2023-09-17T06:44:13Z INFO testing jerm custom slog handler"
assert.Equal(expected, actual)
}

func TestDebugLevelOutput(t *testing.T) {
assert := assert.New(t)
var buf bytes.Buffer

logLevel := &slog.LevelVar{}
logLevel.Set(slog.LevelDebug)
h := New(&buf, &slog.HandlerOptions{
Level: logLevel,
})
logger := slog.New(logtimeHandler{testTime, h})
logger.Log(context.Background(), slog.LevelDebug, "testing jerm custom slog handler")

actual := buf.String()
actual = actual[:len(actual)-1]
expected := "2023-09-17T06:44:13Z DEBUG testing jerm custom slog handler"
assert.Equal(expected, actual)
}

func TestErrorLevelOutput(t *testing.T) {
assert := assert.New(t)
var buf bytes.Buffer

logLevel := &slog.LevelVar{}
logLevel.Set(slog.LevelError)
h := New(&buf, &slog.HandlerOptions{
Level: logLevel,
})
logger := slog.New(logtimeHandler{testTime, h})
logger.Log(context.Background(), slog.LevelError, "testing jerm custom slog handler")

actual := buf.String()
actual = actual[:len(actual)-1]
expected := "2023-09-17T06:44:13Z ERROR testing jerm custom slog handler"
assert.Equal(expected, actual)
}

func TestWarnLevelOutput(t *testing.T) {
assert := assert.New(t)
var buf bytes.Buffer

logLevel := &slog.LevelVar{}
logLevel.Set(slog.LevelWarn)
h := New(&buf, &slog.HandlerOptions{
Level: logLevel,
})
logger := slog.New(logtimeHandler{testTime, h})
logger.Log(context.Background(), slog.LevelWarn, "testing jerm custom slog handler")

actual := buf.String()
actual = actual[:len(actual)-1]
expected := "2023-09-17T06:44:13Z WARN testing jerm custom slog handler"
assert.Equal(expected, actual)
}

func TestOutputWithAttr(t *testing.T) {
assert := assert.New(t)
var buf bytes.Buffer

h := New(&buf, nil)
logger := slog.New(logtimeHandler{testTime, h})
attr := []slog.Attr{slog.String("t", "test"), slog.Bool("b", true)}
logger.LogAttrs(context.Background(), slog.LevelInfo, "testing jerm custom slog handler", attr...)

actual := buf.String()
actual = actual[:len(actual)-1]
expected := "2023-09-17T06:44:13Z INFO testing jerm custom slog handler t=test b=true"
assert.Equal(expected, actual)
}

func TestOutputWithGroup(t *testing.T) {
assert := assert.New(t)
var buf bytes.Buffer

h := New(&buf, nil)
logger := slog.New(logtimeHandler{testTime, h})
attr := []slog.Attr{
slog.String("t", "test"),
slog.Group("g", slog.Int("a", 1), slog.Int("b", 2)),
slog.Bool("b", true),
}
logger.LogAttrs(context.Background(), slog.LevelInfo, "testing jerm custom slog handler", attr...)

actual := buf.String()
actual = actual[:len(actual)-1]
expected := "2023-09-17T06:44:13Z INFO testing jerm custom slog handler t=test g.a=1 g.b=2 b=true"
assert.Equal(expected, actual)
}

type logtimeHandler struct {
t time.Time
h slog.Handler
}

func (h logtimeHandler) Enabled(ctx context.Context, level slog.Level) bool {
return h.h.Enabled(ctx, level)
}

func (h logtimeHandler) WithGroup(name string) slog.Handler {
return logtimeHandler{h.t, h.h.WithGroup(name)}
}

func (h logtimeHandler) Handle(ctx context.Context, r slog.Record) error {
r.Time = h.t
return h.h.Handle(ctx, r)
}

func (h logtimeHandler) WithAttrs(as []slog.Attr) slog.Handler {
return logtimeHandler{h.t, h.h.WithAttrs(as)}
}
24 changes: 24 additions & 0 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"log/slog"
"os"

"github.com/fatih/color"
)
Expand All @@ -22,17 +23,40 @@ var (
)

func Info(msg string, v ...interface{}) {
logLevel := &slog.LevelVar{}
logLevel.Set(slog.LevelInfo)
slog.SetDefault(slog.New(New(os.Stdout, &slog.HandlerOptions{
Level: logLevel,
})))
slog.Info(msg, v...)
}

func Debug(msg string, v ...interface{}) {
if os.Getenv("JERM_VERBOSE") != "1" {
return
}
logLevel := &slog.LevelVar{}
logLevel.Set(slog.LevelDebug)
slog.SetDefault(slog.New(New(os.Stdout, &slog.HandlerOptions{
Level: logLevel,
})))
slog.Debug(msg, v...)
}

func Warn(msg string, v ...interface{}) {
logLevel := &slog.LevelVar{}
logLevel.Set(slog.LevelWarn)
slog.SetDefault(slog.New(New(os.Stdout, &slog.HandlerOptions{
Level: logLevel,
})))
slog.Warn(msg, v...)
}

func Error(msg string, v ...interface{}) {
logLevel := &slog.LevelVar{}
logLevel.Set(slog.LevelError)
slog.SetDefault(slog.New(New(os.Stdout, &slog.HandlerOptions{
Level: logLevel,
})))
slog.Error(msg, v...)
}
10 changes: 10 additions & 0 deletions jerm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/spatocode/jerm/config"
"github.com/spatocode/jerm/internal/log"
"github.com/spf13/cobra"
)

// Config is the Jerm configuration
Expand Down Expand Up @@ -207,3 +208,12 @@ func (p *Project) archivePackage(archivePath, dir string) (int64, error) {

return info.Size(), err
}

func Verbose(cmd *cobra.Command) {
verbose, _ := cmd.Flags().GetBool("verbose")
if verbose {
os.Setenv("JERM_VERBOSE", "1")
} else {
os.Setenv("JERM_VERBOSE", "0")
}
}

0 comments on commit e2e8423

Please sign in to comment.