Skip to content

Commit

Permalink
♻️ Use github.com/ewen-lbh/label-logger-go for logging & progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
ewen-lbh committed Oct 12, 2024
1 parent c29f2d4 commit 079bb3c
Show file tree
Hide file tree
Showing 22 changed files with 175 additions and 406 deletions.
21 changes: 11 additions & 10 deletions add.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/anaskhan96/soup"
"github.com/charmbracelet/huh"
ll "github.com/ewen-lbh/label-logger-go"
"golang.org/x/text/cases"
"golang.org/x/text/language"

Expand Down Expand Up @@ -176,7 +177,7 @@ func (ctx *RunContext) CreateDescriptionFile(workId string, metadataItems []stri
if fileExists(readmePath) {
readmeTitle, readmeBody, err := fromReadme(readmePath)
if err != nil {
DisplayWarning("couldn't extract info from README.md", err)
ll.WarnDisplay("couldn't extract info from README.md", err)
} else {
if readmeTitle != "" {
defaultProjectTitle = readmeTitle
Expand All @@ -188,10 +189,10 @@ func (ctx *RunContext) CreateDescriptionFile(workId string, metadataItems []stri
detectedStartDate, err := DetectStartDate(ctx.PathToWorkFolder(workId))
defaultStartedAt := ""
if err != nil {
DisplayWarning("while detecting start date of %s", err, workId)
ll.WarnDisplay("while detecting start date of %s", err, workId)
} else {
defaultStartedAt = detectedStartDate.Format("2006-01-02")
LogCustom("Detected", "cyan", "start date to be [bold][blue]%s[reset]", defaultStartedAt)
ll.Log("Detected", "cyan", "start date to be [bold][blue]%s[reset]", defaultStartedAt)
}

startedAtPlaceholder := "YYYY-MM-DD"
Expand All @@ -207,27 +208,27 @@ func (ctx *RunContext) CreateDescriptionFile(workId string, metadataItems []stri

autodetectedTechs, err := ctx.DetectTechnologies(workId)
if err != nil {
LogWarning(formatErrors(fmt.Errorf("while autodetecting technologies for %s: %w", workId, err)))
ll.Warn(ll.FormatErrors(fmt.Errorf("while autodetecting technologies for %s: %w", workId, err)))
} else {
displayTags := make([]string, 0, len(autodetectedTechs))
for _, tech := range autodetectedTechs {
metadata.MadeWith = append(metadata.MadeWith, tech.Slug)
displayTags = append(displayTags, tech.String())
}
if len(metadata.MadeWith) > 0 {
LogCustom("Detected", "cyan", "technologies to be %s", formatList(displayTags, "[bold][blue]%s[reset]", ", "))
ll.Log("Detected", "cyan", "technologies to be %s", ll.List(displayTags, "[bold][blue]%s[reset]", ", "))
}
}

autodetectedTags, err := ctx.DetectTags(workId, autodetectedTechs)
if err != nil {
DisplayWarning("while autodetecting tags for %s", err, workId)
ll.WarnDisplay("while autodetecting tags for %s", err, workId)
} else {
for _, tag := range autodetectedTags {
metadata.Tags = append(metadata.Tags, tag.String())
}
if len(metadata.Tags) > 0 {
LogCustom("Detected", "cyan", "tags to be %s", formatList(metadata.Tags, "[bold][blue]%s[reset]", ", "))
ll.Log("Detected", "cyan", "tags to be %s", ll.List(metadata.Tags, "[bold][blue]%s[reset]", ", "))
}
}

Expand All @@ -248,7 +249,7 @@ func (ctx *RunContext) CreateDescriptionFile(workId string, metadataItems []stri
Value(&metadata.MadeWith).
Options(allTechsOptions...).
Validate(func(s []string) error {
LogDebug("Selected %v", s)
ll.Debug("Selected %v", s)
return nil
}).
Height(2+6),
Expand Down Expand Up @@ -276,7 +277,7 @@ func (ctx *RunContext) CreateDescriptionFile(workId string, metadataItems []stri
defaultFinishedAt := time.Now().Format("2006-01-02")
if finishedAtFromGit, err := LastGitCommitDate(ctx.PathToWorkFolder(workId)); err == nil {
defaultFinishedAt = finishedAtFromGit.Format("2006-01-02")
LogCustom("Detected", "cyan", "finish date to be [bold][blue]%s[reset]", defaultFinishedAt)
ll.Log("Detected", "cyan", "finish date to be [bold][blue]%s[reset]", defaultFinishedAt)
}

err = huh.NewForm(
Expand Down Expand Up @@ -330,6 +331,6 @@ func (ctx *RunContext) CreateDescriptionFile(workId string, metadataItems []stri

os.MkdirAll(filepath.Dir(outputPath), 0o755)
os.WriteFile(outputPath, []byte(output), 0o644)
LogCustom("Created", "green", "description.md file at [bold]%s[reset]", outputPath)
ll.Log("Created", "green", "description.md file at [bold]%s[reset]", outputPath)
return outputPath, nil
}
71 changes: 36 additions & 35 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"path"

ll "github.com/ewen-lbh/label-logger-go"
jsoniter "github.com/json-iterator/go"
)

Expand Down Expand Up @@ -227,7 +228,7 @@ func AcquireBuildLock(outputFilename string) error {
func ReleaseBuildLock(outputFilename string) error {
err := os.Remove(BuildLockFilepath(outputFilename))
if err != nil {
DisplayError("could not release build lockfile %s", err, BuildLockFilepath(outputFilename))
ll.ErrorDisplay("could not release build lockfile %s", err, BuildLockFilepath(outputFilename))
}
return err
}
Expand All @@ -248,19 +249,19 @@ func PrepareBuild(databaseDirectory string, outputFilename string, flags Flags,
thumbnailSizesCount := len(ctx.Config.MakeThumbnails.Sizes)

if thumbnailSizesCount/2 > flags.WorkersCount {
LogDebug("ThumbnailSizesCount/2 (%d) > flags.WorkersCount (%d). Using 2 thumbnailers per work.", thumbnailSizesCount/2, flags.WorkersCount)
ll.Debug("ThumbnailSizesCount/2 (%d) > flags.WorkersCount (%d). Using 2 thumbnailers per work.", thumbnailSizesCount/2, flags.WorkersCount)
ctx.thumbnailersPerWork = 2
} else {
LogDebug("Configuration asks for %d thumbnail sizes. setting thumbnail workers count per work to half of that.", thumbnailSizesCount)
ll.Debug("Configuration asks for %d thumbnail sizes. setting thumbnail workers count per work to half of that.", thumbnailSizesCount)
ctx.thumbnailersPerWork = thumbnailSizesCount / 2
}

LogDebug("Using %d thumbnailers threads per work", ctx.thumbnailersPerWork)
ll.Debug("Using %d thumbnailers threads per work", ctx.thumbnailersPerWork)

if ctx.ProgressInfoFile != "" {
LogDebug("Removing progress info file %s", ctx.ProgressInfoFile)
ll.Debug("Removing progress info file %s", ctx.ProgressInfoFile)
if err := os.Remove(ctx.ProgressInfoFile); err != nil {
LogDebug("Could not remove progress info file %s: %s", ctx.ProgressInfoFile, err.Error())
ll.Debug("Could not remove progress info file %s: %s", ctx.ProgressInfoFile, err.Error())
}
}

Expand All @@ -278,24 +279,24 @@ func PrepareBuild(databaseDirectory string, outputFilename string, flags Flags,
ctx.Exporters = append(ctx.Exporters, exporter)
}

LogDebug("Running with configuration %#v", &config)
ll.Debug("Running with configuration %#v", &config)

previousBuiltDatabaseRaw, err := os.ReadFile(outputFilename)
if err != nil {
if !os.IsNotExist(err) {
DisplayError("No previously built database file %s to use", err, outputFilename)
ll.ErrorDisplay("No previously built database file %s to use", err, outputFilename)
}
} else {
previousDb := Database{}
err = json.Unmarshal(previousBuiltDatabaseRaw, &previousDb)
if err != nil {
DisplayError("Couldn't use previous built database file %s", err, outputFilename)
ll.ErrorDisplay("Couldn't use previous built database file %s", err, outputFilename)
}
ctx.previousBuiltDatabase = PreviouslyBuiltDatabase{Database: previousDb}
}

if ctx.Config.IsDefault {
LogInfo("No configuration file found. The default configuration was used.")
ll.Info("No configuration file found. The default configuration was used.")
}

err = os.MkdirAll(config.Media.At, 0o755)
Expand All @@ -312,7 +313,7 @@ func PrepareBuild(databaseDirectory string, outputFilename string, flags Flags,
return &ctx, err
}

LogCustom("Using", "magenta", "exporter [bold]%s[reset]\n[dim]%s", exporter.Name(), exporter.Description())
ll.Log("Using", "magenta", "exporter [bold]%s[reset]\n[dim]%s", exporter.Name(), exporter.Description())
err = exporter.Before(&ctx, options)
if err != nil {
return &ctx, fmt.Errorf("while running exporter %s before hook: %w", exporter.Name(), err)
Expand Down Expand Up @@ -348,7 +349,7 @@ func directoriesLeftToBuild(all []string, built []string) []string {
func (ctx *RunContext) RunExporters(work *Work) error {
for _, exporter := range ctx.Exporters {
if debugging {
LogCustom("Exporting", "magenta", "%s to %s", work.ID, exporter.Name())
ll.Log("Exporting", "magenta", "%s to %s", work.ID, exporter.Name())
}
options := ctx.Config.Exporters[exporter.Name()]
err := exporter.Export(ctx, options, work)
Expand All @@ -371,7 +372,7 @@ func (ctx *RunContext) BuildSome(include string, databaseDirectory string, outpu

// Initialize stuff
works := ctx.PreviouslyBuiltDatabase()
// LogDebug("initialized works@%p from previous@%p", works, ctx.previousBuiltDatabase.Database)
// ll.Debug("initialized works@%p from previous@%p", works, ctx.previousBuiltDatabase.Database)
workDirectories, err := ctx.ComputeProgressTotal()
if err != nil {
return Database{}, fmt.Errorf("while computing total number of works to build: %w", err)
Expand All @@ -391,20 +392,20 @@ func (ctx *RunContext) BuildSome(include string, databaseDirectory string, outpu
ctx.StartProgressBar(len(workDirectories))

if flags.WorkersCount < ctx.thumbnailersPerWork {
LogWarning("Number of workers (%d) is less than the number of thumbnailers per work (%d). Setting number of workers to %d", ctx.Flags.WorkersCount, ctx.thumbnailersPerWork, ctx.thumbnailersPerWork)
ll.Warn("Number of workers (%d) is less than the number of thumbnailers per work (%d). Setting number of workers to %d", ctx.Flags.WorkersCount, ctx.thumbnailersPerWork, ctx.thumbnailersPerWork)
flags.WorkersCount = ctx.thumbnailersPerWork
}

// Build works in parallel
for i := 0; i < flags.WorkersCount/ctx.thumbnailersPerWork; i++ {
i := i
LogDebug("worker #%d: starting", i)
ll.Debug("worker #%d: starting", i)
go func() {
LogDebug("worker #%d: starting", i)
ll.Debug("worker #%d: starting", i)
for {
dirEntry := <-workDirectoriesChannel
workID := dirEntry.Name()
LogDebug("worker #%d: starting with work %s", i, workID)
ll.Debug("worker #%d: starting with work %s", i, workID)
_, presentBefore := ctx.PreviouslyBuiltWork(workID)
var included bool
if include == "*" {
Expand All @@ -430,7 +431,7 @@ func (ctx *RunContext) BuildSome(include string, databaseDirectory string, outpu
ctx.Status(workID, PhaseBuilding)
newWork, usedCache, err := ctx.Build(string(descriptionRaw), outputFilename, workID)
if err != nil {
DisplayError("while building %s", err, workID)
ll.ErrorDisplay("while building %s", err, workID)
builtChannel <- builtItem{err: fmt.Errorf("while building %s (%s): %w", workID, ctx.DescriptionFilename(databaseDirectory, workID), err)}
continue
}
Expand All @@ -443,55 +444,55 @@ func (ctx *RunContext) BuildSome(include string, databaseDirectory string, outpu
}

// Update in database
LogDebug("worker #%d: sending freshly built work %s", i, workID)
ll.Debug("worker #%d: sending freshly built work %s", i, workID)
builtChannel <- builtItem{work: newWork, workID: workID}
continue
// }
} else if presentBefore {
// Nothing to do, old work will be kept as-is.
LogDebug("worker #%d: nothing to do for work %s", i, workID)
ll.Debug("worker #%d: nothing to do for work %s", i, workID)
ctx.Status(workID, PhaseUnchanged)
} else {
LogDebug("worker #%d: Build skipped: not included by %s, not present in previous database file.", i, include)
ll.Debug("worker #%d: Build skipped: not included by %s, not present in previous database file.", i, include)
}
LogDebug("worker #%d: reusing old work %s", i, workID)
ll.Debug("worker #%d: reusing old work %s", i, workID)
builtChannel <- builtItem{reuseOld: true, workID: workID}
}
}()
}

LogDebug("main: filling work directories")
ll.Debug("main: filling work directories")
for _, workDirectory := range workDirectories {
workDirectoriesChannel <- workDirectory
}

// Collect all newly-built works
LogDebug("main: collecting results")
ll.Debug("main: collecting results")
for len(builtDirectories) < len(workDirectories) {
result := <-builtChannel
LogDebug("main: got result %v", result)
ll.Debug("main: got result %v", result)
if result.err != nil {
LogDebug("main: got error, returning early")
ll.Debug("main: got error, returning early")
return Database{}, result.err
}
if !result.reuseOld {
LogDebug("main: updating work %s", result.workID)
ll.Debug("main: updating work %s", result.workID)
ctx.previousBuiltDatabase.mu.Lock()
works[result.workID] = result.work
ctx.previousBuiltDatabase.mu.Unlock()
}
ctx.WriteDatabase(works, flags, outputFilename, true)
builtDirectories = append(builtDirectories, result.workID)
LogDebug("main: built dirs: %d out of %d", len(builtDirectories), len(workDirectories))
LogDebug("main: left to build: %v", directoriesLeftToBuild(workDirectoriesNames, builtDirectories))
ll.Debug("main: built dirs: %d out of %d", len(builtDirectories), len(workDirectories))
ll.Debug("main: left to build: %v", directoriesLeftToBuild(workDirectoriesNames, builtDirectories))
}

for _, exporter := range ctx.Exporters {
options := ctx.Config.Exporters[exporter.Name()]
LogDebug("Running exporter %s's after hook with options %#v", exporter.Name(), options)
ll.Debug("Running exporter %s's after hook with options %#v", exporter.Name(), options)
err := exporter.After(ctx, options, &works)
if err != nil {
DisplayError("while running exporter %s's after hook: %s", err, exporter.Name())
ll.ErrorDisplay("while running exporter %s's after hook: %s", err, exporter.Name())
}

}
Expand All @@ -500,7 +501,7 @@ func (ctx *RunContext) BuildSome(include string, databaseDirectory string, outpu
}

func (ctx *RunContext) WriteDatabase(works Database, flags Flags, outputFilename string, partial bool) {
LogDebug("Writing database (partial=%v) to %s", partial, outputFilename)
ll.Debug("Writing database (partial=%v) to %s", partial, outputFilename)
worksWithDatabaseMetadata := make(Database, 0)
for id, work := range works {
work.Metadata.DatabaseMetadata = DatabaseMeta{Partial: partial}
Expand Down Expand Up @@ -559,7 +560,7 @@ func (ctx *RunContext) ComputeProgressTotal() (workDirectories []fs.DirEntry, er
}
// If it's not there, this directory is not a project worth scanning.
if _, err := os.Stat(descriptionFilename); os.IsNotExist(err) {
LogDebug("skipping %s as it has no description file: %s does not exist", dirEntry.Name(), descriptionFilename)
ll.Debug("skipping %s as it has no description file: %s does not exist", dirEntry.Name(), descriptionFilename)
continue
}

Expand Down Expand Up @@ -594,7 +595,7 @@ func (ctx *RunContext) Build(descriptionRaw string, outputFilename string, workI
newDescriptionHash := base64.StdEncoding.EncodeToString(hash[:])

if oldWork, found := ctx.PreviouslyBuiltWork(workID); found && oldWork.DescriptionHash == newDescriptionHash && !ctx.Flags.NoCache {
LogDebug("parsing description for %s: using cached work", workID)
ll.Debug("parsing description for %s: using cached work", workID)
work = oldWork
usedCache = true
} else {
Expand All @@ -613,7 +614,7 @@ func (ctx *RunContext) Build(descriptionRaw string, outputFilename string, workI
if block.Type != "media" {
continue
}
LogDebug("Handling media %#v", block.Media)
ll.Debug("Handling media %#v", block.Media)
analyzed, anchor, usedCacheForMedia, err := ctx.HandleMedia(workID, block.ID, block.Media, lang)
if err != nil {
return Work{}, false, err
Expand Down
3 changes: 2 additions & 1 deletion cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/MakeNowJust/heredoc"
ortfodb "github.com/ortfo/db"
ll "github.com/ewen-lbh/label-logger-go"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -69,7 +70,7 @@ var addCmd = &cobra.Command{

editor := os.Getenv("EDITOR")
if editor != "" {
ortfodb.LogCustom("Opening", "cyan", "%s in %s", descriptionFilepath, editor)
ll.Log("Opening", "cyan", "%s in %s", descriptionFilepath, editor)
editorPath, err := exec.LookPath(editor)
if err != nil {
handleError(fmt.Errorf("while getting path to %s: %w", editor, err))
Expand Down
3 changes: 2 additions & 1 deletion cmd/exporters.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
ortfodb "github.com/ortfo/db"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
ll "github.com/ewen-lbh/label-logger-go"
)

var exportersCmd = &cobra.Command{
Expand Down Expand Up @@ -53,7 +54,7 @@ var exportersInitCmd = &cobra.Command{
" "),
),
0644)
ortfodb.LogCustom("Created", "green", fmt.Sprintf("example exporter at [bold]%s.yaml[reset]", args[0]))
ll.Log("Created", "green", fmt.Sprintf("example exporter at [bold]%s.yaml[reset]", args[0]))
},
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/MakeNowJust/heredoc"
"github.com/mitchellh/colorstring"
ortfodb "github.com/ortfo/db"
ll "github.com/ewen-lbh/label-logger-go"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/term"
Expand Down Expand Up @@ -176,7 +176,7 @@ func customFlagsUsage(f *pflag.FlagSet) string {
}

s := buf.String()
if !ortfodb.ShowingColors() {
if !ll.ShowingColors() {
s = regexp.MustCompile(`\x1b\[[0-9;]*[a-zA-Z]`).ReplaceAllString(s, "")
}
return s
Expand Down
Loading

0 comments on commit 079bb3c

Please sign in to comment.