Skip to content

Commit

Permalink
feat: archive compression
Browse files Browse the repository at this point in the history
Added the --compress option to the nuke command. This allows for the automatic compression of the archive file to gzip format.
  • Loading branch information
jlanzarotta committed Feb 19, 2025
1 parent 80bfb0a commit 85ffb33
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
17 changes: 16 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ All 639 entries would have been nuked.
==== archive
The `archive` options tells Khronos that you would like to archive the entries that are nuked from your database. The nuked records are written to a _khronos_archive_yyyymmddhhmmss.csv_ file.
The `archive` option tells Khronos that you would like to archive the entries that are nuked from your database. The nuked records are written to a _khronos_archive_yyyymmddhhmmss.csv_ file.
[source, shell]
----
Expand All @@ -466,6 +466,21 @@ $ls -l
.a--- 58 KB Tue 2025-02-18 08:25:48 PM . khronos_archive_20250218202548.csv
----
==== compress
The `compress` option tells Khronos that you would like to have the archive file automatically compressed. The compression format is _gzip_ and the compressed file will be named _khronos_archive_yyyymmddhhmmss.csv.gz_.
----
$k nuke --all --archive --compress
Are you sure you want to nuke ALL the entries from your database? (Y/N (yes/no)) yes
WARNING: Are you REALLY sure you want to nuke ALL the entries from your database? (Y/N (yes/no)) yes
LAST WARNING: Are you REALLY REALLY sure you want to nuke ALL the entries from your database? (Y/N (yes/no)) yes
All 639 entries would have been nuked.
$ls -l
.a--- 58 KB Tue 2025-02-18 08:25:48 PM . khronos_archive_20250218202548.csv.gz
----
=== show
The `show` command tells Khronos you would like to show various information.
Expand Down
6 changes: 4 additions & 2 deletions cmd/nuke.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func init() {
nukeCmd.Flags().BoolP(constants.PRIOR_YEARS, constants.EMPTY, false, constants.PRIOR_YEARS_DESCRIPTION)
nukeCmd.Flags().BoolP(constants.DRY_RUN, constants.EMPTY, false, constants.DRY_RUN_DESCRIPTION)
nukeCmd.Flags().BoolP(constants.ARCHIVE, constants.EMPTY, false, constants.ARCHIVE_DESCRIPTION)
nukeCmd.Flags().BoolP(constants.COMPRESS, constants.EMPTY, false, constants.COMPRESS_DESCRIPTION)
rootCmd.AddCommand(nukeCmd)
}

Expand All @@ -69,6 +70,7 @@ func runNuke(cmd *cobra.Command, _ []string) {
priorYears, _ := cmd.Flags().GetBool(constants.PRIOR_YEARS)
dryRun, _ := cmd.Flags().GetBool(constants.DRY_RUN)
archive, _ := cmd.Flags().GetBool(constants.ARCHIVE)
compress, _ := cmd.Flags().GetBool(constants.COMPRESS)

if all {
yesNo := yesNoPrompt("Are you sure you want to nuke ALL the entries from your database?")
Expand All @@ -79,7 +81,7 @@ func runNuke(cmd *cobra.Command, _ []string) {
if yesNo {
// Yes was enter, so nuke ALL entries.
db := database.New(viper.GetString(constants.DATABASE_FILE))
var count = db.NukeAllEntries(dryRun, archive)
var count = db.NukeAllEntries(dryRun, archive, compress)
showExplosion()
if dryRun {
log.Printf("%s\n", color.HiBlueString("All %d entries would have been nuked.", count))
Expand Down Expand Up @@ -107,7 +109,7 @@ func runNuke(cmd *cobra.Command, _ []string) {
yesNo = yesNoPrompt(prompt)
if yesNo {
db := database.New(viper.GetString(constants.DATABASE_FILE))
var count = db.NukePriorYearsEntries(dryRun, year, archive)
var count = db.NukePriorYearsEntries(dryRun, year, archive, compress)
showExplosion()
if dryRun {
log.Printf("%s\n", color.YellowString("All %d entries prior to %d would have been nuked.\n", count, year))
Expand Down
2 changes: 2 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const BREAK_SHORT_DESCRIPTION = "Add a break"
const CARBON_DATE_FORMAT string = "Y-m-d"
const CARBON_START_END_TIME_FORMAT string = "h:ia"
const CARBON_START_END_TIME_24H_FORMAT string = "H:i"
const COMPRESS string = "compress"
const COMPRESS_DESCRIPTION string = "Compress archive file in gzip format"
const CONFIGURATION_FILE string = ".khronos.yaml"
const CONFIGURE_LONG_DESCRIPTION = "Write out a YAML config file. Print path to config file."
const CONFIGURE_SHORT_DESCRIPTION = "Write out a YAML config file"
Expand Down
57 changes: 50 additions & 7 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ POSSIBILITY OF SUCH DAMAGE.
package database

import (
"compress/gzip"
"context"
"database/sql"
"fmt"
"io"
"log"
"os"
"strings"
Expand Down Expand Up @@ -369,14 +371,14 @@ func (db *Database) GetCountEntries() int64 {
return count
}

func CreateArchiveFile(entryWithProperty []EntryWithProperty) {
func CreateArchiveFile(entryWithProperty []EntryWithProperty, compress bool) {
// Create our unique archive file.
archiveFile, err := os.Create(constants.APPLICATION_NAME_LOWERCASE + "_archive_" + carbon.Now().ToShortDateTimeString() + ".csv")
var filename = constants.APPLICATION_NAME_LOWERCASE + "_archive_" + carbon.Now().ToShortDateTimeString()
archiveFile, err := os.Create(filename + ".csv")
if err != nil {
log.Fatalf("%s: Error trying to create archive file. %s\n", color.RedString(constants.FATAL_NORMAL_CASE), err.Error())
os.Exit(1)
}

defer archiveFile.Close()

_, err = archiveFile.WriteString("uid,project,note,entry_date_time,name,value\n")
Expand All @@ -396,9 +398,50 @@ func CreateArchiveFile(entryWithProperty []EntryWithProperty) {

// Flush the archive file to disk so it can be closed.
archiveFile.Sync()
archiveFile.Close()

if compress {
// Open the original archive file.
originalFile, err := os.Open(filename + ".csv")
if err != nil {
log.Fatalf("%s: Error opening original archive file. %s\n", color.RedString(constants.FATAL_NORMAL_CASE), err.Error())
os.Exit(1)
}
defer originalFile.Close()

// Create a new gzipped file.
gzippedFile, err := os.Create(filename + ".csv.gz")
if err != nil {
log.Fatalf("%s: Error creating gzip file. %s\n", color.RedString(constants.FATAL_NORMAL_CASE), err.Error())
os.Exit(1)
}
defer gzippedFile.Close()

// Create a new gzip writer.
gzipWriter := gzip.NewWriter(gzippedFile)
defer gzipWriter.Close()

// Copy the contents of the original file to the gzip writer.
_, err = io.Copy(gzipWriter, originalFile)
if err != nil {
log.Fatalf("%s: Error writing to gzip file. %s\n", color.RedString(constants.FATAL_NORMAL_CASE), err.Error())
os.Exit(1)
}

// Flush the gzip writer to ensure all data is written.
gzipWriter.Flush()

// Delete the original archive file.
originalFile.Close()
err = os.Remove(filename + ".csv")
if err != nil {
log.Fatalf("%s: Error deleting original archive file after compressing. %s\n", color.RedString(constants.FATAL_NORMAL_CASE), err.Error())
os.Exit(1)
}
}
}

func (db *Database) NukePriorYearsEntries(dryRun bool, year int, archive bool) int64 {
func (db *Database) NukePriorYearsEntries(dryRun bool, year int, archive bool, compress bool) int64 {
var count int64 = 0
var query strings.Builder

Expand Down Expand Up @@ -444,7 +487,7 @@ func (db *Database) NukePriorYearsEntries(dryRun bool, year int, archive bool) i
// If the user wants an archive, write the archived collection to a
// file.
if archive {
CreateArchiveFile(archiveRecords)
CreateArchiveFile(archiveRecords, compress)
}

// Create a transaction.
Expand Down Expand Up @@ -499,7 +542,7 @@ func (db *Database) NukePriorYearsEntries(dryRun bool, year int, archive bool) i
return count
}

func (db *Database) NukeAllEntries(dryRun bool, archive bool) int64 {
func (db *Database) NukeAllEntries(dryRun bool, archive bool, compress bool) int64 {
var count int64 = 0

if !dryRun {
Expand Down Expand Up @@ -530,7 +573,7 @@ func (db *Database) NukeAllEntries(dryRun bool, archive bool) int64 {
// If the user wants an archive, write the archived collection to a
// file.
if archive {
CreateArchiveFile(archiveRecords)
CreateArchiveFile(archiveRecords, compress)
}

// Create a transaction.
Expand Down

0 comments on commit 85ffb33

Please sign in to comment.