/
keys.go
63 lines (49 loc) · 914 Bytes
/
keys.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
55
56
57
58
59
60
61
62
63
package ssh
import (
"errors"
"io/fs"
"os"
"path/filepath"
"github.com/recode-sh/cli/internal/system"
)
const PrivateKeyFilePerm os.FileMode = 0600
type Keys struct {
sshDir string
}
func NewKeys(SSHDir string) Keys {
return Keys{
sshDir: SSHDir,
}
}
func NewKeysWithDefaultDir() Keys {
return NewKeys(
system.DefaultSSHDir(),
)
}
func (k Keys) CreateOrReplacePEM(
PEMName string,
PEMContent string,
) (pathWritten string, err error) {
pathWritten = filepath.Join(k.sshDir, PEMName+".pem")
err = os.WriteFile(
pathWritten,
[]byte(PEMContent),
PrivateKeyFilePerm,
)
return
}
func (k Keys) RemovePEMIfExists(PEMName string) error {
err := os.Remove(
k.GetPEMFilePath(PEMName),
)
if err != nil && errors.Is(err, fs.ErrNotExist) {
return nil
}
return err
}
func (k Keys) GetPEMFilePath(PEMName string) string {
return filepath.Join(
k.sshDir,
PEMName+".pem",
)
}