Skip to content

Commit

Permalink
adding utils for simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehul-Kumar-27 committed Sep 13, 2024
1 parent 3807142 commit ce6a19e
Show file tree
Hide file tree
Showing 4 changed files with 353 additions and 148 deletions.
122 changes: 110 additions & 12 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,102 @@ import (
"github.com/spf13/viper"
)

var AppConfig *Config

type Config struct {
LogLevel string
OwnRegistry bool
OwnRegistryAdr string
OwnRegistryPort string
ZotConfigPath string
Input string
logLevel string
ownRegistry bool
ownRegistryAdr string
ownRegistryPort string
zotConfigPath string
input string
zot_url string
registry string
repository string
user_input string
scheme string
api_version string
image string
}

func GetLogLevel() string {
return AppConfig.logLevel
}

func GetOwnRegistry() bool {
return AppConfig.ownRegistry
}

func GetOwnRegistryAdr() string {
return AppConfig.ownRegistryAdr
}

func GetOwnRegistryPort() string {
return AppConfig.ownRegistryPort
}

func GetZotConfigPath() string {
return AppConfig.zotConfigPath
}

func GetInput() string {
return AppConfig.input
}

func SetZotURL(url string) {
AppConfig.zot_url = url
}

func GetZotURL() string {
return AppConfig.zot_url
}

func SetRegistry(registry string) {
AppConfig.registry = registry
}

func GetRegistry() string {
return AppConfig.registry
}

func SetRepository(repository string) {
AppConfig.repository = repository
}

func GetRepository() string {
return AppConfig.repository
}

func SetUserInput(user_input string) {
AppConfig.user_input = user_input
}

func GetUserInput() string {
return AppConfig.user_input
}

func SetScheme(scheme string) {
AppConfig.scheme = scheme
}

func GetScheme() string {
return AppConfig.scheme
}

func SetAPIVersion(api_version string) {
AppConfig.api_version = api_version
}

func GetAPIVersion() string {
return AppConfig.api_version
}

func SetImage(image string) {
AppConfig.image = image
}

func GetImage() string {
return AppConfig.image
}

func LoadConfig() (*Config, error) {
Expand All @@ -24,11 +113,20 @@ func LoadConfig() (*Config, error) {
}

return &Config{
LogLevel: viper.GetString("log_level"),
OwnRegistry: viper.GetBool("bring_own_registry"),
OwnRegistryAdr: viper.GetString("own_registry_adr"),
OwnRegistryPort: viper.GetString("own_registry_port"),
ZotConfigPath: viper.GetString("zotConfigPath"),
Input: viper.GetString("url_or_file"),
logLevel: viper.GetString("log_level"),
ownRegistry: viper.GetBool("bring_own_registry"),
ownRegistryAdr: viper.GetString("own_registry_adr"),
ownRegistryPort: viper.GetString("own_registry_port"),
zotConfigPath: viper.GetString("zotConfigPath"),
input: viper.GetString("url_or_file"),
}, nil
}

func InitConfig() error {
var err error
AppConfig, err = LoadConfig()
if err != nil {
return err
}
return nil
}
11 changes: 11 additions & 0 deletions internal/images/get-images.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ import (
"time"
)

type ImageList struct {
RegistryURL string `json:"registryUrl"`
Repositories []struct {
Repository string `json:"repository"`
Images []struct {
Name string `json:"name"`
} `json:"images"`
} `json:"repositories"`
}


type Image struct {
ID int `json:"ID"`
Registry string `json:"Registry"`
Expand Down
131 changes: 131 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package utils

import (
"encoding/json"
"errors"
"fmt"
"net"
"net/url"
"os"
"path/filepath"
"strings"

"container-registry.com/harbor-satellite/internal/config"
"container-registry.com/harbor-satellite/internal/images"
"container-registry.com/harbor-satellite/registry"
)

// / ValidateRegistryAddress validates the registry address and port and returns the URL
func ValidateRegistryAddress(registryAdr, registryPort string) (string, error) {
ip := net.ParseIP(registryAdr)
if ip == nil {
return "", errors.New("invalid IP address")
}
if ip.To4() != nil {
} else {
return "", errors.New("IP address is IPv6 format and unsupported")
}

return fmt.Sprintf("%s:%s", registryAdr, registryPort), nil
}

// / HandleOwnRegistry handles the own registry address and port and sets the Zot URL
func HandleOwnRegistry() error {
zotURL, err := ValidateRegistryAddress(config.GetOwnRegistryAdr(), config.GetOwnRegistryPort())
if err != nil {
return err
}
config.SetZotURL(zotURL)
return nil
}

// LaunchDefaultZotRegistry launches the default Zot registry using the Zot config path
func LaunchDefaultZotRegistry() error {
launch, err := registry.LaunchRegistry(config.GetZotConfigPath())
if !launch {
return fmt.Errorf("error launching registry: %w", err)
}
if err != nil {
return fmt.Errorf("error launching registry: %w", err)
}
return nil
}

// Helper function to determine if input is a valid URL
func IsValidURL(input string) bool {
parsedURL, err := url.Parse(input)
return err == nil && parsedURL.Scheme != ""
}

// GetAbsFilePath gets the absolute file path of the input file path and checks if it exists
func GetAbsFilePath(input string) error {
dir, err := os.Getwd()
if err != nil {
return err
}
absPath := filepath.Join(dir, input)
if _, err := os.Stat(absPath); os.IsNotExist(err) {
return err
}
return nil
}

// Check if path contains invalid characters
func HasInvalidPathChars(input string) bool {
return strings.ContainsAny(input, "\\:*?\"<>|")
}

// ParseImagesJsonFile parses the images.json file and decodes it into the ImageList struct
func ParseImagesJsonFile(absPath string, imagesList *images.ImageList) error {
file, err := os.Open(absPath)
if err != nil {
return err
}
defer file.Close()

if err := json.NewDecoder(file).Decode(imagesList); err != nil {
return err
}
return nil
}

// Set registry environment variables
func SetRegistryEnvVars(imageList images.ImageList) error {
registryURL := imageList.RegistryURL
registryParts := strings.Split(registryURL, "/")
if len(registryParts) < 3 {
return fmt.Errorf("invalid registryUrl format in images.json")
}

os.Setenv("REGISTRY", registryParts[2])
config.SetRegistry(registryParts[2])

if len(imageList.Repositories) > 0 {
os.Setenv("REPOSITORY", imageList.Repositories[0].Repository)
config.SetRepository(imageList.Repositories[0].Repository)
} else {
return fmt.Errorf("no repositories found in images.json")
}

return nil
}

// SetUrlConfig sets the URL configuration for the input URL and sets the environment variables
func SetUrlConfig(input string) {
os.Setenv("USER_INPUT", input)
config.SetUserInput(input)
parts := strings.SplitN(input, "://", 2)
scheme := parts[0] + "://"
os.Setenv("SCHEME", scheme)
config.SetScheme(scheme)
registryAndPath := parts[1]
registryParts := strings.Split(registryAndPath, "/")
os.Setenv("REGISTRY", registryParts[0])
config.SetRegistry(registryParts[0])
os.Setenv("API_VERSION", registryParts[1])
config.SetAPIVersion(registryParts[1])
os.Setenv("REPOSITORY", registryParts[2])
config.SetRepository(registryParts[2])
os.Setenv("IMAGE", registryParts[3])
config.SetImage(registryParts[3])
}
Loading

0 comments on commit ce6a19e

Please sign in to comment.