From 5106e9ce1eb0a332250b82936112d2c925a196e6 Mon Sep 17 00:00:00 2001 From: Slug-Boi Date: Wed, 2 Apr 2025 18:35:10 +0200 Subject: [PATCH] refactor(json): create author now works on new json authorfile --- src/cmd/tui/tui_author.go | 42 +++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/cmd/tui/tui_author.go b/src/cmd/tui/tui_author.go index 6213ff1..d87079f 100644 --- a/src/cmd/tui/tui_author.go +++ b/src/cmd/tui/tui_author.go @@ -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()