Skip to content

Commit

Permalink
add utility to extract domain
Browse files Browse the repository at this point in the history
Signed-off-by: MUzairS15 <muzair.shaikh810@gmail.com>
  • Loading branch information
MUzairS15 committed Feb 26, 2024
1 parent 3a07008 commit 979a8c2
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
mathrand "math/rand"
"net/http"
"net/url"
"os"
"os/user"
"path/filepath"
Expand Down Expand Up @@ -357,7 +358,7 @@ func WriteJSONToFile[K any](outputPath string, data K) error {
return nil
}

func CreateDirectory(path string) error{
func CreateDirectory(path string) error {
err := os.MkdirAll(path, 0755)
if err != nil {
err = ErrCreateDir(err, path)
Expand All @@ -370,3 +371,12 @@ func CreateDirectory(path string) error{
func ReplaceSpacesAndConvertToLowercase(s string) string {
return strings.ToLower(strings.ReplaceAll(s, " ", ""))
}

func ExtractDomainFromURL(location string) string {
parsedURL, err := url.Parse(location)
// If unable to extract domain return the location as is.
if err != nil {
return location
}
return regexp.MustCompile(`(([a-zA-Z0-9]+\.)([a-zA-Z0-9]+))$`).FindString(parsedURL.Hostname())
}

0 comments on commit 979a8c2

Please sign in to comment.