feat: Utilities for file creation.
This commit is contained in:
1 parent
ceac86f155
commit
16474e57b9
1 file changed
+24
@@ -0,0 +1,24 @@
|
|||||||
|
package hmpki
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CreateFileWith Creates a new file (fails it if already exists) with the
|
||||||
|
// given content and permissions.
|
||||||
|
func createFileWith(name string, content []byte, perms os.FileMode) error {
|
||||||
|
f, err := os.OpenFile(
|
||||||
|
name,
|
||||||
|
// Write only, and fail if it alreay exists.
|
||||||
|
os.O_WRONLY|os.O_CREATE|os.O_EXCL,
|
||||||
|
perms,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
if _, err = f.Write(content); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in new issue
Block a user