realize/settings/io.go

31 lines
678 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"
)
// Scan 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 {
content, err := ioutil.ReadFile(file)
2016-11-01 09:56:12 +00:00
s.Validate(err)
2016-10-21 15:30:12 +00:00
return content, err
}
return nil, err
}
// 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-11-01 09:56:12 +00:00
func (s Settings) Create(file string) *os.File {
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
}