Initial save
This commit is contained in:
commit
d0997d9282
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
.DS_Store
|
15
add.go
Normal file
15
add.go
Normal file
@ -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)
|
||||
}
|
BIN
address.db
Normal file
BIN
address.db
Normal file
Binary file not shown.
10
go.mod
Normal file
10
go.mod
Normal file
@ -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
|
13
go.sum
Normal file
13
go.sum
Normal file
@ -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=
|
61
main.go
Normal file
61
main.go
Normal file
@ -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()
|
||||
}
|
||||
|
||||
}
|
35
utils/sql.go
Normal file
35
utils/sql.go
Normal file
@ -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()
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user