package config import ( "fmt" "kode-starter/utils" "time" "github.com/spf13/cobra" ) type Config struct { DebugMode bool LogLevel string author string ModifDate string ConfigFileYmlName string TokenEnv string VerboseFlag bool JsonFlag bool VersionFlag bool HelpFlag bool Url URLConfig ListCmd ListCmdConfig CreateCmd CreateCmdConfig EnvCmd EnvCmdConfig applicationName string } type URLConfig struct { Base string ApiBase string ApiOrgs string } type ListCmdConfig struct { Organization string GitFlag bool TeaFlag bool User string listOrganizationFlag bool } type CreateCmdConfig struct { Name string Organisation string Description string Private bool } type EnvCmdConfig struct { EnvVariablesFlag bool EnvVariablesListFlag bool EnvVariablesFilterFlag bool EnvVariablesFilter string EnvVariablesYmlFlag bool EnvVariablesCategoryFlag bool EnvVariablesCategory string EnvVariablesSetFlag bool EnvVariablesShowAllFlag bool } // * * * * * * * * * * * * * * * * * * * * * * * * // // **** G E N E R A L GETTER - SETTER **** // * * * * * * * * * * * * * * * * * * * * * * * * // // Getter for DebugMode field func (c *Config) GetDebugMode() bool { return c.DebugMode } // Setter for DebugMode field func (c *Config) SetDebugMode(value bool) { c.DebugMode = value } // Getter for LogLevel field func (c *Config) GetLogLevel() string { return c.LogLevel } // Setter for LogLevel field func (c *Config) SetLogLevel(value string) { c.LogLevel = value } // Getter for author field func (c *Config) GetAuthor() string { return c.author } // Setter for author field func (c *Config) SetAuthor(value string) { c.author = value } // Getter for modifDate field func (c *Config) GetModifDate() string { return c.ModifDate } // Setter for modifDate field func (c *Config) SetModifDate(value string) { c.ModifDate = value } // Getter for ConfigFileYmlName field func (c *Config) GetConfigFileYmlName() string { return c.ConfigFileYmlName } // Setter for ConfigFileYmlName field func (c *Config) SetConfigFileYmlName(value string) { c.ConfigFileYmlName = value } // Getter for tokenEnv field func (c *Config) GetTokenEnv() string { return c.TokenEnv } // Setter for tokenEnv field func (c *Config) SetTokenEnv(value string) { c.TokenEnv = value } // Getter for verboseFlag field func (c *Config) GetVerboseFlag() bool { return c.VerboseFlag } // Setter for verboseFlag field func (c *Config) SetVerboseFlag(value bool) { c.VerboseFlag = value } // Getter for jsonFlag field func (c *Config) GetJsonFlag() bool { return c.JsonFlag } // Setter for jsonFlag field func (c *Config) SetJsonFlag(value bool) { c.JsonFlag = value } // Getter for versionFlag field func (c *Config) GetVersionFlag() bool { return c.VersionFlag } // Setter for versionFlag field func (c *Config) SetVersionFlag(value bool) { c.VersionFlag = value } // Getter for helpFlag field func (c *Config) GetHelpFlag() bool { return c.HelpFlag } // Setter for helpFlag field func (c *Config) SetHelpFlag(value bool) { c.HelpFlag = value } // Getter for url field func (c *Config) GetUrl() URLConfig { return c.Url } // Setter for url field func (c *Config) SetUrl(value URLConfig) { c.Url = value } // Getter urlBase field func (c *Config) GetUrlBase() string { return c.Url.Base } // Setter urlBase field func (c *Config) SetUrlBase(value string) { c.Url.Base = value } // Getter urlApiBase field func (c *Config) GetUrlApiBase() string { return c.Url.ApiBase } // Setter urlApiBase field func (c *Config) SetUrlApiBase(value string) { c.Url.ApiBase = value } // Getter urlApiOrgs field func (c *Config) GetUrlApiOrgs() string { return c.Url.ApiOrgs } // Setter urlApiOrgs field func (c *Config) SetUrlApiOrgs(value string) { c.Url.ApiOrgs = value } // Getter for applicationName field func (c *Config) GetApplicationName() string { return c.applicationName } // Setter for applicationName field func (c *Config) SetApplicationName(value string) { c.applicationName = value } // * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * L I S T C M D GETTER - SETTER * * * * // * * * * * * * * * * * * * * * * * * * * * * * * // // Getter for listCmd field func (c *Config) GetListCmd() ListCmdConfig { return c.ListCmd } // Setter for listCmd field func (c *Config) SetListCmd(value ListCmdConfig) { c.ListCmd = value } // Getter for listOrganizationFlag field func (c *Config) GetListOrganizationFlag() bool { return c.ListCmd.listOrganizationFlag } // Setter for listOrganizationFlag field func (c *Config) SetListOrganizationFlag(value bool) { c.ListCmd.listOrganizationFlag = value } // Getter for listOrganization field func (c *Config) GetListOrganization() string { return c.ListCmd.Organization } // Setter for listOrganization field func (c *Config) SetListOrganization(value string) { c.ListCmd.Organization = value } // getter for Git flag func (c *Config) GetGitFlag() bool { return c.ListCmd.GitFlag } // setter for Git flag func (c *Config) SetGitFlag(value bool) { c.ListCmd.GitFlag = value } // getter for Tea flag func (c *Config) GetTeaFlag() bool { return c.ListCmd.TeaFlag } // setter for Tea flag func (c *Config) SetTeaFlag(value bool) { c.ListCmd.TeaFlag = value } // getter for User func (c *Config) GetListUser() string { return c.ListCmd.User } // setter for User func (c *Config) SetListUser(value string) { c.ListCmd.User = value } // * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * C R E A T E C M D GETTER - SETTER * * * * // * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Getter for createCmd field func (c *Config) GetCreateCmd() CreateCmdConfig { return c.CreateCmd } // Setter for createCmd field func (c *Config) SetCreateCmd(value CreateCmdConfig) { c.CreateCmd = value } // Getter for createName field func (c *Config) GetCreateName() string { return c.CreateCmd.Name } // Setter for createName field func (c *Config) SetCreateName(value string) { c.CreateCmd.Name = value } // Getter for createDescription field func (c *Config) GetCreateDescription() string { return c.CreateCmd.Description } // Setter for createDescription field func (c *Config) SetCreateDescription(value string) { c.CreateCmd.Description = value } // Getter for createPrivate field func (c *Config) GetCreatePrivate() bool { return c.CreateCmd.Private } // Setter for createPrivate field func (c *Config) SetCreatePrivate(value bool) { c.CreateCmd.Private = value } // * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * E N V C M D GETTER - SETTER * * * * // * * * * * * * * * * * * * * * * * * * * * * * * * * * // // Getter for envCmd field func (c *Config) GetEnvCmd() EnvCmdConfig { return c.EnvCmd } // Setter for envCmd field func (c *Config) SetEnvCmd(value EnvCmdConfig) { c.EnvCmd = value } // Getter for envVariblesFlag flag func (c *Config) GetEnvVariblesFlag() bool { return c.EnvCmd.EnvVariablesFlag } // Setter for envVariblesFlag flag func (c *Config) SetEnvVariblesFlag(value bool) { c.EnvCmd.EnvVariablesFlag = value } // Getter for env list flag func (c *Config) GetEnvListFlag() bool { return c.EnvCmd.EnvVariablesListFlag } // Setter for env list flag func (c *Config) SetEnvListFlag(value bool) { c.EnvCmd.EnvVariablesListFlag = value } // Getter for env filter flag func (c *Config) GetEnvFilterFlag() bool { return c.EnvCmd.EnvVariablesFilterFlag } // Setter for env filter flag func (c *Config) SetEnvFilterFlag(value bool) { c.EnvCmd.EnvVariablesFilterFlag = value } // Getter for env filter func (c *Config) GetEnvFilter() string { return c.EnvCmd.EnvVariablesFilter } // Setter for env filter func (c *Config) SetEnvFilterInfo(value string) { c.EnvCmd.EnvVariablesFilter = value } // Getter for env yml file flag func (c *Config) GetEnvYmlFileFlag() bool { return c.EnvCmd.EnvVariablesYmlFlag } // Setter for env yml file flag func (c *Config) SetEnvYmlFileFlag(value bool) { c.EnvCmd.EnvVariablesYmlFlag = value } // Getter for env category flag func (c *Config) GetEnvCategoryFlag() bool { return c.EnvCmd.EnvVariablesCategoryFlag } // Setter for env category flag func (c *Config) SetEnvCategoryFlag(value bool) { c.EnvCmd.EnvVariablesCategoryFlag = value } // Getter for env category Info func (c *Config) GetEnvCategoryInfo() string { return c.EnvCmd.EnvVariablesCategory } // Setter for env category Info func (c *Config) SetEnvCategoryInfo(value string) { c.EnvCmd.EnvVariablesCategory = value } // Getter for env Set flag func (c *Config) GetEnvSetFlag() bool { return c.EnvCmd.EnvVariablesSetFlag } // Setter for env Set flag func (c *Config) SetEnvSetFlag(value bool) { c.EnvCmd.EnvVariablesSetFlag = value } // Getter for env Show All flag func (c *Config) GetEnvShowAllFlag() bool { return c.EnvCmd.EnvVariablesShowAllFlag } // Setter for env Show All flag func (c *Config) SetEnvShowAllFlag(value bool) { c.EnvCmd.EnvVariablesShowAllFlag = value } // * * * * * * * * * * * * * * * * * * * * * * * * * * * // // initialize basic configuration // * * * * * * * * * * * * * * * * * * * * * * * * * * * // func (c *Config) Init() { // autor var author string = "Bruno Charest" // Cfg.SetAuthor(author) c.SetAuthor(author) // set modification date to current date var modifDate string = time.Now().Format("2006-01-02") c.SetModifDate(modifDate) // set config file name var configFileYmlName string = utils.GetProgramDir() + "/config.yml" c.SetConfigFileYmlName(configFileYmlName) // set token from environment variable var tokenEnv string = utils.GetGitTokenFromOsEnv() c.SetTokenEnv(tokenEnv) // set verbose flag c.SetVerboseFlag(false) // set json flag c.SetJsonFlag(false) // set version flag c.SetVersionFlag(false) // set help flag c.SetHelpFlag(false) // set url base var urlBase string = "https://git.bcmaison.cf" c.SetUrlBase(urlBase) // set url api base var urlApiBase string = urlBase + "/api/v1" c.SetUrlApiBase(urlApiBase) // set url api orgs var urlApiOrgs string = urlApiBase + "/orgs" c.SetUrlApiOrgs(urlApiOrgs) //set application name var appName string = "Kode-Creator" c.SetApplicationName(appName) } // * * * * * * * * * * * * * * * * * * * * * * * * * * * // // initialize configuration for commands and flags // for root command // * * * * * * * * * * * * * * * * * * * * * * * * * * * // func (c *Config) InitCmdRoot(cmd *cobra.Command, args []string) { // Check if the version flag is set if cmd.Flags().Changed("version") || cmd.Flags().Changed("V") { c.SetVersionFlag(true) } // Check if the verbose flag is set if cmd.Flags().Changed("verbose") || cmd.Flags().Changed("v") { c.SetVerboseFlag(true) } else { c.SetVerboseFlag(false) } // Check if the json flag is set if cmd.Flags().Changed("json") || cmd.Flags().Changed("j") { c.SetJsonFlag(true) } else { c.SetJsonFlag(false) } // check if the help flag is set if cmd.Flags().Changed("help") || cmd.Flags().Changed("h") { c.SetHelpFlag(true) } } // * * * * * * * * * * * * * * * * * * * * * * * * * * * // // initialize configuration command and flags // for list command // * * * * * * * * * * * * * * * * * * * * * * * * * * * // func (c *Config) InitCmdList(cmd *cobra.Command, args []string) { if cmd.Flags().Changed("org") || cmd.Flags().Changed("o") { if cmd.Name() == "list" { c.SetListOrganization(cmd.Flag("org").Value.String()) c.SetListOrganizationFlag(true) } else { c.SetListOrganization(cmd.Flag("org").Value.String()) } } if cmd.Flags().Changed("user") || cmd.Flags().Changed("u") { c.SetListUser(cmd.Flag("user").Value.String()) } if cmd.Flags().Changed("git") || cmd.Flags().Changed("g") { c.SetGitFlag(true) } if cmd.Flags().Changed("tea") || cmd.Flags().Changed("a") { c.SetTeaFlag(true) } } // * * * * * * * * * * * * * * * * * * * * * * * * * * * // // initialize configuration for commands and flags // for create command // * * * * * * * * * * * * * * * * * * * * * * * * * * * // func (c *Config) InitCmdCreate(cmd *cobra.Command, args []string) { // initialize configuration for root command // c.InitCmdRoot(cmd, args) // validate flag on create command if cmd.Name() == "create" { // set organization flag if set if cmd.Flags().Changed("org") || cmd.Flags().Changed("o") { c.SetListOrganization(cmd.Flag("org").Value.String()) c.SetListOrganizationFlag(true) } // set name flag if set if cmd.Flags().Changed("name") || cmd.Flags().Changed("n") { c.SetCreateName(cmd.Flag("name").Value.String()) } // set description flag if set if cmd.Flags().Changed("desc") || cmd.Flags().Changed("d") { c.SetCreateDescription(cmd.Flag("desc").Value.String()) } // set private flag if set if cmd.Flags().Changed("private") || cmd.Flags().Changed("p") { if cmd.Flag("private").Value.String() == "true" { c.SetCreatePrivate(true) } else if cmd.Flag("private").Value.String() == "false" { c.SetCreatePrivate(false) } else { c.SetCreatePrivate(false) } } } } // * * * * * * * * * * * * * * * * * * * * * * * * * * * // // initialize configuration commands and flags // for env command // * * * * * * * * * * * * * * * * * * * * * * * * * * * // func (c *Config) InitCmdEnv(cmd *cobra.Command, args []string) { // initialize configuration for root command c.InitCmdRoot(cmd, args) // validate flag on env command if cmd.Name() == "env" { // if flag list is set, set filter flag if cmd.Flags().Changed("list") || cmd.Flags().Changed("l") { c.SetEnvListFlag(true) // // set env variables filter flag if set // if cmd.Flags().Changed("filter") || cmd.Flags().Changed("f") { // c.SetEnvFilterInfo(cmd.Flag("filter").Value.String()) // c.SetEnvFilterFlag(true) // } } // set env variables filter flag if set if cmd.Flags().Changed("filter") || cmd.Flags().Changed("f") { c.SetEnvFilterInfo(cmd.Flag("filter").Value.String()) c.SetEnvFilterFlag(true) } if cmd.Flags().Changed("yml") || cmd.Flags().Changed("y") { c.SetEnvYmlFileFlag(true) } if cmd.Flags().Changed("set") || cmd.Flags().Changed("s") { c.SetEnvSetFlag(true) } // set env variables category flag if set if cmd.Flags().Changed("cat") || cmd.Flags().Changed("c") { fmt.Println("cat flag set") c.SetEnvCategoryFlag(true) c.SetEnvCategoryInfo(cmd.Flag("cat").Value.String()) } // set env variables show all flag if set if cmd.Flags().Changed("all") || cmd.Flags().Changed("a") { c.SetEnvShowAllFlag(true) } } } // function that print all configuration if DebugMode is true func (c *Config) DebugPrintConfig(section string) { if c.GetDebugMode() { fmt.Println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -") fmt.Println("| Section: " + section) fmt.Println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -") c.printAllConfiguration() } } // function that print string if DebugMode is true func (c *Config) DebugPrintString(str string) { if c.GetDebugMode() { fmt.Println(str) } } // * * * * * * * * * * * * * * * * * * * * * * * * * * * // // function to print all configuration // for information and flags // * * * * * * * * * * * * * * * * * * * * * * * * * * * // func (c *Config) printAllConfiguration() { fmt.Println("General Configuration:") fmt.Println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -") fmt.Println("Config: DebugMode: ", c.GetDebugMode()) fmt.Println("Config: LogLevel: ", c.GetLogLevel()) fmt.Println("config: Author: ", c.GetAuthor()) fmt.Println("config: ModifDate: ", c.GetModifDate()) fmt.Println("config: ConfigFileYmlName: ", c.GetConfigFileYmlName()) fmt.Println("config: TokenEnv: ", c.GetTokenEnv()) fmt.Println("config: VerboseFlag: ", c.GetVerboseFlag()) fmt.Println("config: JsonFlag: ", c.GetJsonFlag()) fmt.Println("config: VersionFlag: ", c.GetVersionFlag()) fmt.Println("config: HelpFlag: ", c.GetHelpFlag()) fmt.Println("config: UrlBase: ", c.GetUrlBase()) fmt.Println("config: UrlApiBase: ", c.GetUrlApiBase()) fmt.Println("config: UrlApiOrgs: ", c.GetUrlApiOrgs()) fmt.Println("List Commands Configuration:") fmt.Println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -") fmt.Println("config: ListOrganization: ", c.GetListOrganization()) fmt.Println("config: ListOrganizationFlag: ", c.GetListOrganizationFlag()) fmt.Println("Create commands Configuration:") fmt.Println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -") fmt.Println("config: CreateName: ", c.GetCreateCmd().Name) fmt.Println("config: Organization: ", c.GetCreateCmd().Organisation) fmt.Println("config: CreateDescription: ", c.GetCreateCmd().Description) fmt.Println("config: CreatePrivate: ", c.GetCreatePrivate()) fmt.Println("config: CreateCmd: ", c.GetCreateCmd()) fmt.Println("Environment Variables Configuration:") fmt.Println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -") fmt.Println("config: EnvCmd: ", c.GetEnvCmd()) fmt.Println("config: EnvVariablesFlag: ", c.GetEnvVariblesFlag()) fmt.Println("config: EnvListFlag: ", c.GetEnvListFlag()) fmt.Println("config: EnvFilterFlag: ", c.GetEnvFilterFlag()) fmt.Println("config: EnvVariablesFilter: ", c.GetEnvFilter()) fmt.Println("config: EnvVariablesYmlFlag: ", c.GetEnvYmlFileFlag()) fmt.Println("config: EnvVariablesCategoryFlag: ", c.GetEnvCategoryFlag()) fmt.Println("config: EnvVariablesCategory: ", c.GetEnvCategoryInfo()) fmt.Println("config: EnvVariablesSetFlag: ", c.GetEnvSetFlag()) fmt.Println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -") }