Skip to content

Commit

Permalink
#4 Refactor and add init function
Browse files Browse the repository at this point in the history
  • Loading branch information
sklein94 committed Aug 10, 2022
1 parent 3bd2b71 commit 2a172da
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
53 changes: 34 additions & 19 deletions archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package archive
import (
"archive/zip"
"fmt"
"github.com/prometheus/common/log"
"io"
"os"
)
Expand Down Expand Up @@ -36,45 +37,51 @@ func (d *defaultFileHandler) copy(dst io.Writer, src io.Reader) (written int64,
return io.Copy(dst, src)
}

type ArchiveHandler interface {
Open(path string) (io.Writer, error)
AppendFileToArchive(path string, writer zip.Writer) error
Close() error
}

type SupportArchiveHandler interface {
type Handler interface {
CreateZipArchiveFile(zipFilePath string) (io.Writer, error)
InitialiseZipWriter(zipFile io.Writer)
AppendFileToArchive(fileToZipPath string, filepathInZip string) error
Close() error
WriteFilesIntoArchive(filePaths []string, closeAfterFinish bool) error
}

// DefaultSupportArchiveHandler
// DefaultHandler
// The normal procedure should look like this.
// 1. CreateZipArchiveFile
// 2. InitialiseZipWriter
// 3. AppendFileToArchive (n times)
// 4. Close
type DefaultSupportArchiveHandler struct {
type DefaultHandler struct {
writer zipWriter
fileCreator fileCreator
fileOpener fileOpener
fileCopier fileCopier
}

func New() SupportArchiveHandler {
return &DefaultSupportArchiveHandler{
func NewHandler() Handler {
return &DefaultHandler{
fileCreator: &defaultFileHandler{},
fileOpener: &defaultFileHandler{},
fileCopier: &defaultFileHandler{},
}
}

func InitArchiveInPath(filePath string) (Handler, error) {
supportArchiveHandler := NewHandler()
supportArchive, err := supportArchiveHandler.CreateZipArchiveFile(filePath)
if err != nil {
return nil, fmt.Errorf("failed to create support archive file: %w", err)
}

supportArchiveHandler.InitialiseZipWriter(supportArchive)

return supportArchiveHandler, nil
}

// CreateZipArchiveFile creates the file that will be the zip archive.
// The zipFilePath expects a complete path with the correct file extension (.zip).
// If you not intend to create an io.Writer beforehand this method can be the input of InitialiseZipWriter.
func (ar *DefaultSupportArchiveHandler) CreateZipArchiveFile(zipFilePath string) (io.Writer, error) {
func (ar *DefaultHandler) CreateZipArchiveFile(zipFilePath string) (io.Writer, error) {
zippedArchiveFile, err := ar.fileCreator.create(zipFilePath)
if err != nil {
return nil, fmt.Errorf("failed to create archive file: %w", err)
Expand All @@ -83,14 +90,14 @@ func (ar *DefaultSupportArchiveHandler) CreateZipArchiveFile(zipFilePath string)
}

// InitialiseZipWriter takes an existing io.Writer and initializes a zip.Writer based on it.
func (ar *DefaultSupportArchiveHandler) InitialiseZipWriter(zipFile io.Writer) {
func (ar *DefaultHandler) InitialiseZipWriter(zipFile io.Writer) {
zipWriter := zip.NewWriter(zipFile)
ar.writer = zipWriter
}

// AppendFileToArchive takes a path to file that is read and appended to an archive.
// make sure to call the Close method when you're done with appending files to the archive.
func (ar *DefaultSupportArchiveHandler) AppendFileToArchive(fileToZipPath string, filepathInZip string) error {
func (ar *DefaultHandler) AppendFileToArchive(fileToZipPath string, filepathInZip string) error {
file, err := ar.fileOpener.open(fileToZipPath)
if err != nil {
return fmt.Errorf("failed to read base file for appending to archive: %w", err)
Expand All @@ -108,23 +115,31 @@ func (ar *DefaultSupportArchiveHandler) AppendFileToArchive(fileToZipPath string
return nil
}

func (ar *DefaultSupportArchiveHandler) Close() error {
func (ar *DefaultHandler) Close() error {
err := ar.writer.Close()
if err != nil {
return fmt.Errorf("could not close archive file: %w", err)
}
return nil
}

func (ar *DefaultSupportArchiveHandler) WriteFilesIntoArchive(filePaths []string, closeAfterFinish bool) error {
func (ar *DefaultHandler) WriteFilesIntoArchive(filePaths []string, closeAfterFinish bool) error {
if closeAfterFinish {
defer func() {
log.Debug("Closing archive handler...")
err := ar.Close()
if err != nil {
log.Error(err)
}
}()
}

for _, filePath := range filePaths {
err := ar.AppendFileToArchive(filePath, filePath)
if err != nil {
return fmt.Errorf("failed to write logfiles into archive: %w", err)
}
}
if closeAfterFinish {
defer ar.Close()
}

return nil
}
28 changes: 14 additions & 14 deletions archive/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@ func (mzw *mockFailedFileCopier) copy(dst io.Writer, src io.Reader) (written int

func TestSupportArchiveHandler_CreateZipArchiveFile(t *testing.T) {
//test success
handler := DefaultSupportArchiveHandler{fileCreator: &mockFileCreator{}}
handler := DefaultHandler{fileCreator: &mockFileCreator{}}
file, err := handler.CreateZipArchiveFile("test.zip")
assert.NoError(t, err)
assert.NotNil(t, file)
// test failure
handler = DefaultSupportArchiveHandler{fileCreator: &mockFailedFileCreator{}}
handler = DefaultHandler{fileCreator: &mockFailedFileCreator{}}
_, err = handler.CreateZipArchiveFile("test.zip")
require.Error(t, err)
}

func TestSupportArchiveHandler_New(t *testing.T) {
supportArchiveHandler := New()
supportArchiveHandler := NewHandler()
assert.NotNil(t, supportArchiveHandler)
}

func TestSupportArchiveHandler_Close(t *testing.T) {
handler := DefaultSupportArchiveHandler{
handler := DefaultHandler{
writer: &mockZipWriter{},
}

Expand All @@ -73,15 +73,15 @@ func TestSupportArchiveHandler_Close(t *testing.T) {
}

func TestSupportArchiveHandler_InitialiseZipWriter(t *testing.T) {
handler := DefaultSupportArchiveHandler{fileCreator: &mockFileCreator{}}
handler := DefaultHandler{fileCreator: &mockFileCreator{}}
file, _ := handler.CreateZipArchiveFile("test.zip")
handler.InitialiseZipWriter(file)

assert.NotNil(t, handler.writer)
}

func TestSupportArchiveHandler_AppendFileToArchive_Success(t *testing.T) {
handler := DefaultSupportArchiveHandler{
handler := DefaultHandler{
fileCreator: &mockFileCreator{},
fileOpener: &defaultFileHandler{},
fileCopier: &defaultFileHandler{},
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestSupportArchiveHandler_AppendFileToArchive_Success(t *testing.T) {

func TestSupportArchiveHandler_AppendFileToArchive_Failure(t *testing.T) {
// first point of failure: Cannot open file
handler := DefaultSupportArchiveHandler{
handler := DefaultHandler{
fileCreator: &mockFileCreator{},
fileOpener: &mockFailedFileOpener{},
}
Expand All @@ -125,7 +125,7 @@ func TestSupportArchiveHandler_AppendFileToArchive_Failure(t *testing.T) {
assert.Contains(t, err.Error(), "failed to open file")

// second point of failure: Cannot open file
handler = DefaultSupportArchiveHandler{
handler = DefaultHandler{
fileCreator: &mockFileCreator{},
fileOpener: &defaultFileHandler{},
writer: &mockZipWriter{},
Expand All @@ -135,7 +135,7 @@ func TestSupportArchiveHandler_AppendFileToArchive_Failure(t *testing.T) {
assert.Contains(t, err.Error(), "failed to create file with writer")

// third point of failure: Cannot copy file into zip archive
handler = DefaultSupportArchiveHandler{
handler = DefaultHandler{
fileCreator: &mockFileCreator{},
fileOpener: &defaultFileHandler{},
fileCopier: &mockFailedFileCopier{},
Expand All @@ -149,7 +149,7 @@ func TestSupportArchiveHandler_AppendFileToArchive_Failure(t *testing.T) {
}

//func TestSupportArchiveHandler_WriteLogFileIntoArchive_Success(t *testing.T) {
// handler := DefaultSupportArchiveHandler{
// handler := DefaultHandler{
// fileCreator: &mockFileCreator{},
// fileOpener: &defaultFileHandler{},
// fileCopier: &defaultFileHandler{},
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestSupportArchiveHandler_AppendFileToArchive_Failure(t *testing.T) {
//
//func TestSupportArchiveHandler_WriteLogFileIntoArchive_Fail(t *testing.T) {
// // first point of failure: Cannot open file
// handler := DefaultSupportArchiveHandler{
// handler := DefaultHandler{
// fileCreator: &mockFileCreator{},
// fileOpener: &mockFailedFileOpener{},
// }
Expand All @@ -192,7 +192,7 @@ func TestSupportArchiveHandler_AppendFileToArchive_Failure(t *testing.T) {
// assert.Contains(t, err.Error(), "no such file or directory")
//
// // second point of failure: Cannot open file
// handler = DefaultSupportArchiveHandler{
// handler = DefaultHandler{
// fileCreator: &mockFileCreator{},
// fileOpener: &defaultFileHandler{},
// writer: &mockZipWriter{},
Expand All @@ -203,7 +203,7 @@ func TestSupportArchiveHandler_AppendFileToArchive_Failure(t *testing.T) {
// assert.Contains(t, err.Error(), "failed to create file with writer")
//
// // third point of failure: Cannot copy file into zip archive
// handler = DefaultSupportArchiveHandler{
// handler = DefaultHandler{
// fileCreator: &mockFileCreator{},
// fileOpener: &defaultFileHandler{},
// fileCopier: &mockFailedFileCopier{},
Expand All @@ -218,7 +218,7 @@ func TestSupportArchiveHandler_AppendFileToArchive_Failure(t *testing.T) {
//

func TestSupportArchiveHandler_WriteFilesIntoArchive(t *testing.T) {
handler := DefaultSupportArchiveHandler{
handler := DefaultHandler{
fileCreator: &mockFileCreator{},
fileOpener: &defaultFileHandler{},
fileCopier: &defaultFileHandler{},
Expand Down

0 comments on commit 2a172da

Please sign in to comment.