staging #77

Merged
bagas merged 48 commits from staging into main 2026-01-27 18:08:36 +07:00
Showing only changes of commit e796ab5328 - Show all commits
+9 -3
View File
@@ -5,6 +5,7 @@ import (
"crypto/rsa" "crypto/rsa"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"errors"
"io" "io"
"log" "log"
"os" "os"
@@ -26,6 +27,7 @@ var (
) )
func GenerateSSHKeyIfNotExist(keyPath string) error { func GenerateSSHKeyIfNotExist(keyPath string) error {
var errGroup = make([]error, 0)
if _, err := os.Stat(keyPath); err == nil { if _, err := os.Stat(keyPath); err == nil {
log.Printf("SSH key already exists at %s", keyPath) log.Printf("SSH key already exists at %s", keyPath)
return nil return nil
@@ -52,7 +54,9 @@ func GenerateSSHKeyIfNotExist(keyPath string) error {
if err != nil { if err != nil {
return err return err
} }
defer privateKeyFile.Close() defer func(privateKeyFile *os.File) {
errGroup = append(errGroup, privateKeyFile.Close())
}(privateKeyFile)
if err := pemEncode(privateKeyFile, privateKeyPEM); err != nil { if err := pemEncode(privateKeyFile, privateKeyPEM); err != nil {
return err return err
@@ -68,7 +72,9 @@ func GenerateSSHKeyIfNotExist(keyPath string) error {
if err != nil { if err != nil {
return err return err
} }
defer pubKeyFile.Close() defer func(pubKeyFile *os.File) {
errGroup = append(errGroup, pubKeyFile.Close())
}(pubKeyFile)
_, err = pubKeyWrite(pubKeyFile, ssh.MarshalAuthorizedKey(publicKey)) _, err = pubKeyWrite(pubKeyFile, ssh.MarshalAuthorizedKey(publicKey))
if err != nil { if err != nil {
@@ -76,5 +82,5 @@ func GenerateSSHKeyIfNotExist(keyPath string) error {
} }
log.Printf("SSH key pair generated successfully at %s and %s", keyPath, pubKeyPath) log.Printf("SSH key pair generated successfully at %s and %s", keyPath, pubKeyPath)
return nil return errors.Join(errGroup...)
} }