-
Notifications
You must be signed in to change notification settings - Fork 9
/
dir.go
54 lines (44 loc) · 1.25 KB
/
dir.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package keys
import (
"context"
"fmt"
"os"
"path/filepath"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/workspace/dirs"
)
func KeysDir() (fnfs.LocalFS, error) {
cfg, err := dirs.Config()
if err != nil {
if os.IsNotExist(err) {
return nil, ErrKeyGen
}
return nil, err
}
keysDir := filepath.Join(cfg, "keys")
if _, err := os.Stat(keysDir); os.IsNotExist(err) {
return nil, ErrKeyGen
} else if err != nil {
return nil, err
}
return fnfs.Local(keysDir), nil
}
func EnsureKeysDir(ctx context.Context) (fnfs.LocalFS, error) {
cfg, err := dirs.Config()
if err != nil {
return nil, err
}
keysDir := filepath.Join(cfg, "keys")
if st, err := os.Stat(keysDir); os.IsNotExist(err) {
if err := os.MkdirAll(keysDir, 0700); err != nil {
return nil, err
}
} else if mode := st.Mode().Perm(); mode != 0700 {
fmt.Fprintf(console.Stderr(ctx), "%s expected permissions to be %o, saw %o\n", keysDir, 0700, mode)
}
return fnfs.ReadWriteLocalFS(keysDir), nil
}