Skip to content

Commit

Permalink
added all return error
Browse files Browse the repository at this point in the history
  • Loading branch information
vvalerio-priv committed Feb 29, 2024
1 parent d37e65d commit 52792f1
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 65 deletions.
54 changes: 37 additions & 17 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var deployCmd = &cobra.Command{
Use: "deploy",
Short: "Deploy an environment on docker",
Long: `Deploy an enviroment with .env set up on docker`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
env, _ := cmd.Flags().GetString("env")
externalip, _ := cmd.Flags().GetString("externalip")
dockercomposefile, _ := cmd.Flags().GetString("dockercompose")
Expand All @@ -56,34 +56,53 @@ var deployCmd = &cobra.Command{

dname := os.TempDir() + os.Getenv("PREFIX")

RemoveContents(dname)
createDirectory(dname)
if err := RemoveContents(dname); err != nil {
printError("Error on removing the content from directory " + err.Error())
return err
}
if err := createDirectory(dname); err != nil {
printError("Error on creating the directory " + err.Error())
return err
}

isDefaultEnv := false
if env == "" {
env = generateTempFile(dname, "configurations", configurations)
ret_env, err := generateTempFile(dname, "configurations", configurations)
if err != nil {
return err
}
env = ret_env
isDefaultEnv = true
}

if dockercomposefile == "" {
dockercomposefile = generateTempFile(dname, "dockercompose", dockercompose)
ret_dockercomposefile, err := generateTempFile(dname, "dockercompose", dockercompose)
if err != nil {
return err
}
dockercomposefile = ret_dockercomposefile
}
if err := godotenv.Overload(env); err != nil {
printError("Loading env variables from " + env + " cause: " + err.Error())

}

if isDefaultEnv {
checkImagesUpdate()
return err
}

if autoupdate == "true" {
checkImagesUpdate()
if autoupdate == "true" || isDefaultEnv {
if err := checkImagesUpdate(); err != nil {
printError("Error on updating the docker container images " + err.Error())
return err
}
}
if externalip == "" {
setupIPs()
if err := setupIPs(); err != nil {
printError("Error on setting the IPs " + err.Error())
return err
}
} else {
setupProvidedIPs(externalip)
if err := setupProvidedIPs(externalip); err != nil {
printError("Error on setting the IPs using the provided IP " + err.Error())
return err
}
}

printSetup(env, dockercomposefile)
Expand All @@ -104,7 +123,7 @@ var deployCmd = &cobra.Command{

if err := command.Run(); err != nil {
printError("Creation of rabbitmq container failed, cause: " + err.Error())

return err
}
time.Sleep(15 * time.Second)
printTask("Installing all remaining containers on the machine")
Expand All @@ -120,7 +139,7 @@ var deployCmd = &cobra.Command{
command.Stderr = os.Stderr
if err := command.Run(); err != nil {
printError("Creation of container failed, cause: " + err.Error())

return err
}
printWait("Waiting for the containers to be up and running...")
time.Sleep(40 * time.Second)
Expand All @@ -135,10 +154,11 @@ var deployCmd = &cobra.Command{
command.Stderr = os.Stderr
if err := command.Run(); err != nil {
printError("Creation of container failed, cause: " + err.Error())

return err
}
time.Sleep(5 * time.Second)
print_urls()
return nil
},
}

Expand Down
31 changes: 23 additions & 8 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete an environment on docker",
Long: `Delete an enviroment with .env set up on docker`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
env, _ := cmd.Flags().GetString("env")
dockercomposefile, _ := cmd.Flags().GetString("dockercompose")
envname, _ := cmd.Flags().GetString("envname")
Expand All @@ -56,19 +56,34 @@ var deleteCmd = &cobra.Command{

dname := os.TempDir() + os.Getenv("PREFIX")

RemoveContents(dname)
createDirectory(dname)
if err := RemoveContents(dname); err != nil {
printError("Error on removing the content from directory " + err.Error())
return err
}
if err := createDirectory(dname); err != nil {
printError("Error on creating the directory " + err.Error())
return err
}

if env == "" {
env = generateTempFile(dname, "configurations", configurations)
ret_env, err := generateTempFile(dname, "configurations", configurations)
if err != nil {
return err
}
env = ret_env
isDefaultEnv = true
}

if dockercomposefile == "" {
dockercomposefile = generateTempFile(dname, "dockercompose", dockercompose)
ret_dockercomposefile, err := generateTempFile(dname, "dockercompose", dockercompose)
if err != nil {
return err
}
dockercomposefile = ret_dockercomposefile
}
if err := godotenv.Overload(env); err != nil {
printError("Error loading env variables from " + env + " cause: " + err.Error())

return err
}
if isDefaultEnv {
checkImagesUpdate()
Expand All @@ -86,9 +101,9 @@ var deleteCmd = &cobra.Command{
command.Stderr = os.Stderr
if err := command.Run(); err != nil {
printError("Error deleting environment, cause: " + err.Error())

return err
}

return nil
},
}

Expand Down
13 changes: 10 additions & 3 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,26 @@ var exportCmd = &cobra.Command{
Use: "export",
Short: "Export configuration files in output folder, options: [env, compose]",
Long: `Export configuration files for customization in output folder, options: [env, compose]`,
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {

file, _ := cmd.Flags().GetString("file")
output, _ := cmd.Flags().GetString("output")

switch file {
case "env":
generateFile(configurations, output+"/configurations.env")
if err := generateFile(configurations, output+"/configurations.env"); err != nil {
printError("Error on generating file ENV, cause: " + err.Error())
return err
}
case "compose":
generateFile(dockercompose, output+"/docker-compose.yaml")
if err := generateFile(dockercompose, output+"/docker-compose.yaml"); err != nil {
printError("Error on generating file DOCKER-COMPOSE, cause: " + err.Error())
return err
}
default:
printError("Invalid option, available options: [env, compose]")
}
return nil
},
}

Expand Down
Loading

0 comments on commit 52792f1

Please sign in to comment.