make some correction

This commit is contained in:
2023-11-29 12:57:49 -05:00
parent 70c9e72753
commit 8000ff69c9
11 changed files with 948 additions and 65 deletions
+128 -16
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"kode-creator/config"
"kode-creator/configFile"
"os/exec"
"kode-creator/utils"
"os"
@@ -28,17 +29,8 @@ var EnvCmd = &cobra.Command{
Long: `A simple CLI option to manage environment variables`,
Run: func(cmd *cobra.Command, args []string) {
<<<<<<< HEAD
// Start Env command
startEnvVar(cmd, args)
=======
config.SetInformations(cmd, args)
// if option --env or -e is set then print environment variables
if config.GetEnvVariablesFlag() {
printEnvVariables()
}
>>>>>>> b7ba427 (corr. envVar)
},
}
@@ -55,6 +47,7 @@ func startEnvVar(cmd *cobra.Command, args []string) {
CfgGlobal.DebugPrintConfig("Function StartEnvVar")
// check if env flag EnvVariablesListFlag is set
if CfgGlobal.GetEnvListFlag() {
if !CfgGlobal.GetJsonFlag() {
@@ -74,6 +67,40 @@ func startEnvVar(cmd *cobra.Command, args []string) {
fmt.Println(json)
}
// check if env flag EnvVariablesSetFlag is set
} else if CfgGlobal.GetEnvSetFlag() {
// if key and value are not empty
if CfgGlobal.GetEnvKey() != "" && CfgGlobal.GetEnvValue() != "" {
// set environment variable with key and value permanently
if utils.IsWindows() {
createEnvVariableWindows(CfgGlobal.GetEnvKey(), CfgGlobal.GetEnvValue())
} else {
createEnvVariableLinux(CfgGlobal.GetEnvKey(), CfgGlobal.GetEnvValue())
}
utils.PrintHeader()
color.Green.Println("\n S E T E N V I R O N M E N T V A R I A B L E ")
color.Yellow.Println(utils.CreateLine(15))
color.Green.Println("Key: " + CfgGlobal.GetEnvKey())
color.Green.Println("Value: " + CfgGlobal.GetEnvValue())
// get all environment variables
printEnvVariables()
color.Yellow.Println(utils.CreateLine(15))
} else {
utils.PrintHeader()
color.Green.Println("\n S E T E N V I R O N M E N T V A R I A B L E ")
color.Yellow.Println(utils.CreateLine(15))
color.Red.Println("Error: key and value are required")
fmt.Println("\nExample: kode-creator env -s -k KEY -v VALUE")
color.Yellow.Println(utils.CreateLine(15))
return
}
// check if env flag EnvVariablesYmlFlag is set
} else if CfgGlobal.GetEnvYmlFileFlag() {
cfgYmlFile := configFile.LoadConfigFile(CfgGlobal)
@@ -146,8 +173,9 @@ func startEnvVar(cmd *cobra.Command, args []string) {
}
} else {
utils.PrintHeader()
cmd.Help()
fmt.Println(utils.CreateLine(8))
color.Yellow.Println(utils.CreateLine(15))
}
}
@@ -307,8 +335,14 @@ func listEnvVariables() error {
}
func printEnvVariables() {
fmt.Println("E n v i r o n m e n t v a r i a b l e s")
fmt.Println("-----------------------------------------")
// print header if json flag is not set
if !CfgGlobal.GetJsonFlag() {
utils.PrintHeader()
}
color.Green.Println("E n v i r o n m e n t v a r i a b l e s")
color.Yellow.Println(utils.CreateLine(15))
nb := 1
@@ -320,13 +354,14 @@ func printEnvVariables() {
nb++
}
fmt.Println("-----------------------------------------")
color.Yellow.Println(utils.CreateLine(15))
}
func printEnvVariablesFilter(filter string, showAll bool) {
fmt.Println("Filter Environment variables")
// fmt.Println("---------------------")
utils.CreateLine(5)
utils.PrintHeader()
color.Green.Println("F i l t e r E n v i r o n m e n t v a r i a b l e s")
color.Yellow.Println(utils.CreateLine(15))
nb := 1
var str2, str3 string
@@ -359,4 +394,81 @@ func printEnvVariablesFilter(filter string, showAll bool) {
nb++
}
color.Yellow.Println(utils.CreateLine(15))
}
// create a permanent environment variable on Windows with key and value
func createEnvVariableWindows(key string, value string) error {
// detect if OS is Windows
if utils.IsWindows() {
// set environment variable with key and value permanently
// setx Key "Value"
cmd := exec.Command("cmd", "/C", "setx", key, value)
err := cmd.Run()
if err != nil {
color.Red.Println("Error:", err)
}
}
return nil
}
// create a permanent environment variable on Linux with key and value
func createEnvVariableLinux(key string, value string) error {
// detect if OS is Linux
if !utils.IsWindows() {
// set environment variable with key and value permanently
// setx Key "Value"
cmd := exec.Command("export", key+"="+value)
err := cmd.Run()
if err != nil {
color.Red.Println("Error:", err)
}
}
return nil
}
// delete a permanent environment variable on Windows with key
func deleteEnvVariableWindows(key string) error {
// detect if OS is Windows
if utils.IsWindows() {
// set environment variable with key and value permanently
// setx Key "Value"
cmd := exec.Command("cmd", "/C", "setx", key, "")
err := cmd.Run()
if err != nil {
color.Red.Println("Error:", err)
}
}
return nil
}
// delete a permanent environment variable on Linux with key
func deleteEnvVariableLinux(key string) error {
// detect if OS is Linux
if !utils.IsWindows() {
// set environment variable with key and value permanently
// setx Key "Value"
cmd := exec.Command("unset", key)
err := cmd.Run()
if err != nil {
color.Red.Println("Error:", err)
}
}
return nil
}