This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmagefile-ops.go
125 lines (111 loc) · 3.14 KB
/
magefile-ops.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//go:build mage
// +build mage
package main
import (
"github.com/magefile/mage/sh"
"go.uber.org/zap"
"os"
_ "embed"
"fmt"
"github.com/cloudfoundry/go-cf-api/internal/config"
"github.com/cloudfoundry/go-cf-api/internal/helpers"
"github.com/cloudfoundry/go-cf-api/internal/logging"
dbconfig "github.com/cloudfoundry/go-cf-api/internal/storage/db"
"github.com/golobby/repl/interpreter"
)
//###################################//
// Mage Commands used my an operator //
//###################################//
/////////////////////
// RUNTIME HELPERS //
/////////////////////
var (
currentInterpreter *interpreter.Interpreter
)
// DBCreate creates the Database that is specified in the config file
func DBCreate(configPath string) error {
config, err := config.Get(configPath)
if err != nil {
return err
}
db, info, err := dbconfig.NewConnection(config.DB, false)
if err != nil {
return err
}
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", info.DatabaseName))
if err != nil {
return err
}
zap.L().Info(fmt.Sprintf("Successfully created database %v", info.DatabaseName))
return nil
}
// DBDelete deletes the Database that is specified in the config file
func DBDelete(configPath string) error {
config, err := config.Get(configPath)
if err != nil {
return err
}
logging.Setup(config)
db, info, err := dbconfig.NewConnection(config.DB, false)
if err != nil {
return err
}
_, err = db.Exec(fmt.Sprintf("DROP DATABASE %v", info.DatabaseName))
if err != nil {
return err
}
zap.L().Info(fmt.Sprintf("Successfully dropped database %v", info.DatabaseName))
return nil
}
// DBRecreate recreates (Delete,Create,Migrates) the Database that is specified in the config file (Cf-Api Config is passed as first postitional parameter and sql file as second postitional parameter)
func DBRecreate(configPath string, sqlFilePath string) error {
if err := DBDelete(configPath); err != nil {
return err
}
if err := DBCreate(configPath); err != nil {
return err
}
if err := DBLoad(configPath, sqlFilePath); err != nil {
return err
}
return nil
}
// DBLoad loads a SQL file into the Database (Cf-Api Config is passed as first postitional parameter and sql file as second postitional parameter)
func DBLoad(configPath string, sqlFilePath string) error {
config, err := config.Get(configPath)
if err != nil {
return err
}
logging.Setup(config)
_, info, err := dbconfig.NewConnection(config.DB, false)
if err != nil {
return err
}
switch info.Type {
case "postgres":
if err := sh.RunV("psql", config.DB.ConnectionString, "-f", sqlFilePath); err != nil {
return err
}
case "mysql":
if err := sh.RunV("mysql",
"-h", info.Host,
"-P", info.Port,
"-u", info.User,
"-D", info.DatabaseName,
fmt.Sprintf("--password=%s", info.Password),
"--protocol=tcp",
"-e", fmt.Sprintf("source %s", sqlFilePath),
); err != nil {
return err
}
}
return nil
}
//go:embed internal/config/defaults.yml
var defaultConfig []byte
// GenerateDefaultConfig generate a config file with default values
func GenerateDefaultConfig(configPath string) error {
err := os.WriteFile(configPath, defaultConfig, 0611)
helpers.CheckErrFatal(err)
return nil
}