Skip to content

Commit

Permalink
Merge pull request #53 from leancodepl/task/better-logging
Browse files Browse the repository at this point in the history
Add more visually pleasing logging
  • Loading branch information
Albert221 authored Jan 20, 2023
2 parents c082902 + add8a3d commit 8d945bf
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 16 deletions.
35 changes: 27 additions & 8 deletions cmd/poe.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/leancodepl/poe2arb/converter"
"github.com/leancodepl/poe2arb/flutter"
"github.com/leancodepl/poe2arb/log"
"github.com/leancodepl/poe2arb/poeditor"
"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -18,7 +19,9 @@ var poeCmd = &cobra.Command{
Use: "poe",
Short: "Exports POEditor terms and converts them to ARB. " +
"Must be run from the Flutter project root directory or its subdirectory.",
RunE: runPoe,
SilenceErrors: true,
SilenceUsage: true,
RunE: runPoe,
}

const (
Expand All @@ -36,23 +39,32 @@ func init() {
}

func runPoe(cmd *cobra.Command, args []string) error {
log := getLogger(cmd)

logSub := log.Info("loading options").Sub()

sel, err := getOptionsSelector(cmd)
if err != nil {
logSub.Error("failed " + err.Error())
return err
}

options, err := sel.SelectOptions()
if err != nil {
logSub.Error("failed: " + err.Error())
return err
}

poeCmd, err := NewPoeCommand(options)
poeCmd, err := NewPoeCommand(options, log)
if err != nil {
logSub.Error(err.Error())
return err
}

log.Info("fetching project languages")
langs, err := poeCmd.GetExportLanguages()
if err != nil {
logSub.Error(err.Error())
return err
}

Expand All @@ -70,7 +82,7 @@ func runPoe(cmd *cobra.Command, args []string) error {
}
}

fmt.Println("\nDone!")
log.Success("done")

return nil
}
Expand Down Expand Up @@ -110,9 +122,10 @@ func getFlutterConfig() (*flutter.FlutterConfig, error) {
type poeCommand struct {
options *poeOptions
client *poeditor.Client
log *log.Logger
}

func NewPoeCommand(options *poeOptions) (*poeCommand, error) {
func NewPoeCommand(options *poeOptions, log *log.Logger) (*poeCommand, error) {
if errs := validatePoeOptions(options); len(errs) > 0 {
msg := ""
for _, err := range errs {
Expand All @@ -126,6 +139,7 @@ func NewPoeCommand(options *poeOptions) (*poeCommand, error) {
return &poeCommand{
options: options,
client: client,
log: log,
}, nil
}

Expand All @@ -144,7 +158,6 @@ func validatePoeOptions(options *poeOptions) []error {
}

func (c *poeCommand) GetExportLanguages() ([]poeditor.Language, error) {
fmt.Println("Fetching project languages...")
langs, err := c.client.GetProjectLanguages(c.options.ProjectID)
if err != nil {
return nil, err
Expand Down Expand Up @@ -186,8 +199,9 @@ func (c *poeCommand) GetExportLanguages() ([]poeditor.Language, error) {
func (c *poeCommand) EnsureOutputDirectory() error {
dir := c.options.OutputDir
if _, err := os.Stat(dir); os.IsNotExist(err) {
fmt.Printf("Creating directory %s...\n", dir)
logSub := c.log.Info("creating directory %s", dir).Sub()
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
logSub.Error("failed: " + err.Error())
return err
}
}
Expand All @@ -196,31 +210,36 @@ func (c *poeCommand) EnsureOutputDirectory() error {
}

func (c *poeCommand) ExportLanguage(lang poeditor.Language, template, requireResourceAttributes bool) error {
fmt.Printf("Fetching JSON export for %s (%s)...\n", lang.Name, lang.Code)
logSub := c.log.Info("fetching JSON export for %s (%s)", lang.Name, lang.Code).Sub()
url, err := c.client.GetExportURL(c.options.ProjectID, lang.Code)
if err != nil {
return err
}

resp, err := http.Get(url)
if err != nil {
logSub.Error("making HTTP request failed: " + err.Error())
return errors.Wrap(err, "making HTTP request for export")
}

filePath := path.Join(c.options.OutputDir, fmt.Sprintf("%s%s.arb", c.options.ARBPrefix, lang.Code))
file, err := os.Create(filePath)
if err != nil {
logSub.Error("creating file failed: " + err.Error())
return errors.Wrap(err, "creating ARB file")
}
defer file.Close()

convertLogSub := logSub.Info("converting JSON to ARB").Sub()

conv := converter.NewConverter(resp.Body, lang.Code, template, requireResourceAttributes)
err = conv.Convert(file)
if err != nil {
convertLogSub.Error(err.Error())
return err
}

fmt.Printf("Success converting JSON to ARB for %s (%s).\n", lang.Name, lang.Code)
logSub.Success("saved to %s", filePath)

return nil
}
21 changes: 18 additions & 3 deletions cmd/poe2arb.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
// Package cmd provides command-line interface commands.
package cmd

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

"github.com/leancodepl/poe2arb/log"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "poe2arb",
Short: "POEditor JSON to Flutter ARB converter",
}

func Execute() {
type ctxKey int

const loggerKey = ctxKey(1)

func Execute(logger *log.Logger) {
rootCmd.AddCommand(convertCmd)
rootCmd.AddCommand(poeCmd)
rootCmd.AddCommand(versionCmd)

rootCmd.Execute()
ctx := context.WithValue(context.Background(), loggerKey, logger)

rootCmd.ExecuteContext(ctx)
}

func getLogger(cmd *cobra.Command) *log.Logger {
return cmd.Context().Value(loggerKey).(*log.Logger)
}
4 changes: 3 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ var versionCmd = &cobra.Command{
}

func runVersion(cmd *cobra.Command, args []string) error {
cmd.Printf("poe2arb version %s, commit %s, built at %s\n", Version, Commit, BuiltDate)
log := getLogger(cmd)

log.Info("poe2arb version %s, commit %s, built at %s", Version, Commit, BuiltDate)

return nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/leancodepl/poe2arb
go 1.19

require (
github.com/TwiN/go-color v1.4.0
github.com/caarlos0/env/v6 v6.10.1
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.6.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/TwiN/go-color v1.4.0 h1:fNbOwOrvup5oj934UragnW0B1WKaAkkB85q19Y7h4ng=
github.com/TwiN/go-color v1.4.0/go.mod h1:0QTVEPlu+AoCyTrho7bXbVkrCkVpdQr7YF7PYWEtSxM=
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
Expand Down Expand Up @@ -40,8 +42,6 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/wk8/go-ordered-map/v2 v2.1.0 h1:irLJKNi0WMmX6f4MQC7urUX5pE8Ab5dOtM2orbaeEjU=
github.com/wk8/go-ordered-map/v2 v2.1.0/go.mod h1:ny62VR4ZctzfAJyvdkBYE1DmSHfsuLsj6AGsxni6wWQ=
github.com/wk8/go-ordered-map/v2 v2.1.1 h1:y13jts8SahUV/CChdundKh4kAR/1+Y4qYE+rI3Br7cY=
github.com/wk8/go-ordered-map/v2 v2.1.1/go.mod h1:ny62VR4ZctzfAJyvdkBYE1DmSHfsuLsj6AGsxni6wWQ=
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
Expand Down
54 changes: 54 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Package log provides beautiful logger for the console interface.
package log

import (
"fmt"
"io"
"strings"

clr "github.com/TwiN/go-color"
)

type Logger struct {
writer io.Writer
depth int
}

func New(writer io.Writer) *Logger {
return &Logger{
writer: writer,
depth: 0,
}
}

func (l *Logger) Info(msg string, params ...any) *Logger {
l.log(clr.Blue, msg, params...)

return l
}

func (l *Logger) Success(msg string, params ...any) *Logger {
l.log(clr.Green, msg, params...)

return l
}

func (l *Logger) Error(msg string, params ...any) *Logger {
l.log(clr.Red, msg, params...)

return l
}

func (l *Logger) log(color, msg string, params ...any) {
str := strings.Repeat(" ", l.depth)
str += color + " • " + clr.Reset + fmt.Sprintf(msg, params...) + "\n"

fmt.Fprint(l.writer, str)
}

func (l *Logger) Sub() *Logger {
return &Logger{
writer: l.writer,
depth: l.depth + 1,
}
}
60 changes: 60 additions & 0 deletions log/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package log_test

import (
"bytes"
"testing"

"github.com/leancodepl/poe2arb/log"
"github.com/stretchr/testify/assert"
)

const (
blue = "\x1b[34m"
green = "\x1b[32m"
red = "\x1b[31m"
reset = "\x1b[0m"
)

func TestLoggerInfo(t *testing.T) {
var buf bytes.Buffer

l := log.New(&buf)

l.Info("Hello, %s!", "world")

assert.Equal(t, blue+" • "+reset+"Hello, world!\n", buf.String())
}

func TestLoggerSuccess(t *testing.T) {
var buf bytes.Buffer

l := log.New(&buf)

l.Success("Hello, %s!", "world")

assert.Equal(t, green+" • "+reset+"Hello, world!\n", buf.String())
}

func TestLoggerError(t *testing.T) {
var buf bytes.Buffer

l := log.New(&buf)

l.Error("Hello, %s!", "world")

assert.Equal(t, red+" • "+reset+"Hello, world!\n", buf.String())
}

func TestLoggerSub(t *testing.T) {
var buf bytes.Buffer

l := log.New(&buf)

sub := l.Sub()
sub.Info("test")

subSub := sub.Sub()
subSub.Info("test2")

assert.Equal(t, " "+blue+" • "+reset+"test\n "+blue+" • "+reset+"test2\n", buf.String())
}
11 changes: 9 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package main

import "github.com/leancodepl/poe2arb/cmd"
import (
"os"

"github.com/leancodepl/poe2arb/cmd"
"github.com/leancodepl/poe2arb/log"
)

func main() {
cmd.Execute()
logger := log.New(os.Stdout)

cmd.Execute(logger)
}

0 comments on commit 8d945bf

Please sign in to comment.