added helpful comments

This commit is contained in:
John O'Keefe 2023-12-08 20:00:21 -05:00
parent 5a0aa7bcdb
commit 60efc1ca64

15
main.go
View File

@ -12,16 +12,20 @@ import (
)
func main() {
// Set Welcome Header
pterm.DefaultHeader.WithBackgroundStyle(pterm.NewStyle(pterm.BgCyan)).WithTextStyle(pterm.NewStyle(pterm.FgCyan)).Println("Welcome to Checkdigit Calculator")
// Check what directory the commandline is on when the program is launched.
directory, err := os.Getwd()
Error(err)
// Get name of file to be processed.
pterm.Printf(pterm.Cyan("What is the name of the file in this directory?: "))
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
Error(err)
// Create upcs array and push all upc strings into it
input = strings.TrimSpace(input)
file, err := os.Open(input)
Error(err)
@ -36,12 +40,13 @@ func main() {
fmt.Println(scanner.Err())
}
// Start PTerm Spinner
pterm.DefaultSpinner.Start()
Error(err)
// Create gtins array that will eventually be returned
var gtins []string
// Using regex compile so the program stays fast
regex, err := regexp.Compile("[^0-9]")
Error(err)
@ -119,23 +124,29 @@ func main() {
gtins = append(gtins, upc+remainderString)
}
// Check if answer.txt already exists and if so, delete it
if _, err := os.Stat(directory + "/answer.txt"); err == nil {
e := os.Remove(directory + "/answer.txt")
Error(e)
}
// Create new answer.txt
answerFile, err := os.Create(directory + "/answer.txt")
Error(err)
defer answerFile.Close()
// Fill the answers.txt file
for _, gtin := range gtins {
_, err := answerFile.WriteString(gtin + "\n")
Error(err)
}
// Stop the Spinner
pterm.DefaultSpinner.Stop()
// Print Success message and exit
pterm.Printf("\n")
pterm.Success.Println("The answer.txt file has been successfully generated with check digits.")
os.Exit(0)
}