-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (45 loc) · 1.17 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"log"
"os"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: url-shortener [shorten|resolve] [options]")
return
}
store, err := NewURLStore()
if err != nil {
log.Fatalf("Failed to initialize store: %v", err)
}
command := os.Args[1]
switch command {
case "shorten":
shortenCmd := flag.NewFlagSet("shorten", flag.ExitOnError)
url := shortenCmd.String("url", "", "URL to shorten")
shortenCmd.Parse(os.Args[2:])
if *url == "" {
fmt.Println("URL is required")
return
}
shortUrl := GenerateShortUrl()
store.SaveUrl(shortUrl, *url)
fmt.Printf("Shortened URL: http://localhost:8080/%s\n", shortUrl)
case "resolve":
resolveCmd := flag.NewFlagSet("resolve", flag.ExitOnError)
shortUrl := resolveCmd.String("short", "", "Short URL to resolve")
resolveCmd.Parse(os.Args[2:])
if *shortUrl == "" {
fmt.Println("Short URL is required") // Print an error message if no short URL is provided.
return
}
longUrl, exists := store.GetUrl(*shortUrl)
if !exists {
fmt.Println("Short URL not found")
return
}
fmt.Printf("Original URL: %s\n", longUrl)
}
}