realize/settings/io.go

38 lines
870 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
)
// 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
func (s Settings) Create(path string, name string) *os.File {
var file string
if _, err := os.Stat(".realize/"); err == nil {
file = filepath.Join(path, ".realize/", 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
}