add yml file config

This commit is contained in:
bruno 2023-06-24 02:00:53 -04:00
parent 23e372d025
commit 73855be6f4
11 changed files with 399 additions and 50 deletions

61
Taskfile.yml Normal file
View File

@ -0,0 +1,61 @@
# https://taskfile.dev
version: '3'
# Variables
vars:
## Commands Go ##
GOCMD: go
GOBUILD: "{{.GOCMD}} build -o"
GORUN: "{{.GOCMD}} run"
GOTEST: "{{.GOCMD}} test -v"
GOCOVER: "{{.GOCMD}} test -v --cover"
WINDOWS_BINARY_NAME: ./release/windows/kode-starter.exe
LINUX_BINARY_NAME: ./release/linux/kode-starter
# env:
GOOS: Linux
GOARCH: amd64
tasks:
###### LINUX ######
## Make sure you have PATH the versionManager !!!
linux-version:
cmds:
- "cd ./version/ && versionManager go build"
silent: true
## Build ##
linux-build:
cmds:
- "{{.GOBUILD}} {{.LINUX_BINARY_NAME}} -v"
# silent: true
## change Build version and build
linux-vbuild:
cmds:
- task: linux-version
- task: linux-build
###### WINDOWS ######
## Make sure you have PATH the versionManager.exe !!!
windows-version:
cmds:
- "cd ./version/ && versionManager.exe go build"
silent: true
windows-build:
cmds:
- "{{.GOBUILD}} {{.WINDOWS_BINARY_NAME}} -v"
silent: true
## change Build version and build
windows-vbuild:
cmds:
- task: windows-version
- task: windows-build

View File

@ -18,9 +18,9 @@ var EnvCmd = &cobra.Command{
config.SetInformations(cmd, args) config.SetInformations(cmd, args)
// if option --env or -e is set then print environment variables // if option --env or -e is set then print environment variables
if envVariablesFlag { // if envVariablesFlag {
printEnvVariables() // printEnvVariables()
} // }
}, },
} }

View File

@ -1,3 +1,94 @@
version: "3" # All the configuration elements listed below must be in lowercase
# category, software, variables environnement, directory, pipelines,name,
# version, author, location, description, path
category: category:
Go:
software:
Go Lang:
name: "Go Lang"
version: "1.17.5"
author: "Google"
location: 'C:\Users\bruno\scoop\apps\go\current'
description: "Go is an open source programming language that makes it easy to build simple, reliable, and efficient software."
Tasks:
name: "Tasks"
version: "0.2.0"
author: "Google"
location: 'C:\Users\bruno\scoop\apps\tasks\current'
description: 'Task is a task runner / build tool that aims to be simpler and easier to use than, for example, GNU Make.'
environments variables:
PATH: 'C:\Users\bruno\go\bin'
PATH2: 'C:\Users\bruno\scoop\apps\tasks\current\bin'
directory:
source:
path:
- src/config
- src/version
test:
path:
- test
release:
path:
- release
- release/windows
- release/linux
pipelines:
# build: "go build"
# test: "go test"
# lint: "golint"
Java:
software:
Quarkus:
name: "Quarkus"
version: "2.0.0"
author: "Red Hat"
location: 'C:\Users\bruno\scoop\apps\quarkus\current'
description: "Quarkus is a full-stack, Kubernetes-native Java framework made for Java virtual machines (JVMs) and native compilation, optimizing Java specifically for containers and enabling it to become an effective platform for serverless, cloud, and Kubernetes environments."
Maven:
name: "Maven"
version: "3.8.3"
author: "Apache"
location: 'C:\Users\bruno\scoop\apps\maven\current'
description: "Maven is a build automation tool used primarily for Java projects. Maven addresses two aspects of building software: first, it describes how software is built, and second, it describes its dependencies."
Ant:
name: "Ant"
version: "1.10.11"
author: "Apache"
location: 'C:\Users\bruno\scoop\apps\ant\current'
description: "Apache Ant is a Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications."
environments variables:
JAVA_HOME: 'C:\Users\bruno\scoop\apps\graalvm-jdk17\current'
ANT_HOME: 'C:\Users\bruno\scoop\apps\ant\current'
directory:
source:
path:
- src/main/java
test:
path:
- src/test/java
pipelines:
# compile: mvn compile
# test: mvn test
# package: mvn package
Mixe:
software:
Visual Studio Code:
name: "Visual Studio Code"
version: "1.59.1"
author: "Microsoft Corporation"
location: 'C:\Users\bruno\scoop\apps\vscode\current'
description: "Visual Studio Code is a streamlined code editor with support for development operations like debugging, task running, and version control."
variables environnement: {}
extensions:
- esbenp.prettier-vscode
- editorconfig.editorconfig

179
config/config-fileyml.go Normal file
View File

@ -0,0 +1,179 @@
package config
import (
"fmt"
"io/ioutil"
"gopkg.in/yaml.v2"
)
// The above code defines a configuration struct for software projects with sections for software,
// environment variables, directories, and pipeline configurations.
// @property Sections - A map of SectionConfig objects, where each key is a string representing the
// name of the section.
type Config struct {
Sections map[string]SectionConfig `yaml:"category"`
}
type SectionConfig struct {
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"`
}
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"`
// Compile string `yaml:"compile"`
// Package string `yaml:"package"`
}
// The function loads a configuration file in YAML format and returns a pointer to a Config struct or
// an error.
func LoadConfig(filename string) (*Config, error) {
var config Config
source, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
// print source
fmt.Println(string(source))
err = yaml.Unmarshal(source, &config)
if err != nil {
return nil, err
}
return &config, nil
}
// 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.
func (c *Config) GetListSoftware(sectionName string) ([]string, error) {
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.
func (c *Config) GetSoftware(sectionName, SoftwareName string) (*SoftwareConfig, error) {
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
func (c *Config) GetListEnvironmentVariables(sectionName string) ([]string, error) {
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.
func (c *Config) GetEnvironmentVariables(sectionName, variableName string) (string, error) {
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
}
// 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.
func (c *Config) GetDirectory(sectionName, directoryName string) (*DirectoryConfig, error) {
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.
func (c *Config) GetPipeline(sectionName, pipelineName string) (*PipelineConfig, error) {
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
}

3
go.mod
View File

@ -1,6 +1,6 @@
module kode-starter module kode-starter
go 1.20 go 1.19
require ( require (
github.com/gookit/color v1.5.3 github.com/gookit/color v1.5.3
@ -13,4 +13,5 @@ require (
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
golang.org/x/sys v0.6.0 // indirect golang.org/x/sys v0.6.0 // indirect
gopkg.in/yaml.v2 v2.4.0
) )

2
go.sum
View File

@ -18,5 +18,7 @@ github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1z
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

BIN
release/windows/kode-starter.exe → kode-starter Normal file → Executable file

Binary file not shown.

View File

@ -1,12 +1,15 @@
package main package main
import ( import (
"fmt"
"kode-starter/cmdCreate" "kode-starter/cmdCreate"
"kode-starter/cmdEnv" "kode-starter/cmdEnv"
"kode-starter/cmdLists" "kode-starter/cmdLists"
"kode-starter/config" "kode-starter/config"
"kode-starter/utils" "kode-starter/utils"
"kode-starter/version" "kode-starter/version"
"log"
"os"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -58,6 +61,7 @@ func mainProgram(cmd *cobra.Command, args []string) {
config.SetInformations(cmd, args) config.SetInformations(cmd, args)
fmt.Println("Header")
utils.PrintHeader() utils.PrintHeader()
// Check if the help flag is set // Check if the help flag is set
@ -76,6 +80,60 @@ func mainProgram(cmd *cobra.Command, args []string) {
func main() { func main() {
filename := "config.yml"
cfg, err := config.LoadConfig(filename)
if err != nil {
log.Fatalf("error loading config file: %v", err)
}
// list all elements from category "Category-Go"
ListSoftwares, err := cfg.GetListSoftware("Go")
if err != nil {
log.Fatalf("error getting logiciels: %v", err)
}
for _, s := range ListSoftwares {
fmt.Printf("Software: %s\n", s)
Software, err := cfg.GetSoftware("Go", s)
if err != nil {
log.Fatalf("error getting logiciel: %v", err)
}
fmt.Printf("Nom: %s\n", Software.Name)
fmt.Printf("Version: %s\n", Software.Version)
fmt.Printf("Auteur: %s\n", Software.Author)
fmt.Printf("Location: %s\n", Software.Location)
fmt.Printf("Description: %s\n", Software.Description)
}
EnvironmentVariables, err := cfg.GetListEnvironmentVariables("Go")
if err != nil {
log.Fatalf("error getting environment variables: %v", err)
}
for _, e := range EnvironmentVariables {
fmt.Printf("Environment variable: %s\n", e)
EnvVar, err := cfg.GetEnvironmentVariables("Go", e)
if err != nil {
log.Fatalf("error getting environment variable: %v", err)
}
fmt.Printf("%s = %s\n", e, EnvVar)
}
// liste configuration elements for "directory"
// Dir, err := cfg.GetDirectory("Category-Go", "source")
// if err != nil {
// log.Fatalf("error getting directory: %v", err)
// }
// for _, d := range Dir.Path {
// fmt.Printf("Directory: %s\n", d)
// }
os.Exit(0)
rootCmd.Execute() rootCmd.Execute()
} }

View File

@ -1,43 +0,0 @@
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
# Binary name
WINDOWS_BINARY_NAME=./release/windows/kode-starter.exe
LINUX_BINARY_NAME=./release/linux/kode-starter
# test, build, clean, release
all: test build
# build
build-windows:
cd ./version/ && versionManager.exe go build
cd ..
set GOOS=windows
set GOARCH=amd64
$(GOBUILD) -o $(WINDOWS_BINARY_NAME) -v
build-linux:
cd ./version/ && versionManager.exe go build
cd ..
set GOOS=linux
set GOARCH=amd64
$(GOBUILD) -o $(LINUX_BINARY_NAME) -v
build-all: build-windows build-linux
test:
$(GOTEST) -v ./...
clean:
$(GOCLEAN)
powershell.exe -c "rm .\release\ -Recurse -Force"
release: clean
cd ./version/ && versionManager.exe go release
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GOBUILD) -o $(WINDOWS_BINARY_NAME) -v
.PHONY: all build test clean release

BIN
release/linux/kode-starter Normal file → Executable file

Binary file not shown.

View File

@ -15,7 +15,7 @@ type Version struct {
// version variable that will be set at build time by versionManager.exe // version variable that will be set at build time by versionManager.exe
// *** DO NOT EDIT *** // *** DO NOT EDIT ***
var versionNumber Version = Version{0, 0, 0, 7} var versionNumber Version = Version{0, 0, 0, 25}
// GetFullVersion returns the full version number as a string // GetFullVersion returns the full version number as a string
func GetFullVersion() string { func GetFullVersion() string {