Files
scm/utils/CheckOrCreateRootKeyCertificate.go
T
wt 39c27b0de3
Build / Build (push) Successful in 13s
added root generation
2025-07-21 16:36:28 +07:00

51 lines
1.7 KiB
Go

package utils
import (
"fmt"
"os"
)
func CheckOrCreateRootKeyCertificate() error {
_, err := os.Stat(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root"))
if err == nil {
_, err := os.Stat(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root/root.key"))
if err == nil {
_, err := os.Stat(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root/root.crt"))
if err == nil {
return fmt.Errorf("%s", "key already exists")
} else if os.IsNotExist(err) {
os.RemoveAll(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root"))
err = CreateRootKeyCertificate()
if err != nil {
fmt.Println("error creating root key certificate:", err)
return fmt.Errorf("%s", "error creating root key certificate")
}
} else {
fmt.Println("error checking certificate directory:", err)
return fmt.Errorf("%s", "error checking certificate directory")
}
} else if os.IsNotExist(err) {
os.RemoveAll(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root"))
err = CreateRootKeyCertificate()
if err != nil {
fmt.Println("error creating root key certificate:", err)
return fmt.Errorf("%s", "error creating root key certificate")
}
} else {
fmt.Println("error checking certificate directory:", err)
return fmt.Errorf("%s", "error checking certificate directory")
}
} else if os.IsNotExist(err) {
os.RemoveAll(fmt.Sprintf("%s/%s", os.Getenv("CERTS_PATH"), "root"))
err = CreateRootKeyCertificate()
if err != nil {
fmt.Println("error creating root key certificate:", err)
return fmt.Errorf("%s", "error creating root key certificate")
}
} else {
fmt.Println("error checking certificate directory:", err)
return fmt.Errorf("%s", "error checking certificate directory")
}
return nil
}