Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace delete-cache --sizes by show-cache #85

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 2 additions & 26 deletions cli/delete_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var DeleteCache = cmd.Sub{
type DeleteCacheFlags struct {
All bool `short:"a" long:"all" desc:"Additionally delete (s)ccache, packages and sources"`
Images bool `short:"i" long:"images" desc:"Additionally delete solbuild images"`
Sizes bool `short:"s" long:"sizes" desc:"Show disk usage of the caches"`
Sizes bool `short:"s" long:"sizes" desc:"Deprecated: use 'show-cache' instead"`
}

// DeleteCacheRun carries out the "delete-cache" sub-command.
Expand All @@ -75,31 +75,7 @@ func DeleteCacheRun(r *cmd.Root, s *cmd.Sub) {

// If sizes is requested just print disk usage of caches and return
if sFlags.Sizes {
sizeDirs := []string{
manager.Config.OverlayRootDir,
builder.CacheDirectory,
builder.ObsoleteCcacheDirectory,
builder.ObsoleteSccacheDirectory,
builder.ObsoleteLegacyCcacheDirectory,
builder.ObsoleteLegacySccacheDirectory,
builder.PackageCacheDirectory,
source.SourceDir,
}

var totalSize int64

for _, p := range sizeDirs {
size, _ := getDirSize(p)
totalSize += size

if err != nil {
slog.Warn("Couldn't get directory size", "reason", err)
}

slog.Info(fmt.Sprintf("Size of '%s' is '%s'", p, humanReadableFormat(float64(size))))
}

slog.Info(fmt.Sprintf("Total size: '%s'", humanReadableFormat(float64(totalSize))))
showCacheSizes(manager)

return
}
Expand Down
71 changes: 71 additions & 0 deletions cli/show_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cli

import (
"fmt"
"log/slog"

"github.com/DataDrake/cli-ng/v2/cmd"

"github.com/getsolus/solbuild/builder"
"github.com/getsolus/solbuild/builder/source"
"github.com/getsolus/solbuild/cli/log"
)

func init() {
cmd.Register(&ShowCache)
}

// ShowCache shows the disk usage of the solbuild cache.
var ShowCache = cmd.Sub{
Name: "show-cache",
Alias: "sc",
Short: "Show the size of assets stored on disk by solbuild",
Run: ShowCacheRun,
}

func ShowCacheRun(r *cmd.Root, s *cmd.Sub) {
rFlags := r.Flags.(*GlobalFlags) //nolint:forcetypeassert // guaranteed by callee.

if rFlags.Debug {
log.Level.Set(slog.LevelDebug)
}

if rFlags.NoColor {
log.SetUncoloredLogger()
}

manager, err := builder.NewManager()
if err != nil {
log.Panic("Failed to create new Manager: %e\n", err)
}

showCacheSizes(manager)
}

func showCacheSizes(manager *builder.Manager) {
sizeDirs := []string{
manager.Config.OverlayRootDir,
builder.CacheDirectory,
builder.ObsoleteCcacheDirectory,
builder.ObsoleteSccacheDirectory,
builder.ObsoleteLegacyCcacheDirectory,
builder.ObsoleteLegacySccacheDirectory,
builder.PackageCacheDirectory,
source.SourceDir,
}

var totalSize int64

for _, p := range sizeDirs {
size, err := getDirSize(p)
totalSize += size

if err != nil {
slog.Warn("Couldn't get directory size", "reason", err)
}

slog.Info(fmt.Sprintf("Size of '%s' is '%s'", p, humanReadableFormat(float64(size))))
}

slog.Info(fmt.Sprintf("Total size: '%s'", humanReadableFormat(float64(totalSize))))
}