config struct before

This commit is contained in:
bruno 2023-06-25 12:54:50 -04:00
parent 1895ade98e
commit 7c24456629
8 changed files with 210 additions and 78 deletions

View File

@ -3,9 +3,12 @@ package cmdEnv
import ( import (
"fmt" "fmt"
"kode-starter/config" "kode-starter/config"
"kode-starter/utils"
"os" "os"
"strconv"
"strings" "strings"
"github.com/gookit/color"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -16,15 +19,56 @@ var EnvCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
config.SetInformations(cmd, args) config.SetInformations(cmd, args)
startEnvVar(cmd, args)
// if option --env or -e is set then print environment variables // listFlag := config.GetEnvVariablesListFlag()
// if envVariablesFlag {
// fmt.Println("listFlag: " + strconv.FormatBool(listFlag))
// fmt.Println("GetEnvVariablesListFlag: " + strconv.FormatBool(config.GetEnvVariablesListFlag()))
// if listFlag {
// printEnvVariables() // printEnvVariables()
// } // }
// err := listEnvVariables()
// if err != nil {
// fmt.Println("Error:", err)
// return
// }
// if config.GetEnvVariablesFilterFlag() {
// printEnvVariablesFilter(config.GetEnvVariablesFilter())
// } else if config.GetEnvVariablesListFlag() {
// printEnvVariables()
// }
}, },
} }
func startEnvVar(cmd *cobra.Command, args []string) {
config.SetInformations(cmd, args)
flag := config.EnvVariablesFlag
// if list flag is set to true list all environment variables
fmt.Println("GetEnvVariablesListFlag: " + strconv.FormatBool(config.GetEnvVariablesListFlag()))
if flag {
printEnvVariables()
}
}
func listEnvVariables() error {
if config.GetEnvVariablesListFlag() {
fmt.Println("GetEnvVariablesListFlag: " + strconv.FormatBool(config.GetEnvVariablesListFlag()))
printEnvVariables()
}
return nil
}
func printEnvVariables() { func printEnvVariables() {
fmt.Println("Environment variables") fmt.Println("Environment variables")
fmt.Println("---------------------") fmt.Println("---------------------")
@ -33,6 +77,26 @@ func printEnvVariables() {
for _, e := range os.Environ() { for _, e := range os.Environ() {
// split key and value // split key and value
pair := strings.Split(e, "=") pair := strings.Split(e, "=")
fmt.Println(pair[0]) fmt.Printf("%s=%s\n", pair[0], pair[1])
}
}
func printEnvVariablesFilter(filter string) {
fmt.Println("Environment variables")
// fmt.Println("---------------------")
utils.CreateLine(5)
// loop over all environment variables
for _, e := range os.Environ() {
// split key and value
pair := strings.Split(e, "=")
if strings.Contains(pair[0], filter) {
fmt.Printf("%s=%s\n", utils.ColorizeSubStrRed(pair[0], filter), color.Green.Sprint(pair[1]))
} else if strings.Contains(pair[1], filter) {
fmt.Printf("%s=%s\n", color.Green.Sprint(pair[0]), utils.ColorizeSubStrRed(pair[1], filter))
} else {
fmt.Printf("%s=%s\n", pair[0], pair[1])
}
} }
} }

View File

@ -30,7 +30,7 @@ var ListCmd = &cobra.Command{
// if -o flag is set, list projects for that org // if -o flag is set, list projects for that org
if config.GetListOrganisationFlag() { if config.GetListOrganisationFlag() {
if !config.GetJsonFlag() { if !config.GetJsonFlag() {
utils.PrintHeader() // utils.PrintHeader()
fmt.Println("\nList projects for org: " + config.GetListOrganisation() + "\n") fmt.Println("\nList projects for org: " + config.GetListOrganisation() + "\n")
fmt.Println(utils.CreateLine(6)) fmt.Println(utils.CreateLine(6))
} }

View File

@ -0,0 +1,84 @@
package configFile
import (
"fmt"
"kode-starter/config"
"log"
"os"
)
func loadConfigFile() *Config {
filename := config.GetConfigFileName()
cfg, err := LoadConfig(filename)
if err != nil {
log.Fatalf("error loading config file: %v", err)
os.Exit(1)
}
return cfg
}
func Listcategory() {
cfg := loadConfigFile()
// list all categories
ListCategories, err := cfg.GetListCategory()
if err != nil {
log.Fatalf("error getting categories: %v", err)
}
for _, c := range ListCategories {
fmt.Printf("Category: %s\n", c)
}
}
// // 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)

View File

@ -1,4 +1,4 @@
package config package configFile
import ( import (
"fmt" "fmt"
@ -61,7 +61,7 @@ func LoadConfig(filename string) (*Config, error) {
} }
// print source // print source
fmt.Println(string(source)) // fmt.Println(string(source))
err = yaml.Unmarshal(source, &config) err = yaml.Unmarshal(source, &config)
if err != nil { if err != nil {
@ -71,6 +71,16 @@ func LoadConfig(filename string) (*Config, error) {
return &config, nil return &config, nil
} }
// function GetListCategory return list category from config file
func (c *Config) GetListCategory() ([]string, error) {
var categories []string
for categoryName := range c.Sections {
categories = append(categories, categoryName)
}
return categories, nil
}
// This is a method defined on the `Config` struct that returns a list of software names for a given // 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 // section. It takes in a `sectionName` string as a parameter and returns a slice of strings and an
// error. // error.

1
go.sum
View File

@ -17,6 +17,7 @@ github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHg
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

View File

@ -1,15 +1,12 @@
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"
) )
@ -25,32 +22,41 @@ var rootCmd = &cobra.Command{
}, },
} }
var token, org, name, private, description, group string var token, org, name, private, description, envGroup, envCategory string
var verbose, createFlag bool var verbose, createFlag, versionFlag bool
var listFlag, envVariablesFlag, groupFlag bool var listEnvFlag, listEnvCfgFlag, setEnvFlag, envVariablesFlag, envGroupFlag bool
func init() { func init() {
rootCmd.Flags().BoolP("help", "h", false, "Show help for create command") // var config = &config.Config{}
// root menu section
rootCmd.Flags().BoolP("help", "h", false, "Show help for create command")
rootCmd.Flags().BoolVarP(&versionFlag, "version", "V", false, "Show version")
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "mode verbose") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "mode verbose")
rootCmd.PersistentFlags().BoolVarP(&createFlag, "json", "j", false, "Print output as json format") rootCmd.PersistentFlags().BoolVarP(&createFlag, "json", "j", false, "Print output as json format")
// rootCmd.PersistentFlags().BoolVarP(&envVariablesFlag, "env", "e", false, "environment variables ")
// List menu section
cmdLists.ListCmd.Flags().StringVarP(&token, "token", "t", "", "Github token") cmdLists.ListCmd.Flags().StringVarP(&token, "token", "t", "", "Github token")
cmdLists.ListCmd.Flags().StringVarP(&org, "org", "o", "", "Github organization") cmdLists.ListCmd.Flags().StringVarP(&org, "org", "o", "", "Github organization")
// Create menu section
cmdCreate.CreateCmd.Flags().StringVarP(&token, "token", "t", "", "Github token") cmdCreate.CreateCmd.Flags().StringVarP(&token, "token", "t", "", "Github token")
cmdCreate.CreateCmd.Flags().StringVarP(&org, "org", "o", "", "Github organization") cmdCreate.CreateCmd.Flags().StringVarP(&org, "org", "o", "", "Github organization")
cmdCreate.CreateCmd.Flags().StringVarP(&name, "name", "n", "", "Project name") cmdCreate.CreateCmd.Flags().StringVarP(&name, "name", "n", "", "Project name")
cmdCreate.CreateCmd.Flags().StringVarP(&description, "desc", "d", "", "Description") cmdCreate.CreateCmd.Flags().StringVarP(&description, "desc", "d", "", "Description")
cmdCreate.CreateCmd.Flags().StringVarP(&private, "private", "p", "", "true/false") cmdCreate.CreateCmd.Flags().StringVarP(&private, "private", "p", "", "true/false")
cmdEnv.EnvCmd.Flags().BoolVarP(&listFlag, "list", "l", false, "List options") // Env menu section
cmdEnv.EnvCmd.Flags().BoolVarP(&envVariablesFlag, "env", "e", false, "environnement variables") cmdEnv.EnvCmd.Flags().BoolVarP(&envVariablesFlag, "env", "e", false, "environnement variables management")
cmdEnv.EnvCmd.Flags().BoolVarP(&groupFlag, "group", "g", false, "categories of informations") cmdEnv.EnvCmd.Flags().BoolVarP(&listEnvFlag, "list", "l", false, "List environnement variables")
cmdEnv.EnvCmd.Flags().StringVarP(&group, "filter", "f", "", "filter for a specific category") cmdEnv.EnvCmd.Flags().StringVarP(&envGroup, "filter", "f", "", "Highlight a specific word in environnement variables")
cmdEnv.EnvCmd.Flags().BoolVarP(&listEnvCfgFlag, "yml", "y", false, "List environnement variables from config.yml")
cmdEnv.EnvCmd.Flags().StringVarP(&envCategory, "cat", "c", "", "filter for a specific category")
cmdEnv.EnvCmd.Flags().BoolVarP(&setEnvFlag, "set", "s", false, "set environnement variable available from config.yml")
// Add subcommands
rootCmd.AddCommand(cmdLists.ListCmd) rootCmd.AddCommand(cmdLists.ListCmd)
rootCmd.AddCommand(cmdCreate.CreateCmd) rootCmd.AddCommand(cmdCreate.CreateCmd)
rootCmd.AddCommand(cmdEnv.EnvCmd) rootCmd.AddCommand(cmdEnv.EnvCmd)
@ -61,79 +67,29 @@ func mainProgram(cmd *cobra.Command, args []string) {
config.SetInformations(cmd, args) config.SetInformations(cmd, args)
fmt.Println("Header") // if version flag is set, print version and exit
utils.PrintHeader() if config.GetVersionFlag() {
utils.PrintVersion(version.GetFullVersion(), config.GetAuthor(), config.GetBuildDate())
// Check if the help flag is set
if cmd.Flags().Changed("help") || cmd.Flags().Changed("h") {
utils.PrintHelpFormated(version.GetFullVersion(), config.GetAuthor(), config.GetBuildDate(), cmd)
return return
// if no flag is set, print help and exit
} else { } else {
// If no flag is set, show help //configFile.Listcategory()
utils.PrintHelpFormated(version.GetFullVersion(), config.GetAuthor(), config.GetBuildDate(), cmd) utils.PrintHelpFormated(version.GetFullVersion(), config.GetAuthor(), config.GetBuildDate(), cmd)
return return
} }
} }
func main() { func main() {
filename := "config.yml" // var config = &config.Config{}
cfg, err := config.LoadConfig(filename) // print header
if err != nil { utils.PrintHeader()
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)
// execute root command
rootCmd.Execute() rootCmd.Execute()
} }

View File

@ -67,6 +67,9 @@ You can set the following environment variables:
- `GIT_TOKEN` - Your Github personal access token. This will be used instead of passing the `-t` flag. - `GIT_TOKEN` - Your Github personal access token. This will be used instead of passing the `-t` flag.
*Temporaire*
(Git Token for linux)[ af65e1b846d721e3465422c04997d69b0cfe7f18 ]
##License ##License
MIT License. MIT License.

View File

@ -35,6 +35,15 @@ func PrintHelpFormated(version string, author string, buildDate string, cmd *cob
fmt.Println("") fmt.Println("")
cmd.Help() cmd.Help()
fmt.Println("") fmt.Println("")
PrintVersion(version, author, buildDate)
// color.Yellow.Println(CreateLine(15))
// color.Green.Println("| Version: " + version + " || Author: " + author + " || Build date: " + buildDate + " |")
// color.Yellow.Println(CreateLine(15))
// fmt.Println("")
}
func PrintVersion(version string, author string, buildDate string) {
color.Yellow.Println(CreateLine(15)) color.Yellow.Println(CreateLine(15))
color.Green.Println("| Version: " + version + " || Author: " + author + " || Build date: " + buildDate + " |") color.Green.Println("| Version: " + version + " || Author: " + author + " || Build date: " + buildDate + " |")
color.Yellow.Println(CreateLine(15)) color.Yellow.Println(CreateLine(15))
@ -113,3 +122,8 @@ func PadString(s string, n int) string {
} }
return s + strings.Repeat(" ", n-len(s)) return s + strings.Repeat(" ", n-len(s))
} }
// function that color in red sub-trings in a string
func ColorizeSubStrRed(s string, sub string) string {
return strings.Replace(s, sub, color.Red.Sprint(sub), -1)
}