aboutsummaryrefslogtreecommitdiffstats
path: root/key/file.go
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2025-04-11 14:19:12 -0400
committerSam Anthony <sam@samanthony.xyz>2025-04-11 14:19:12 -0400
commitb5901aa3caddd987c5f4b6bc1ab7f62249580fcc (patch)
tree2555d5c7bc15979d7e363a6ef97587a70eff0844 /key/file.go
parent434434f6fa361daf599f6e7a35aa3b85a664a17f (diff)
downloadhose-b5901aa3caddd987c5f4b6bc1ab7f62249580fcc.zip
load public key from file
Diffstat (limited to 'key/file.go')
-rw-r--r--key/file.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/key/file.go b/key/file.go
new file mode 100644
index 0000000..f96190a
--- /dev/null
+++ b/key/file.go
@@ -0,0 +1,59 @@
+package key
+
+import (
+ "errors"
+ "fmt"
+ "github.com/adrg/xdg"
+ "os"
+ "path/filepath"
+)
+
+var (
+ pubKeyFile = filepath.Join(xdg.DataHome, "hose", "pubkey")
+ pubKeyFileMode os.FileMode = 0644
+
+ privKeyFile = filepath.Join(xdg.DataHome, "hose", "privkey")
+ privKeyFileMode os.FileMode = 0600
+)
+
+// createFile creates a file with the specified permissions and returns it for writing.
+// It does not truncate an existing file. If the file already exists, an error is returned.
+func createFile(name string, mode os.FileMode) (*os.File, error) {
+ exists, err := fileExists(name)
+ if err != nil {
+ return nil, err // unexpected error.
+ } else if exists {
+ return nil, errFileExists(name) // file exists; do not overwrite.
+ }
+ // Does not exist; continue;
+
+ f, err := os.Create(name)
+ if err != nil {
+ return nil, err
+ }
+
+ if err := f.Chmod(mode); err != nil {
+ f.Close()
+ _ = os.Remove(name)
+ return nil, err
+ }
+
+ return f, nil
+}
+
+// fileExists returns a nil error and true/false if a file does/doesn't exist.
+// If an error is encountered, a non-nil error is returned.
+func fileExists(path string) (bool, error) {
+ _, err := os.Stat(path)
+ if errors.Is(err, os.ErrNotExist) {
+ return false, nil // file doesn't exist.
+ } else if err != nil {
+ return false, err // unexpected error.
+ }
+ return true, nil // file exists.
+}
+
+// errFileExists constructs a 'file already exists' error message.
+func errFileExists(path string) error {
+ return fmt.Errorf("%s: %s", os.ErrExist, path)
+}