refactor(depen_injec): add dependency injection to CheckAuthorFile and change to panics

remove comment
This commit is contained in:
Slug-Boi
2025-04-09 22:35:19 +02:00
parent 73020870a1
commit c8923525ea
+45 -42
View File
@@ -3,6 +3,7 @@ package utils
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"os" "os"
"strings" "strings"
) )
@@ -23,53 +24,55 @@ func Find_authorfile() string {
} }
} }
func CheckAuthorFile() string { func CheckAuthorFile(input io.Reader, output io.Writer) (string,error) {
var cocommit_folder string var cocommit_folder string
authorfile := Find_authorfile() authorfile := Find_authorfile()
if _, err := os.Stat(authorfile); os.IsNotExist(err) {
println("Author file not found at: ", authorfile)
println("Would you like to create one? (y/n)")
var response string
_, err := fmt.Scanln(&response)
if err != nil {
println("Error reading response")
}
if response == "y" {
parts := strings.Split(authorfile, "/")
cocommit_folder = strings.Join(parts[:len(parts)-1], "/")
// create the author file if _, err := os.Stat(authorfile); os.IsNotExist(err) {
if _, dirErr := os.Stat(cocommit_folder); os.IsNotExist(dirErr) { fmt.Fprintf(output, "Author file not found at: %s\n", authorfile)
err := os.Mkdir(cocommit_folder, 0766) fmt.Fprintf(output, "Would you like to create one? (y/n)\n")
if err != nil {
fmt.Println("Error creating directory: ", err, cocommit_folder) var response string
os.Exit(1) _, err := fmt.Fscanln(input, &response)
} if err != nil {
} fmt.Fprintln(output, "Error reading response")
file, err := os.Create(authorfile) }
if err != nil {
fmt.Println("Error creating file: ", err) if response == "y" {
os.Exit(1) parts := strings.Split(authorfile, "/")
if len(parts) > 1 {
// remove the last part of the path
cocommit_folder = strings.Join(parts[:len(parts)-1], "/")
} else {
cocommit_folder = "."
} }
defer file.Close() // create the author file
if _, dirErr := os.Stat(cocommit_folder); os.IsNotExist(dirErr) {
err := os.Mkdir(cocommit_folder, 0766)
if err != nil {
return "", fmt.Errorf("error creating directory: %v %s", err, cocommit_folder)
}
}
// write the header to the file file, err := os.Create(authorfile)
json_string := if err != nil {
`{ return "", fmt.Errorf("error creating file: %v", err)
"Authors": { }
} defer file.Close()
// write the header to the file
json_string := `{
"Authors": {
}
}` }`
file.Write([]byte(json_string))
file.Write([]byte(json_string)) fmt.Fprintln(output, "Author file created. To add authors please launch the TUI with -a and press 'C'")
} else {
fmt.Println("Author file created. To add authors please launch the TUI with -a and press 'C'") os.Exit(0)
} else { }
os.Exit(1) }
} return authorfile, nil
}
// This string output is mostly for convenience can mostly be ignored
return authorfile
} }
func CreateAuthor(user User) { func CreateAuthor(user User) {