go-KodeStarter/configFile/config-fileyml.go

216 lines
6.6 KiB
Go
Raw Permalink Normal View History

2023-06-25 12:54:50 -04:00
package configFile
2023-06-24 02:00:53 -04:00
import (
"fmt"
"io/ioutil"
"gopkg.in/yaml.v2"
)
2023-06-30 10:09:01 -04:00
type ConfigYml struct {
2023-06-24 02:00:53 -04:00
Sections map[string]SectionConfig `yaml:"category"`
}
type SectionConfig struct {
2023-07-01 00:49:45 -04:00
Softwares map[string]SoftwareConfig `yaml:"software"`
EnvironmentsVariables map[string]string `yaml:"environments variables"`
Directory map[string]DirectoryConfig `yaml:"directory"`
Pipelines map[string]PipelineConfig `yaml:"pipelines"`
InstallApps map[string]InstallAppsConfig `yaml:"install apps"`
2023-06-24 02:00:53 -04:00
}
type SoftwareConfig struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
Author string `yaml:"author"`
Location string `yaml:"location"`
Description string `yaml:"description"`
}
type DirectoryConfig struct {
Source struct {
Path []string `yaml:"path"`
} `yaml:"source"`
Test struct {
Path []string `yaml:"path"`
} `yaml:"test"`
Release struct {
Path []string `yaml:"path"`
} `yaml:"release"`
}
type PipelineConfig struct {
Build string `yaml:"build"`
Test string `yaml:"test"`
Lint string `yaml:"lint"`
2023-06-30 10:09:01 -04:00
}
2023-07-01 00:49:45 -04:00
type InstallAppsConfig struct {
Name string `yaml:"name"`
CmdWin string `yaml:"cmdWin"`
CmdLinux string `yaml:"cmdLinux"`
}
2023-06-30 10:09:01 -04:00
// function GetPathsFromCategory
func (c *ConfigYml) GetPathsFromCategory(sectionName string) ([]string, error) {
section, ok := c.Sections[sectionName]
if !ok {
return nil, fmt.Errorf("section '%s' not found", sectionName)
}
var paths []string
for _, EnvVar := range section.EnvironmentsVariables {
paths = append(paths, EnvVar)
}
return paths, nil
2023-06-24 02:00:53 -04:00
}
// The function loads a configuration file in YAML format and returns a pointer to a Config struct or
// an error.
2023-06-30 10:09:01 -04:00
func LoadConfig(filename string) (*ConfigYml, error) {
var config ConfigYml
2023-06-24 02:00:53 -04:00
source, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
// print source
2023-06-25 12:54:50 -04:00
// fmt.Println(string(source))
2023-06-24 02:00:53 -04:00
err = yaml.Unmarshal(source, &config)
if err != nil {
return nil, err
}
return &config, nil
}
2023-06-25 12:54:50 -04:00
// function GetListCategory return list category from config file
2023-06-30 10:09:01 -04:00
func (c *ConfigYml) GetListCategory() ([]string, error) {
2023-06-25 12:54:50 -04:00
var categories []string
for categoryName := range c.Sections {
categories = append(categories, categoryName)
}
return categories, nil
}
2023-06-24 02:00:53 -04:00
// This is a method defined on the `Config` struct that returns a list of software names for a given
// section. It takes in a `sectionName` string as a parameter and returns a slice of strings and an
// error.
2023-06-30 10:09:01 -04:00
func (c *ConfigYml) GetListSoftware(sectionName string) ([]string, error) {
2023-06-24 02:00:53 -04:00
section, ok := c.Sections[sectionName]
if !ok {
return nil, fmt.Errorf("section '%s' not found", sectionName)
}
var softwares []string
for softwareName := range section.Softwares {
softwares = append(softwares, softwareName)
}
return softwares, nil
}
// This is a method defined on the `Config` struct that takes in two string parameters `sectionName`
// and `SoftwareName`. It returns a pointer to a `SoftwareConfig` struct and an error.
2023-06-30 10:09:01 -04:00
func (c *ConfigYml) GetSoftware(sectionName, SoftwareName string) (*SoftwareConfig, error) {
2023-06-24 02:00:53 -04:00
section, ok := c.Sections[sectionName]
if !ok {
return nil, fmt.Errorf("section '%s' not found", sectionName)
}
software, ok := section.Softwares[SoftwareName]
if !ok {
return nil, fmt.Errorf("logiciel '%s' not found in section '%s'", SoftwareName, sectionName)
}
return &software, nil
}
// get list environment variables
2023-06-30 10:09:01 -04:00
func (c *ConfigYml) GetListEnvironmentVariables(sectionName string) ([]string, error) {
2023-06-24 02:00:53 -04:00
section, ok := c.Sections[sectionName]
if !ok {
return nil, fmt.Errorf("section '%s' not found", sectionName)
}
var environmentVariables []string
for environmentVariableName := range section.EnvironmentsVariables {
environmentVariables = append(environmentVariables, environmentVariableName)
}
return environmentVariables, nil
}
// This is a method defined on the `Config` struct that takes in two string parameters `sectionName`
// and `variableName`. It returns a string value and an error. The method looks for the specified
// section in the configuration and then looks for the specified environment variable within that
// section. If the section or variable is not found, an error is returned. If the variable is found,
// its value is returned.
2023-06-30 10:09:01 -04:00
func (c *ConfigYml) GetEnvironmentVariables(sectionName, variableName string) (string, error) {
2023-06-24 02:00:53 -04:00
section, ok := c.Sections[sectionName]
if !ok {
return "", fmt.Errorf("section '%s' not found", sectionName)
}
variable, ok := section.EnvironmentsVariables[variableName]
if !ok {
return "", fmt.Errorf("variable '%s' not found in section '%s'", variableName, sectionName)
}
return variable, nil
}
2023-06-30 10:09:01 -04:00
// get all environment variables names and values
func (c *ConfigYml) GetAllEnvironmentVariables(sectionName string) (map[string]string, error) {
section, ok := c.Sections[sectionName]
if !ok {
return nil, fmt.Errorf("section '%s' not found", sectionName)
}
return section.EnvironmentsVariables, nil
}
2023-06-24 02:00:53 -04:00
// This is a method defined on the `Config` struct that takes in two string parameters `sectionName`
// and `directoryName`. It returns a pointer to a `DirectoryConfig` struct and an error. The method
// looks for the specified section in the configuration and then looks for the specified directory
// within that section. If the section or directory is not found, an error is returned. If the
// directory is found, its configuration is returned.
2023-06-30 10:09:01 -04:00
func (c *ConfigYml) GetDirectory(sectionName, directoryName string) (*DirectoryConfig, error) {
2023-06-24 02:00:53 -04:00
section, ok := c.Sections[sectionName]
if !ok {
return nil, fmt.Errorf("section '%s' not found", sectionName)
}
directory, ok := section.Directory[directoryName]
if !ok {
return nil, fmt.Errorf("directory '%s' not found in section '%s'", directoryName, sectionName)
}
return &directory, nil
}
// This is a method defined on the `Config` struct that takes in two string parameters `sectionName`
// and `pipelineName`. It returns a pointer to a `PipelineConfig` struct and an error. The method looks
// for the specified section in the configuration and then looks for the specified pipeline within that
// section. If the section or pipeline is not found, an error is returned. If the pipeline is found,
// its configuration is returned.
2023-06-30 10:09:01 -04:00
func (c *ConfigYml) GetPipeline(sectionName, pipelineName string) (*PipelineConfig, error) {
2023-06-24 02:00:53 -04:00
section, ok := c.Sections[sectionName]
if !ok {
return nil, fmt.Errorf("section '%s' not found", sectionName)
}
pipeline, ok := section.Pipelines[pipelineName]
if !ok {
return nil, fmt.Errorf("pipeline '%s' not found in section '%s'", pipelineName, sectionName)
}
return &pipeline, nil
}