Skip to content

Commit b4233cd

Browse files
committed
config.go: Don't add unecessary fields to default config
Signed-off-by: xplshn <xplshn@murena.io>
1 parent 966b4d9 commit b4233cd

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ type config struct {
4343
RetakeOwnership bool `yaml:"RetakeOwnership" env:"DBIN_REOWN" description:"Retake ownership of installed binaries."`
4444
UseIntegrationHooks bool `yaml:"IntegrationHooks" env:"DBIN_USEHOOKS" description:"Use integration hooks for binaries."`
4545
DisableProgressbar bool `yaml:"DisablePbar,omitempty" env:"DBIN_NOPBAR" description:"Disable the progress bar."`
46-
NoConfig bool `env:"DBIN_NOCONFIG" description:"Disable configuration file usage."`
47-
ProgressbarFIFO bool `env:"DBIN_PB_FIFO" description:"Use FIFO for progress bar."`
46+
NoConfig bool `yaml:"-" env:"DBIN_NOCONFIG" description:"Disable configuration file usage."`
47+
ProgressbarFIFO bool `yaml:"-" env:"DBIN_PB_FIFO" description:"Use FIFO for progress bar."`
4848
Hooks hooks `yaml:"Hooks,omitempty"`
4949
}
5050

@@ -356,6 +356,7 @@ func createDefaultConfigAt(configFilePath string) error {
356356
IntegrationCommand: "sh -c \"$DBIN info > ${DBIN_CACHE_DIR}/.info\"",
357357
DeintegrationCommand: "sh -c \"$DBIN info > ${DBIN_CACHE_DIR}/.info\"",
358358
UseRunFromCache: true,
359+
Silent: true,
359360
NoOp: false,
360361
},
361362
},

fetch.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"regexp"
1313
"strings"
1414
"syscall"
15+
"time"
1516

1617
"github.com/goccy/go-json"
1718
"github.com/hedzr/progressbar"
@@ -515,3 +516,50 @@ func downloadOCILayer(ctx context.Context, registry, repository string, manifest
515516
}
516517
return binaryResp, sigResp, nil
517518
}
519+
520+
// cleanTempFiles removes .tmp files in the InstallDir that haven't been accessed in over a day.
521+
func cleanInstallCache(installDir string) error {
522+
const oneDay = 24 * time.Hour
523+
now := time.Now()
524+
525+
entries, err := os.ReadDir(installDir)
526+
if err != nil {
527+
return errFileAccess.Wrap(err)
528+
}
529+
530+
for _, entry := range entries {
531+
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".tmp") {
532+
continue
533+
}
534+
535+
filePath := filepath.Join(installDir, entry.Name())
536+
fileInfo, err := os.Stat(filePath)
537+
if err != nil {
538+
if verbosityLevel >= silentVerbosityWithErrors {
539+
fmt.Fprintf(os.Stderr, "Error accessing file info for %s: %v\n", filePath, err)
540+
}
541+
continue
542+
}
543+
var atime time.Time
544+
if sysInfo, ok := fileInfo.Sys().(*syscall.Stat_t); ok {
545+
atime = time.Unix(sysInfo.Atim.Sec, sysInfo.Atim.Nsec)
546+
} else {
547+
if verbosityLevel >= extraVerbose {
548+
fmt.Fprintf(os.Stderr, "Warning: ATime not supported for %s, skipping cleanup\n", filePath)
549+
}
550+
continue
551+
}
552+
553+
if now.Sub(atime) > oneDay {
554+
if err := os.Remove(filePath); err != nil {
555+
if verbosityLevel >= silentVerbosityWithErrors {
556+
fmt.Fprintf(os.Stderr, "Error removing old .tmp file %s: %v\n", filePath, err)
557+
}
558+
} else if verbosityLevel >= extraVerbose {
559+
fmt.Printf("Removed old .tmp file: %s\n", filePath)
560+
}
561+
}
562+
}
563+
564+
return nil
565+
}

install.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ func installBinaries(ctx context.Context, config *config, bEntries []binaryEntry
4040
cursor.Hide()
4141
defer cursor.Show()
4242

43+
// Clean up old .tmp files before installation
44+
if err := cleanInstallCache(config.InstallDir); err != nil {
45+
if verbosityLevel >= silentVerbosityWithErrors {
46+
fmt.Fprintf(os.Stderr, "Warning: Failed to clean up .tmp files in %s: %v\n", config.InstallDir, err)
47+
}
48+
}
49+
4350
var wg sync.WaitGroup
4451
var errors []string
4552
var errorsMu sync.Mutex

run.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func runFromCache(config *config, bEntry binaryEntry, args []string, transparent
6464
if err := runBinary(cachedFile, args, env); err != nil {
6565
return errRunFailed.Wrap(err)
6666
}
67-
return cleanCache(config.CacheDir)
67+
return cleanRunCache(config.CacheDir)
6868
}
6969

7070
if verbosityLevel >= normalVerbosity {
@@ -94,7 +94,7 @@ func runFromCache(config *config, bEntry binaryEntry, args []string, transparent
9494
if err := runBinary(cachedFile, args, env); err != nil {
9595
return errRunFailed.Wrap(err)
9696
}
97-
return cleanCache(config.CacheDir)
97+
return cleanRunCache(config.CacheDir)
9898
}
9999

100100
func isCached(config *config, bEntry binaryEntry) (string, error) {
@@ -129,7 +129,7 @@ func runBinary(binaryPath string, args []string, env []string) error {
129129
return errRunFailed.Wrap(err)
130130
}
131131

132-
func cleanCache(cacheDir string) error {
132+
func cleanRunCache(cacheDir string) error {
133133
files, err := os.ReadDir(cacheDir)
134134
if err != nil {
135135
return errRunFailed.Wrap(err)

0 commit comments

Comments
 (0)