diff --git a/src_code/go_src/cocommit.go b/src_code/go_src/cocommit.go index 8534842..01242b5 100644 --- a/src_code/go_src/cocommit.go +++ b/src_code/go_src/cocommit.go @@ -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 [co-author2] [co-author3] || \ncocommit [co-author2:email] [co-author3:email] || \ncocommit all || \ncocommit ^ ^[co-author2] || \ncocommit || \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())