Skip to content

A temporary directory DAO (data-access object) for use in Golang, to manage a folder with multiple temporary files.

License

Notifications You must be signed in to change notification settings

BytecodeAgency/tempdir-dao

Repository files navigation

Temporary Directory Data-Access Object Build Status

Warning: this library has been developed for R&D purposes for an internal project at Bytecode. This package is not tested for production usage and it is not (yet) feature complete. For example, using sub-directories is not supported (yet)

Why don't you use ioutil.TempFile and/or os.TempDir

This package was created specifically to work with multiple files in a grouped together. This rules out using iouril.TempFile as it only works with single files.

Using os.TempDir was ruled out as it says in the documentation that The directory is neither guaranteed to exist nor have accessible permissions, while this directory does not delete the files/directory until that method has been called explicitly.

Installation

Use go get or your Go package manager choice:

go get -u github.com/BytecodeAgency/tempdir-dao

Usage

To create a new DAO, use

tempDirDAO, err := tempdirdao.NewTempFileAccess() // returns `(*TempFileAccess, error)`

The following methods are available:

Method Functionality Arguments Returns
LoadDirContents Lists contents of temp directory () ([]os.FileInfo, error
GetFullFilePath Gets the full file path of a file in the temp dir (filename string) (string, error)
LoadFileContents Loads contents of a file in the temp dir () ([]byte, error)
SaveFile Created or overwrites a file in the temp dir (filename string, fileContents []byte, permissions os.FileMode) error
RemoveTempFileAccess Removes the temp dir () error

Note: you never need to include the temporary directory name in the arguments for methods

Example

package main

import (
    "github.com/BytecodeAgency/tempdir-dao"
    "log"
)

const filename = "somefile.ext"

func main () {
    fileContents := funcThatReturnsString()
    
    tfa, err := tempdirdao.NewTempFileAccess()
    defer tfa.RemoveTempFileAccess()
    if err != nil {
    	log.Fatal(err)
    }
    err = tfa.SaveFile(filename, fileContents, 0644)
    if err != nil {
        log.Fatal(err)
    }
    
    fullFilePath, err := tfa.GetFullFilePath(filename)
    if err != nil {
        log.Fatal(err)
    }
    funcThatUsesTheTempFile(fullFilePath)
}

License

GNU Lesser General Public License (LGPL-3.0)

About

A temporary directory DAO (data-access object) for use in Golang, to manage a folder with multiple temporary files.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages