refactor(json): create author now works on new json authorfile

This commit is contained in:
Slug-Boi
2025-04-02 18:35:10 +02:00
parent 4a8572efdf
commit 5106e9ce1e
+25 -17
View File
@@ -4,6 +4,7 @@ package tui
// from the Bubbles component library.
import (
"encoding/json"
"fmt"
"os"
"strings"
@@ -250,28 +251,35 @@ func (m *model_ca) AddAuthor() {
defer f.Close()
sb := strings.Builder{}
sb.WriteRune('\n')
sb.WriteString(fmt.Sprintf("%s|%s|%s|%s",
m.inputs[0].Value(),
m.inputs[1].Value(),
m.inputs[2].Value(),
m.inputs[3].Value()))
if m.exclude {
sb.WriteString(fmt.Sprintf("|%s", "ex"))
// create and add the user to the users map
usr := utils.User{
Shortname: m.inputs[0].Value(),
Longname: m.inputs[1].Value(),
Username: m.inputs[2].Value(),
Email: m.inputs[3].Value(),
Ex: m.exclude,
Groups: strings.Split(m.inputs[4].Value(), "|"),
}
if m.inputs[4].Value() != "" {
sb.WriteString(fmt.Sprintf(";;%s", m.inputs[4].Value()))
utils.Users[m.inputs[0].Value()] = usr
utils.Users[m.inputs[1].Value()] = usr
utils.Authors.Authors[m.inputs[1].Value()] = usr
data, err := json.MarshalIndent(utils.Authors, "", " ")
if err != nil {
panic(fmt.Sprintf("Error marshalling json: %v", err))
}
//sb.WriteRune('\n')
// write the data to the file
f.Truncate(0)
f.Seek(0, 0)
f.Write(data)
f.Close()
if _, err = f.WriteString(sb.String()); err != nil {
panic(err)
}
// redefine the users map for the tui to use
utils.Define_users(utils.Find_authorfile())
author := m.inputs[0].Value()