mirror of
https://github.com/Slug-Boi/cocommit.git
synced 2026-05-13 12:45:47 +00:00
refactor(depen_injec): add dependency injection to CheckAuthorFile and change to panics
remove comment
This commit is contained in:
@@ -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)
|
if _, err := os.Stat(authorfile); os.IsNotExist(err) {
|
||||||
println("Would you like to create one? (y/n)")
|
fmt.Fprintf(output, "Author file not found at: %s\n", authorfile)
|
||||||
var response string
|
fmt.Fprintf(output, "Would you like to create one? (y/n)\n")
|
||||||
_, err := fmt.Scanln(&response)
|
|
||||||
if err != nil {
|
var response string
|
||||||
println("Error reading response")
|
_, err := fmt.Fscanln(input, &response)
|
||||||
}
|
if err != nil {
|
||||||
if response == "y" {
|
fmt.Fprintln(output, "Error reading response")
|
||||||
parts := strings.Split(authorfile, "/")
|
}
|
||||||
cocommit_folder = strings.Join(parts[:len(parts)-1], "/")
|
|
||||||
|
if response == "y" {
|
||||||
// create the author file
|
parts := strings.Split(authorfile, "/")
|
||||||
if _, dirErr := os.Stat(cocommit_folder); os.IsNotExist(dirErr) {
|
if len(parts) > 1 {
|
||||||
err := os.Mkdir(cocommit_folder, 0766)
|
// remove the last part of the path
|
||||||
if err != nil {
|
cocommit_folder = strings.Join(parts[:len(parts)-1], "/")
|
||||||
fmt.Println("Error creating directory: ", err, cocommit_folder)
|
} else {
|
||||||
os.Exit(1)
|
cocommit_folder = "."
|
||||||
}
|
|
||||||
}
|
|
||||||
file, err := os.Create(authorfile)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error creating file: ", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Create(authorfile)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("error creating file: %v", err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
// write the header to the file
|
// write the header to the file
|
||||||
json_string :=
|
json_string := `{
|
||||||
`{
|
"Authors": {
|
||||||
"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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user