realize/settings/io.go

38 lines
854 B
Go
Raw Normal View History

2016-11-01 09:56:12 +00:00
package settings
2016-10-21 15:30:12 +00:00
import (
"io/ioutil"
"os"
"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)
if err != nil {
return nil, err
2016-10-21 15:30:12 +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
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)
} 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
}