-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete.go
77 lines (63 loc) · 2.15 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"database/sql"
"fmt"
dockerMethods "github.com/epos-eu/opensource-docker/cmd/methods"
kubernetesMethods "github.com/epos-eu/opensource-kubernetes/cmd/methods"
)
// Deletes an installed environment from the database given its name and version
func (a *App) DeleteInstalledEnvironment(platform, name, version, context string) error {
fmt.Println("Platform: ", platform)
fmt.Println("Name: ", name)
fmt.Println("Version: ", version)
fmt.Println("Context: ", context)
var err error
if platform == "docker" {
err = deleteDockerEnvironment(name, version)
} else if platform == "kubernetes" {
err = deleteKubernetesEnvironment(name, context)
} else {
return fmt.Errorf("unknown platform: %s", platform)
}
if err != nil {
return err
}
// If the environment was successfully deleted, delete it from the database
err = deleteEnvironmentFromDatabase(name, version, platform, context)
return err
}
// Deletes an installed environment from the database given its name and version
func deleteEnvironmentFromDatabase(name, version, platform, context string) error {
// Open the database
db, err := sql.Open("sqlite3", databasePath)
if err != nil {
return err
}
defer db.Close()
// Delete the environment from the database
result, err := db.Exec("DELETE FROM environments WHERE name = ? AND version = ? AND platform = ? AND context = ?", name, version, platform, context)
// If there was an error or the query didn't match any rows, return the error
if err != nil || result == nil {
return err
}
return nil
}
func deleteDockerEnvironment(name, version string) error {
// Get the environment variables as a temp file
envFilePath, err := getEnvironmentVariablesTempFilePath(name, version, "docker")
// Call the delete cmd
err = dockerMethods.DeleteEnvironment(
envFilePath, // environment variables file path
"", // docker compose file path
name, // environment name
version, // environment version
)
return err
}
func deleteKubernetesEnvironment(name, context string) error {
// Call the delete cmd
return kubernetesMethods.DeleteEnvironment(
context, // kubernetes context
name, // namespace
)
}