Refactored config system again
All checks were successful
Build (artifact) / build (push) Successful in 27s

This commit is contained in:
2026-06-30 16:48:05 -04:00
parent c01195643a
commit 0a78b70821
18 changed files with 478 additions and 175 deletions

View File

@@ -1,8 +1,11 @@
package app
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"os/user"
@@ -15,7 +18,8 @@ import (
var (
VersionCmd = basicCmd("version", "Show version", versionCmd)
NewKeyCmd = basicCmd("gen-key", "Generates encryption key", newKeyCmd)
NewKeyCmd = basicCmd("gen-key", "Generates encryption key", GenKeyCmd)
UpdateCmd = basicCmd("update", "Updates if new version is found", updateCmd)
DevCmd = basicCmd("dev", "Dev Function", devCmd)
domainCertDir string
@@ -74,7 +78,7 @@ func versionCmd(cmd *cobra.Command, args []string) {
)
}
func newKeyCmd(cmd *cobra.Command, args []string) {
func GenKeyCmd(cmd *cobra.Command, args []string) {
key, err := common.GenerateKey()
if err != nil {
log.Fatalf("%v", err)
@@ -82,6 +86,41 @@ func newKeyCmd(cmd *cobra.Command, args []string) {
fmt.Printf(key)
}
func updateCmd(cmd *cobra.Command, args []string) {
if os.Geteuid() != 0 {
log.Fatal(fmt.Errorf("installation must be run as root"))
}
err := LoadConfig()
if err != nil {
log.Fatalf("%v", err)
}
resp, err := http.Get(SourceCertmanRepo + "/latest")
if err != nil {
log.Fatalf("%v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
var release GiteaRelease
err = json.Unmarshal(body, &release)
if err != nil {
log.Fatalf("%v", err)
}
for _, asset := range release.Assets {
if strings.Contains(asset.Name, Config().App.Mode) {
if !strings.Contains(asset.Name, common.Version) {
fmt.Printf("Found a newer version: %s\n", asset.Name)
err = downloadFile("/usr/local/bin/certman", SourceCertmanRepo+"/"+strconv.Itoa(release.Id)+"/assets/"+strconv.Itoa(asset.Id))
if err != nil {
log.Fatalf("%v", err)
}
}
}
}
}
func newDomainCmd(domain, domainDir string, dirOverridden bool) error {
//TODO add config option for "overridden dir"
if !common.IsValidFQDN(domain) {
@@ -160,15 +199,8 @@ func installCmd(isThin bool, mode string) error {
return fmt.Errorf("error creating group: %v: output %s", err, output)
}
}
certmanUser, err := user.Lookup("certman")
if err != nil {
return fmt.Errorf("error getting user certman: %v", err)
}
uid, err := strconv.Atoi(strings.TrimSpace(certmanUser.Uid))
if err != nil {
return err
}
gid, err := strconv.Atoi(strings.TrimSpace(certmanUser.Gid))
//TODO chmod after chown
uid, gid, err := GetUserIds("certman")
if err != nil {
return err
}
@@ -187,5 +219,12 @@ func installCmd(isThin bool, mode string) error {
} else {
CreateConfig(mode)
}
//TODO move executable to /usr/bin
//execPath, err := os.Executable()
//if err != nil {
// return err
//}
return nil
}