Skip to content

Commit

Permalink
Refactor processing of URL and File paths
Browse files Browse the repository at this point in the history
- This commit adds simplified functions to handle file processing of url
- Also handles both processing in separate functions

Signed-off-by: bupd <[email protected]>
  • Loading branch information
bupd committed Jul 1, 2024
1 parent 2978bc7 commit 6d1ebc1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 43 deletions.
6 changes: 3 additions & 3 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Wether to us the built-in Zot registry or not
bring_own_registry = true
bring_own_registry = false

# URL of own registry
own_registry_adr = "127.0.0.1:5000"

# URL of remote registry OR local file path
# url_or_file = "https://demo.goharbor.io/v2/myproject/album-server"
url_or_file = "http://localhost:5001/v2/library/busybox"
url_or_file = "https://demo.goharbor.io/v2/myproject/album-server"
# url_or_file = "http://localhost:5001/v2/library/busybox"

# For testing purposes :
# https://demo.goharbor.io/v2/myproject/album-server
Expand Down
102 changes: 62 additions & 40 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ import (
func main() {
err := run()
if err != nil {
fmt.Println(err)
os.Exit(1)
log.Fatalf("Error running satellite: %v", err)
}
}

Expand Down Expand Up @@ -83,14 +82,6 @@ func run() error {
if bringOwnRegistry {
registryAdr := viper.GetString("own_registry_adr")

// Validate registryAdr format
// matched, err := regexp.MatchString(`^127\.0\.0\.1:\d{1,5}$`, registryAdr)
// if err != nil {
// return fmt.Errorf("error validating registry address: %w", err)
// }
// if matched {
// return fmt.Errorf("invalid registry address format: %s", registryAdr)
// }
os.Setenv("ZOT_URL", registryAdr)
fmt.Println("Registry URL set to:", registryAdr)
} else {
Expand All @@ -108,42 +99,22 @@ func run() error {
}

input := viper.GetString("url_or_file")
// Attempt to parse the input as a URL
parsedURL, err := url.Parse(input)
// If parsing as URL fails or no scheme detected, treat it as a file path
if err != nil || parsedURL.Scheme == "" {
if strings.ContainsAny(input, "\\:*?\"<>|") {
fmt.Println("Path contains invalid characters. Please check the configuration.")
return fmt.Errorf("invalid file path")
}
dir, err := os.Getwd()
// Treat input as a file path
err = processFilePath(input, fetcher)
if err != nil {
fmt.Println("Error getting current directory:", err)
return err
}
absPath := filepath.Join(dir, input)
if _, err := os.Stat(absPath); os.IsNotExist(err) {
fmt.Println("No URL or file found. Please check the configuration.")
return fmt.Errorf("file not found")
log.Fatalf("Error in processing file path: %v", err)
}
fmt.Println("Input is a valid file path.")
fetcher = store.FileImageListFetcher(input)
os.Setenv("USER_INPUT", input)
} else {
fmt.Println("Input is a valid URL.")
// Process input as a URL
fetcher = store.RemoteImageListFetcher(input)
os.Setenv("USER_INPUT", input)
parts := strings.SplitN(input, "://", 2)
scheme := parts[0] + "://"
os.Setenv("SCHEME", scheme)
hostAndPath := parts[1]
hostParts := strings.Split(hostAndPath, "/")
host := hostParts[0]
os.Setenv("HOST", host)
apiVersion := hostParts[1]
os.Setenv("API_VERSION", apiVersion)
registry := hostParts[2]
os.Setenv("REGISTRY", registry)
repository := hostParts[3]
os.Setenv("REPOSITORY", repository)
err = processURL(input)
if err != nil {
log.Fatalf("Error in processing URL: %v", err)
}
}

err = godotenv.Load()
Expand All @@ -165,3 +136,54 @@ func run() error {
}
return nil
}

func processFilePath(input string, fetcher store.ImageFetcher) error {
// Check for invalid characters in file path
if strings.ContainsAny(input, "\\:*?\"<>|") {
fmt.Println("Path contains invalid characters. Please check the configuration.")
return fmt.Errorf("invalid file path")
}
dir, err := os.Getwd()
if err != nil {
fmt.Println("Error getting current directory:", err)
return err
}
absPath := filepath.Join(dir, input)
if _, err := os.Stat(absPath); os.IsNotExist(err) {
fmt.Println("No URL or file found. Please check the configuration.")
return fmt.Errorf("file not found")
}
fmt.Println("Input is a valid file path.")
fetcher = store.FileImageListFetcher(input)
os.Setenv("USER_INPUT", input)

return nil
}

func processURL(input string) error {
fmt.Println("Input is a valid URL.")

// Set environment variables
os.Setenv("USER_INPUT", input)

// Extract URL components
parts := strings.SplitN(input, "://", 2)
scheme := parts[0] + "://"
os.Setenv("SCHEME", scheme)

hostAndPath := parts[1]
hostParts := strings.Split(hostAndPath, "/")
host := hostParts[0]
os.Setenv("HOST", host)

apiVersion := hostParts[1]
os.Setenv("API_VERSION", apiVersion)

registry := hostParts[2]
os.Setenv("REGISTRY", registry)

repository := hostParts[3]
os.Setenv("REPOSITORY", repository)

return nil
}

0 comments on commit 6d1ebc1

Please sign in to comment.