<commit_before>package auth

import (
	"bufio"
	"errors"
	"fmt"
	"log"
	"os"
	"strings"

	"github.com/darwinfroese/networkobserver/pkg/settings"
)

func init() {
	authDbLoc := settings.AuthenticationDBLocation + settings.AuthenticationDBName

	if _, err := os.Stat(authDbLoc); os.IsNotExist(err) {
		err := createFile(authDbLoc)

		if err != nil {
			log.Panicf("Error creating the file: %s\n%s", authDbLoc, err.Error())
		}

		return
	}

	records, err := readFile(authDbLoc)

	if err != nil {
		log.Panicf("Error reading users from file: %s\n%d Records were read in.", err.Error(), records)
	}
}

func readFile(fileLocation string) (int, error) {
	var records = 0
	file, err := os.Open(fileLocation)

	if err != nil {
		return records, err
	}

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := scanner.Text()
		parseLineToUser(line)
		records++
	}

	file.Close()
	return records, err
}

func createFile(fileLocation string) error {
	_, err := os.Create(fileLocation)

	return err
}

func parseLineToUser(line string) (bool, error) {
	data := strings.Split(line, ",")

	if len(data) != 3 {
		msg := fmt.Sprintf("Invalid database entry: %s", line)
		return false, errors.New(msg)
	}

	user := User{
		Username: data[0],
		Password: data[1],
		Salt:     data[2],
	}

	authDatabaseEntries = append(authDatabaseEntries, user)

	return true, nil
}

func writeUserToFile(username, password, salt string) (bool, error) {
	return false, nil
}

func updateUsernameInFile(username, newUsername string) (bool, error) {
	return false, nil
}

func updatePasswordInFile(username, password, salt string) (bool, error) {
	return false, nil
}<commit_msg>Update users and write user to file<commit_after>package auth

import (
	"bufio"
	"errors"
	"fmt"
	"log"
	"os"
	"strings"

	"github.com/darwinfroese/networkobserver/pkg/settings"
)

var authDbLoc string

func init() {
	authDbLoc = settings.AuthenticationDBLocation + settings.AuthenticationDBName

	if _, err := os.Stat(authDbLoc); os.IsNotExist(err) {
		err := createFile(authDbLoc)

		if err != nil {
			log.Panicf("Error creating the file: %s\n%s", authDbLoc, err.Error())
		}

		return
	}

	records, err := readFile(authDbLoc)

	if err != nil {
		log.Panicf("Error reading users from file: %s\n%d Records were read in.", err.Error(), records)
	}
}

func readFile(fileLocation string) (int, error) {
	var records = 0
	file, err := os.Open(fileLocation)

	if err != nil {
		return records, err
	}

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := scanner.Text()
		parseLineToUser(line)
		records++
	}

	file.Close()
	return records, err
}

func createFile(fileLocation string) error {
	_, err := os.Create(fileLocation)

	return err
}

func parseLineToUser(line string) (bool, error) {
	data := strings.Split(line, ",")

	if len(data) != 3 {
		msg := fmt.Sprintf("Invalid database entry: %s", line)
		return false, errors.New(msg)
	}

	user := User{
		Username: data[0],
		Password: data[1],
		Salt:     data[2],
	}

	authDatabaseEntries = append(authDatabaseEntries, user)

	return true, nil
}

func writeUserToFile(username, password, salt string) (bool, error) {
	line := username + "," + password + "," + salt
	file, err := os.Open(authDbLoc)

	if err != nil {
		log.Panicf("Couldn't open the database for writing.\n%s", err.Error())
	}

	_, err = file.Write([]byte(line))

	if err != nil {
		log.Panicf("Couldn't write the record to the database.\n%s", err.Error())
	}

	return true, nil
}

func updateUsername(username, newUsername string) bool {
	for i := range authDatabaseEntries {
		if authDatabaseEntries[i].Username == username {
			authDatabaseEntries[i].Username = newUsername
			return true
		}
	}

	return false
}

func updatePassword(username, password, salt string) bool {
	for i := range authDatabaseEntries {
		if authDatabaseEntries[i].Username == username {
			authDatabaseEntries[i].Password = password
			authDatabaseEntries[i].Salt = salt
			return true
		}
	}

	return false
}
{'fraction_non_alphanumeric': 0.13682092555331993, 'fraction_numerical': 0.0025150905432595573, 'mean_word_length': 4.105263157894737, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5169270', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1365, 'n_words': 357}