From 3a1f862418e135c8e10077f1d61afbe00902a366 Mon Sep 17 00:00:00 2001 From: Shane <6071159+smashedr@users.noreply.github.com> Date: Sun, 1 Feb 2026 18:24:02 -0800 Subject: [PATCH] Add Archive Size --- README.md | 3 +++ cmd/backup.go | 27 ++++++++++++++++++++++----- cmd/exclude.go | 10 ---------- cmd/info.go | 10 ---------- cmd/list.go | 10 ---------- cmd/root.go | 23 ++++++++++++++--------- 6 files changed, 39 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index e291ef4..8e62ea2 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,9 @@ [![Docker](https://img.shields.io/badge/docker_run-ghcr.io%2Fsmashedr%2Fbup-blue?style=flat-square&logo=docker)](#docker) [![GitHub](https://img.shields.io/badge/curl_--L-i.jpillora.com%2Fsmashedr%2Fbup!|bash-blue?style=flat-square&logo=github)](#github) + +BackUP + - [Install](#install) - [Usage](#usage) - [Development](#development) diff --git a/cmd/backup.go b/cmd/backup.go index dacc319..d8f6bbc 100644 --- a/cmd/backup.go +++ b/cmd/backup.go @@ -12,10 +12,6 @@ import ( "time" ) -//var ( -// port int -//) - //func validateDirectory(path, name string) error { // info, err := os.Stat(path) // if err != nil { @@ -31,6 +27,19 @@ import ( // return nil //} +func formatBytes(bytes int64) string { + const unit = 1024 + if bytes < unit { + return fmt.Sprintf("%d B", bytes) + } + div, exp := int64(unit), 0 + for n := bytes / unit; n >= unit; n /= unit { + div *= unit + exp++ + } + return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp]) +} + func createZipArchive(excludes []string, source, destination string) error { // Create timestamp filename timestamp := time.Now().Format("06-01-02-15-04-05") // YY-MM-DD-HH-MM-SS @@ -45,7 +54,7 @@ func createZipArchive(excludes []string, source, destination string) error { // Create zip writer zipWriter := zip.NewWriter(zipFile) - defer func() { _ = zipWriter.Close() }() + //defer func() { _ = zipWriter.Close() }() // Get the base name of the source for proper relative paths sourceBase := filepath.Base(source) @@ -134,6 +143,14 @@ func createZipArchive(excludes []string, source, destination string) error { return fmt.Errorf("failed to walk directory: %w", err) } + _ = zipWriter.Close() + fileInfo, err := zipFile.Stat() + if err != nil { + fmt.Printf("Error getting archive info: %v", err) + } else { + fmt.Printf("Archive Size: %s\n", formatBytes(fileInfo.Size())) + } + fmt.Printf("Archive: %s\n", zipFilename) return nil } diff --git a/cmd/exclude.go b/cmd/exclude.go index 7265090..977b16a 100644 --- a/cmd/exclude.go +++ b/cmd/exclude.go @@ -24,14 +24,4 @@ var excludeCmd = &cobra.Command{ func init() { rootCmd.AddCommand(excludeCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // infoCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // infoCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/cmd/info.go b/cmd/info.go index f16db77..7470622 100644 --- a/cmd/info.go +++ b/cmd/info.go @@ -23,14 +23,4 @@ var infoCmd = &cobra.Command{ func init() { rootCmd.AddCommand(infoCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // infoCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // infoCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/cmd/list.go b/cmd/list.go index 85f8620..938357b 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -46,14 +46,4 @@ func listDir(path string) { func init() { rootCmd.AddCommand(listCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // listCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/cmd/root.go b/cmd/root.go index d380c44..c11bbac 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -57,12 +57,14 @@ func initConfig() { // Set default excluded directories viper.SetDefault("excludes", []string{ - ".cache", + ".*cache", ".venv", "build", "dist", "node_modules", + "out", "venv", + "*.exe", }) if cfgFile != "" { @@ -73,6 +75,7 @@ func initConfig() { fmt.Printf("Unable to read config file: %s\n\n", viper.ConfigFileUsed()) os.Exit(1) } + fmt.Printf("Config File: %s\n", cfgFile) } else { // Find Config viper.SetConfigType("yaml") @@ -89,22 +92,24 @@ func initConfig() { //viper.ReadInConfig() if err := viper.ReadInConfig(); err != nil { - home, _ := os.UserHomeDir() - //fmt.Printf("home: %s\n", home) - configPath := filepath.Join(home, ".config") + homeDir, err := os.UserHomeDir() + if err != nil { + homeDir = "." + } + fmt.Printf("homeDir: %s\n", homeDir) + configPath := filepath.Join(homeDir, ".config") //fmt.Printf("configPath: %s\n", configPath) _ = os.MkdirAll(configPath, 0755) configFile := filepath.Join(configPath, "bup.yaml") //fmt.Printf("configFile: %s\n", configFile) - - // Set a specific file to create viper.SetConfigFile(configFile) _ = viper.SafeWriteConfigAs(configFile) if err := viper.ReadInConfig(); err != nil { - fmt.Printf("Error reading config: %s\n", configFile) - return + fmt.Printf("Error reading config: %s\nUsing Default Config!", configFile) } + fmt.Printf("Config File: %s\n", configFile) + } else { + fmt.Printf("Config File: %s\n", viper.ConfigFileUsed()) } } - fmt.Printf("Config File: %s\n", viper.ConfigFileUsed()) }