commit d0997d928279664ec708b8b01765f60a19c62f4d Author: Kris Crawford Date: Fri Jun 6 07:03:50 2025 -0400 Initial save diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/add.go b/add.go new file mode 100644 index 0000000..cb9d968 --- /dev/null +++ b/add.go @@ -0,0 +1,15 @@ +package main + +import "fmt" + +func add() { + // Prompt the user for the name and phone number of the new contact + var fname, lname, phone string + fmt.Printf("Enter the first name of the new contact: ") + fmt.Scanln(&fname) + fmt.Printf("Enter the last name of the new contact: ") + fmt.Scanln(&lname) + fmt.Printf("Enter the phone number: ") + fmt.Scanln(&phone) + fmt.Printf("You entered: Name: %s, Phone: %s\n", fname, phone) +} diff --git a/address.db b/address.db new file mode 100644 index 0000000..55a2631 Binary files /dev/null and b/address.db differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f8c7c20 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module addressBook + +go 1.24.0 + +require ( + github.com/mattn/go-sqlite3 v1.14.28 + github.com/sirupsen/logrus v1.9.3 +) + +require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..03dbc66 --- /dev/null +++ b/go.sum @@ -0,0 +1,13 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A= +github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..6ce93cd --- /dev/null +++ b/main.go @@ -0,0 +1,61 @@ +package main + +import ( + "addressBook/utils" + "bufio" + "fmt" + "os" + "strings" + + "github.com/sirupsen/logrus" +) + +func init() { + // Create a new logger + log := logrus.New() + log.SetFormatter(&logrus.TextFormatter{}) + +} + +func main() { + // Initialize the database + utils.CreateDatabase() + fmt.Println("Welcome to your address book") + fmt.Println("---------------------") + fmt.Println("a - Add a new contact") + fmt.Println("d - Delete a contact") + fmt.Println("p - Print all contacts") + fmt.Println("q - Quit") + fmt.Println("---------------------") + + reader := bufio.NewReader(os.Stdin) + line, err := reader.ReadString('\n') + line = strings.TrimSpace(line) + if err != nil { + fmt.Errorf("Error reading input: %v", err) + } + + // fmt.Printf("%q", line) + + if len(line) != 1 { + fmt.Println("Invalid input. Please enter a single character.") + main() + } + + switch line[0] { + case 'a': + // Add a new contact + add() + case 'd': + // Delete a contact + case 'p': + // Print all contacts + case 'q': + fmt.Println("Quitting the address book.") + os.Exit(0) + default: + fmt.Println("Invalid option. Please try again.") + main() + } + +} diff --git a/utils/sql.go b/utils/sql.go new file mode 100644 index 0000000..6a26472 --- /dev/null +++ b/utils/sql.go @@ -0,0 +1,35 @@ +package utils + +import ( + "database/sql" + "os" + "time" + + _ "github.com/mattn/go-sqlite3" +) + +func CreateDatabase() { + // Create a new SQLite database if it doesn't exist + fileStartTime := time.Now() + os.Create("address.db") + db, err := sql.Open("sqlite3", "address.db") + if err != nil { + panic(err) + } + fileDuration := time.Since(fileStartTime) + log.Info("Database file created in ", fileDuration) + + // Create the contacts table if it doesn't exist + createTableQuery := `CREATE TABLE IF NOT EXISTS contacts ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + fname TEXT NOT NULL, + lname TEXT NOT NULL, + phone TEXT NOT NULL + );` + + _, err = db.Exec(createTableQuery) + if err != nil { + panic(err) + } + defer db.Close() +}