2016-11-01 09:56:12 +00:00
|
|
|
package settings
|
2016-10-21 15:30:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-12-17 00:48:04 +00:00
|
|
|
"path/filepath"
|
2016-10-21 15:30:12 +00:00
|
|
|
)
|
|
|
|
|
2016-12-17 10:43:27 +00:00
|
|
|
// Stream return a byte stream of a given file
|
2016-11-01 09:56:12 +00:00
|
|
|
func (s Settings) Stream(file string) ([]byte, error) {
|
2016-10-21 15:30:12 +00:00
|
|
|
_, err := os.Stat(file)
|
2017-04-13 12:54:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-10-21 15:30:12 +00:00
|
|
|
}
|
2017-04-13 12:54:13 +00:00
|
|
|
content, err := ioutil.ReadFile(file)
|
|
|
|
s.Validate(err)
|
|
|
|
return content, err
|
2016-10-21 15:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write a file given a name and a byte stream
|
2016-11-01 09:56:12 +00:00
|
|
|
func (s Settings) Write(name string, data []byte) error {
|
2016-10-21 15:30:12 +00:00
|
|
|
err := ioutil.WriteFile(name, data, 0655)
|
2016-11-01 09:56:12 +00:00
|
|
|
return s.Validate(err)
|
2016-10-21 15:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new file and return its pointer
|
2016-12-17 00:48:04 +00:00
|
|
|
func (s Settings) Create(path string, name string) *os.File {
|
|
|
|
var file string
|
2017-04-16 10:02:47 +00:00
|
|
|
if _, err := os.Stat(Dir); err == nil {
|
|
|
|
file = filepath.Join(path, Dir, name)
|
2016-12-17 00:48:04 +00:00
|
|
|
} else {
|
|
|
|
file = filepath.Join(path, name)
|
|
|
|
}
|
2016-10-21 15:30:12 +00:00
|
|
|
out, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_SYNC, 0655)
|
2016-11-01 09:56:12 +00:00
|
|
|
s.Validate(err)
|
2016-10-21 15:30:12 +00:00
|
|
|
return out
|
|
|
|
}
|