Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tmpfile package added #1

Merged
merged 1 commit into from May 17, 2016
Merged

tmpfile package added #1

merged 1 commit into from May 17, 2016

Conversation

odeke-em
Copy link
Owner

Added a package for tmpfile usage and management. It exposes a few methods
of the underlying temp file like:

  • Read
  • Seek
  • ReadAt
  • Write
  • Close
  • Stat

But also the management of this package kicks in when you invoke
.Done() which runs exactly once and removes the file.

This package exposes two different flavours of usage:

  • NewInIsolatedDir - Use this in packages for which you'd just to use a
    unique temp file in an isolated directory, Read from, Write to it and
    discard it at the end
    Sample usage
tmpf, err := tmpfile.NewInIsolatedDir()
if err != nil {
  return nil, err
}

defer tmpf.Done() // Cleanup after self.
if _, err = io.Copy(tmpf, cipherSource); err != nil {
  return nil, err
}
  • New - Use this in situations where you'd like flexibility on suffix
    and location of directory/temp file.
    Sample usage
tmpf, err := tmpfile.New(&tmpfile.Context{
  Dir: "trims",
  Suffix: "mkv",
  CreateInIsolatedDir: true,
})
if err != nil {
  return nil, err
}

defer tmpf.Done() // Cleanup after self.
if _, err = io.Copy(tmpf, mediaRes.Body); err != nil {
  return nil, err
}

Added a package for tmpfile usage and management. It exposes a few methods
of the underlying temp file like:
+ Read
+ Seek
+ ReadAt
+ Write
+ Close
+ Stat

But also the management of this package kicks in when you invoke
`.Done()` which runs exactly once and removes the file.

This package exposes two different flavours of usage:
+ NewInIsolatedDir - Use this in packages for which you'd just to use a
unique temp file in an isolated directory, Read from, Write to it and
discard it at the end
Sample usage

```go
tmpf, err := tmpfile.NewInIsolatedDir()
if err != nil {
  return nil, err
}

defer tmpf.Done() // Cleanup after self.
if _, err = io.Copy(tmpf, cipherSource); err != nil {
  return nil, err
}
```

+ New - Use this in situations where you'd like flexibility on suffix
and location of directory/temp file.
Sample usage

```go
tmpf, err := tmpfile.New(&tmpfile.Context{
  Dir: "trims",
  Suffix: "mkv",
  CreateInIsolatedDir: true,
})
if err != nil {
  return nil, err
}

defer tmpf.Done() // Cleanup after self.
if _, err = io.Copy(tmpf, mediaRes.Body); err != nil {
  return nil, err
}
```
var abortFn func() error
ownCreatedDir := false
if ctx.CreateIsolatedDir {
dir = filepath.Join(os.Getenv("TMPDIR"), dir, uuid.UUID4().String())
Copy link

@sselph sselph May 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm having an issue from this. My system doesn't have TMPDIR. Maybe it could use os.TempDir? or ioutil.TempDir. It is placing files in the current working directory.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. For some reason I though os.TempDir and ioutil.TempDir respect $TMPDIR. Thanks for letting me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants