-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
176 lines (141 loc) · 4.01 KB
/
main.go
File metadata and controls
176 lines (141 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/imsumedhaa/In-memory-database/api"
"github.com/imsumedhaa/In-memory-database/database"
"github.com/imsumedhaa/In-memory-database/filesystem"
"github.com/imsumedhaa/In-memory-database/inmemory"
"github.com/imsumedhaa/In-memory-database/postgres"
"github.com/joho/godotenv"
)
func init() {
// load .env file
if err := godotenv.Load(); err != nil {
log.Println("No env file found")
}
}
var (
name string
)
func main() {
host := os.Getenv("DB_HOST")
port := os.Getenv("DB_PORT")
username := os.Getenv("DB_USER")
password := os.Getenv("DB_PASSWORD")
dbname := os.Getenv("DB_NAME")
if host == "" || port == "" || username == "" || password == "" || dbname == "" {
log.Fatal("Missing one or more required environment variables")
}
if len(os.Args) < 2 {
fmt.Println("Expected subcommand 'filesystem' or 'inmemory' or 'postgres'")
os.Exit(1)
}
cmd := os.Args[1]
flags := flag.NewFlagSet(cmd, flag.ExitOnError)
flags.StringVar(&name, "name", "database.json", "this is the name")
flags.Parse(os.Args[2:]) // Parse args after the subcommand
var operation database.Database = nil
var err error
switch cmd {
case "filesystem":
operation, err = filesystem.NewFileSystem(name)
if err != nil {
fmt.Printf("Error creating filesystem: %v\n", err)
os.Exit(1)
}
case "inmemory":
operation, err = inmemory.NewInmemory()
if err != nil {
fmt.Printf("Error creating the map: %v", err)
os.Exit(1)
}
case "postgres":
operation, err = postgres.NewPostgres(host,port, username, password, dbname)
if err != nil {
fmt.Printf("Error creating the connection: %v\n", err)
os.Exit(1)
}
case "server":
httpConfig, err := api.NewHttp(host,port, username, password, dbname)
if err != nil {
fmt.Printf("Error creating the http connection: %v\n", err)
os.Exit(1)
}
if err := httpConfig.Run(); err != nil {
fmt.Printf("Error run http server: %v\n", err)
os.Exit(1)
}
default:
fmt.Println("Wrong Command. Should be either 'filesystem' or 'inmemory' or 'postgres'")
os.Exit(1)
}
reader := bufio.NewReader(os.Stdin)
for {
fmt.Println("Enter subcommand: create,update,get,delete,show & exit to quit the program")
input, _ := reader.ReadString('\n') //to read the input from user and store into input var
command := strings.TrimSpace(input) //delete the "\n" from input var and store it in command var
switch command {
case "create":
fmt.Println("Enter the key:")
key, _ := reader.ReadString('\n')
key = strings.TrimSpace(key)
fmt.Println("Enter the value:")
value, _ := reader.ReadString('\n')
value = strings.TrimSpace(value)
err := operation.Create(key, value)
if err != nil {
fmt.Printf("Error while creating the map: %v\n", err)
fmt.Println("try using another key...")
}
case "get":
fmt.Println("Enter the key:")
key, _ := reader.ReadString('\n')
key = strings.TrimSpace(key)
err := operation.Get(key)
if err != nil {
fmt.Printf("Error while getting the value: %v\n", err)
os.Exit(1)
}
case "update":
fmt.Println("Enter the key:")
key, _ := reader.ReadString('\n')
key = strings.TrimSpace(key)
fmt.Println("Enter the value:")
value, _ := reader.ReadString('\n')
value = strings.TrimSpace(value)
err := operation.Update(key, value)
if err != nil {
fmt.Printf("Error while updating the value: %v\n", err)
os.Exit(1)
}
case "delete":
fmt.Println("Enter the key you want to delete:")
key, _ := reader.ReadString('\n')
key = strings.TrimSpace(key)
err := operation.Delete(key)
if err != nil {
fmt.Printf("Error while Deleteing the pair: %v\n", err)
os.Exit(1)
}
case "show":
err := operation.Show()
if err != nil {
fmt.Printf("Error while showing the map: %v\n", err)
os.Exit(1)
}
case "exit":
err := operation.Exit()
if err != nil {
fmt.Printf("Error while exiting the program: %v\n", err)
os.Exit(1)
}
default:
fmt.Println("Wrong Command.")
}
}
}