forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executable.go
54 lines (46 loc) · 1.2 KB
/
executable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package vacation
import (
"io"
"os"
"path/filepath"
)
// An Executable writes an executable files from an input stream to the with a
// file name specified by the option `Executable.WithName()` (or defaults to
// `artifact`) in the destination directory with executable permissions (0755).
type Executable struct {
reader io.Reader
name string
}
// NewExecutable returns a new Executable that reads from inputReader.
func NewExecutable(inputReader io.Reader) Executable {
return Executable{
reader: inputReader,
name: "artifact",
}
}
// Decompress copies the reader contents into the destination specified and
// sets executable permissions.
func (e Executable) Decompress(destination string) error {
file, err := os.Create(filepath.Join(destination, e.name))
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, e.reader)
if err != nil {
return err
}
err = os.Chmod(filepath.Join(destination, e.name), 0755)
if err != nil {
return err
}
return nil
}
// WithName provides a way of overriding the name of the file
// that the decompressed file will be copied into.
func (e Executable) WithName(name string) Executable {
if name != "" {
e.name = name
}
return e
}