Skip to content

Commit

Permalink
Refactor 'ReadData' for better implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Jun 17, 2024
1 parent c71e798 commit fa1c5b6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 41 deletions.
92 changes: 55 additions & 37 deletions pkg/yamll/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ type Dependency struct {

// Auth holds the authentication information to resolve the remote yaml files.
type Auth struct {
UserName string `json:"user_name,omitempty" yaml:"user_name,omitempty" mapstructure:"user_name"`
Password string `json:"password,omitempty" yaml:"password,omitempty" mapstructure:"password"`
BarerToken string `json:"barer_token,omitempty" yaml:"barer_token,omitempty" mapstructure:"barer_token"`
CaContent string `json:"ca_content,omitempty" yaml:"ca_content,omitempty" mapstructure:"ca_content"`
UserName string `json:"user_name,omitempty" yaml:"user_name,omitempty"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
BarerToken string `json:"barer_token,omitempty" yaml:"barer_token,omitempty"`
CaContent string `json:"ca_content,omitempty" yaml:"ca_content,omitempty"`
SSHKey string `json:"ssh_key,omitempty" yaml:"ssh_key,omitempty"`
}

// ResolveDependencies addresses the dependencies of YAML imports specified in the YAML files.
Expand Down Expand Up @@ -88,6 +89,7 @@ func (cfg *Config) ResolveDependencies(routes map[string]*YamlData, dependencies
return routes, nil
}

// GetDependencyData reads the imports analyses it and generates Dependency data for it.
func (cfg *Config) GetDependencyData(dependency string) (*Dependency, error) {
imports := strings.Split(dependency, ";")
runeSlice := []rune(imports[0])
Expand All @@ -114,51 +116,67 @@ func (cfg *Config) GetDependencyData(dependency string) (*Dependency, error) {
return dependencyData, nil
}

// ReadData actually reads the data from the identified import.
func (dependency *Dependency) ReadData(log *slog.Logger) (string, error) {
log.Debug("dependency file type identification", slog.String("type", dependency.Type))

switch {
case dependency.Type == TypeURL:
httpClient := resty.New()
if len(dependency.Auth.BarerToken) != 0 {
httpClient.SetAuthToken(dependency.Auth.BarerToken)
}
return dependency.URL()
case dependency.Type == TypeGit:
return dependency.Git()
case dependency.Type == TypeFile:
return dependency.File()
default:
return "", &errors.YamllError{Message: fmt.Sprintf("reading data from of type '%s' is not supported", dependency.Type)}
}
}

if len(dependency.Auth.UserName) != 0 && len(dependency.Auth.Password) != 0 {
httpClient.SetBasicAuth(dependency.Auth.UserName, dependency.Auth.Password)
}
// Git reads the data from the Git import.
func (dependency *Dependency) Git() (string, error) {
return "", &errors.YamllError{Message: "does not support dependency type 'git' at the moment"}
}

if len(dependency.Auth.CaContent) != 0 {
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM([]byte(dependency.Auth.CaContent))
httpClient.SetTLSClientConfig(&tls.Config{RootCAs: certPool}) //nolint:gosec
} else {
httpClient.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) //nolint:gosec
}
// URL reads the data from the URL import.
func (dependency *Dependency) URL() (string, error) {
httpClient := resty.New()
if len(dependency.Auth.BarerToken) != 0 {
httpClient.SetAuthToken(dependency.Auth.BarerToken)
}

resp, err := httpClient.R().Get(dependency.Path)
if err != nil {
return "", err
}
if len(dependency.Auth.UserName) != 0 && len(dependency.Auth.Password) != 0 {
httpClient.SetBasicAuth(dependency.Auth.UserName, dependency.Auth.Password)
}

return resp.String(), err
case dependency.Type == TypeGit:
return "", &errors.YamllError{Message: "does not support dependency type 'git' at the moment"}
case dependency.Type == TypeFile:
absYamlFilePath, err := filepath.Abs(dependency.Path)
if err != nil {
return "", err
}
if len(dependency.Auth.CaContent) != 0 {
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM([]byte(dependency.Auth.CaContent))
httpClient.SetTLSClientConfig(&tls.Config{RootCAs: certPool}) //nolint:gosec
} else {
httpClient.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) //nolint:gosec
}

yamlFileData, err := os.ReadFile(absYamlFilePath)
if err != nil {
return "", &errors.YamllError{Message: fmt.Sprintf("reading YAML dependency errored with: '%v'", err)}
}
resp, err := httpClient.R().Get(dependency.Path)
if err != nil {
return "", err
}

return string(yamlFileData), nil
default:
return "", &errors.YamllError{Message: fmt.Sprintf("reading data from of type '%s' is not supported", dependency.Type)}
return resp.String(), err
}

// File reads the data from the File import.
func (dependency *Dependency) File() (string, error) {
absYamlFilePath, err := filepath.Abs(dependency.Path)
if err != nil {
return "", err
}

yamlFileData, err := os.ReadFile(absYamlFilePath)
if err != nil {
return "", &errors.YamllError{Message: fmt.Sprintf("reading YAML dependency errored with: '%v'", err)}
}

return string(yamlFileData), nil
}

func (dependency *Dependency) identifyType() {
Expand Down
7 changes: 3 additions & 4 deletions pkg/yamll/yamll.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import (

// YamlData holds information of yaml file and its dependency tree.
type YamlData struct {
Root bool `json:"root,omitempty" yaml:"root,omitempty"`
Imported bool `json:"imported,omitempty" yaml:"imported,omitempty"`
File string `json:"file,omitempty" yaml:"file,omitempty"`
/*Dependency []string `json:"dependency,omitempty" yaml:"dependency,omitempty"`*/
Root bool `json:"root,omitempty" yaml:"root,omitempty"`
Imported bool `json:"imported,omitempty" yaml:"imported,omitempty"`
File string `json:"file,omitempty" yaml:"file,omitempty"`
Dependency []*Dependency `json:"dependency,omitempty" yaml:"dependency,omitempty"`
DataRaw string `json:"data_raw,omitempty" yaml:"data_raw,omitempty"`
Data map[string]interface{} `json:"data,omitempty" yaml:"data,omitempty"`
Expand Down

0 comments on commit fa1c5b6

Please sign in to comment.