feat: added author file default path and config creation utility

This commit is contained in:
Slug-Boi
2024-05-19 23:07:15 +02:00
parent dbc32f5106
commit e355bec97d
+75 -2
View File
@@ -36,10 +36,37 @@ var groups = make(map[string][]user)
func main() {
// Reads a shell env variable :: author_file
authors := os.Getenv("author_file")
var authors string
envVar := os.Getenv("author_file")
if envVar == "" {
var err error
authors, err = os.UserConfigDir()
authors += "/cocommit/authors"
if err != nil {
println("Error: ", err)
os.Exit(1)
}
} else {
authors = envVar
}
file, err := os.Open(authors)
check_err(err)
if err != nil {
authors, _ = os.UserConfigDir()
authors += "/cocommit/authors"
println("Authors file cannot be found. Please check the path to the file. \nEither set the author_file env variable or place the file in the default location. \nDefault location: "+ authors)
println("If you want to create a blank template file at the default location type y|yes or cancel with n|no")
var input string
fmt.Scanln(&input)
if input == "y" || input == "yes" {
create_author_file("yes")
os.Exit(1)
} else {
println("Cancelled")
os.Exit(1)
}
}
defer file.Close()
scanner := bufio.NewScanner(file)
@@ -206,6 +233,8 @@ func NoInput(args []string, users map[string]user) {
sort.Strings(user_sb)
println(strings.Join(user_sb, ""))
os.Exit(1)
} else if len(args) == 1 && args[0] == "config"{
create_author_file()
}
// if calling binary with nothing or only string
print("Usage: cocommit <commit message> <co-author1> [co-author2] [co-author3] || \ncocommit <commit message> <co-author1:email> [co-author2:email] [co-author3:email] || \ncocommit <commit message> all || \ncocommit <commit message> ^<co-author1> ^[co-author2] || \ncocommit <commit message> <group> || \ncocommit users || \nMixes of both")
@@ -214,6 +243,50 @@ func NoInput(args []string, users map[string]user) {
}
}
func create_author_file(param ...string) {
var input string
authors, err := os.UserConfigDir()
if err != nil {
println("Error: ", err)
os.Exit(1)
}
if len(param) > 0 {
input = "yes"
goto skip
}
println("This command will create a blank template auhtor file in the default location. \nDefault location: "+ authors + "\nConfirm by typing y|yes or cancel with n|no")
fmt.Scanln(&input)
if err != nil {
println("Error: ", err)
os.Exit(1)
}
skip:
if input == "y" || input == "yes" {
// create folder cocommit in .config
authors += "/cocommit"
err := os.MkdirAll(authors, 0755)
if err != nil {
println("Error in dir creation: ", err.Error())
os.Exit(1)
}
authors += "/authors"
file, err := os.Create(authors)
if err != nil {
println("Error: ", err.Error())
os.Exit(1)
}
defer file.Close()
file.WriteString("name_short|Name|Username|email (opt: |ex) (opt: ;;group1 or ;;group1|group2|group3...)\n")
println("File created successfully at: "+ authors)
os.Exit(1)
} else {
println("Cancelled")
os.Exit(1)
}
}
func check_err(e error) {
if e != nil {
fmt.Println(e.Error())