From 39781da299ffb91ab9ea4728078b218dbc01235d Mon Sep 17 00:00:00 2001 From: Slug-Boi Date: Wed, 2 Apr 2025 18:25:14 +0200 Subject: [PATCH] refactor(json): add ability to delete author using the new json author file --- src/cmd/utils/author_file_utils.go | 52 +++++++++++++----------------- src/cmd/utils/user_util.go | 9 ++++-- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/cmd/utils/author_file_utils.go b/src/cmd/utils/author_file_utils.go index 1b071d5..ea63fed 100644 --- a/src/cmd/utils/author_file_utils.go +++ b/src/cmd/utils/author_file_utils.go @@ -1,11 +1,9 @@ package utils import ( - "bufio" - "bytes" + "encoding/json" "fmt" "os" - "regexp" "strings" ) @@ -71,47 +69,43 @@ func CheckAuthorFile() string { func DeleteOneAuthor(author string) { author_file := Find_authorfile() + if _, exists := Users[author]; !exists { + fmt.Println("User not found") + return + } + // open author_file - file, err := os.OpenFile(author_file, os.O_RDWR, 0644) + file, err := os.OpenFile(author_file, os.O_RDWR, 0666) if err != nil { fmt.Println("Error opening file: ", err) return } - defer file.Close() - - // create regex to capture author line - regexp, err := regexp.Compile(fmt.Sprintf("^(.+\\|%s\\|.+|%s\\|.+\\|.+)$", author, author)) - if err != nil { - fmt.Println("Error compiling regex: ", err) + // check that users aren't empty + if len(Users) < 1 { + fmt.Println("No users to remove") return } - var b []byte - buf := bytes.NewBuffer(b) + usr := Users[author] - // create a scanner for the file - scanner := bufio.NewScanner(file) - - // write the header to the buffer - scanner.Scan() - buf.WriteString(scanner.Text() + "\n") - - // check if author matches the regex and skip - for scanner.Scan() { - line := scanner.Text() - if regexp.MatchString(line) { - continue - } - buf.WriteString(line + "\n") + // Remove the user from the Author struct (try both short and long name) + delete(Authors.Authors, usr.Shortname) + delete(Authors.Authors, usr.Longname) + // marshal the struct back to json + data, err := json.MarshalIndent(Authors, "", " ") + if err != nil { + fmt.Println("Error marshalling json: ", err) + return } - // remove the last newline character - buf.Truncate(buf.Len() - 1) + + // write the data to the file file.Truncate(0) file.Seek(0, 0) - buf.WriteTo(file) + file.Write(data) + file.Close() RemoveUser(author) } diff --git a/src/cmd/utils/user_util.go b/src/cmd/utils/user_util.go index 7a509ea..baeeb51 100644 --- a/src/cmd/utils/user_util.go +++ b/src/cmd/utils/user_util.go @@ -23,6 +23,9 @@ type Author struct { Authors map[string]User } +// purely used for editing the author file later +var Authors = Author{} + var Users = map[string]User{} var DefExclude = []string{} var Groups = map[string][]User{} @@ -45,7 +48,7 @@ func Define_users(author_file string) { Groups = map[string][]User{} var auth Author - + data, err := os.ReadFile(author_file) if err != nil { fmt.Println("Error reading author file: ", err) @@ -56,7 +59,9 @@ func Define_users(author_file string) { fmt.Println("Error unmarshalling json: ", err) os.Exit(2) } - + + Authors = auth + for _, usr := range auth.Authors { Users[usr.Shortname] = usr Users[usr.Longname] = usr