-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
162 lines (120 loc) · 3.02 KB
/
main.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
Magento-cli a cli application for managing local magneto instances
Magento-cli provides a wrapper for the bin/magento command in a directory,
and is intended to provide convenience commands like `serve` and `sql` leveraging
the local php web server, or docker-compose. If it doesn't match a command,
it will pass through the command to the `bin/magento` command, providing a nice
workflow around this tool.
*/
package main
import (
"embed"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/mumoshu/variant/cmd"
)
//go:embed tasks/*.yaml
var tasks embed.FS
//go:embed services/*.yaml
var services embed.FS
var yamlExt string = ".yaml"
var configDir string = ".magento-cli"
var testMode = false
var tempDir string = "./" + configDir + "/tmp"
var (
version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
)
func main() {
// output version info
if len(os.Args) > 1 {
arg := os.Args[1]
if arg == "version" {
fmt.Println("blueacorninc/magento-cli " + version)
os.Exit(0)
}
}
// put services files in a temp directory
extractYamlToTemp(services)
// feeds configuration into paraser and executes
yaml := string(loadYaml(tasks))
// do not execute if running tests
if !testMode {
cmd.YAML(yaml)
}
// remove temp diretory
rmTemp()
}
func extractYamlToTemp(objects embed.FS) {
os.MkdirAll(tempDir, 0777)
// fetches basic configuration, this uses embed.FS and sources from ./<objects>/
fs.WalkDir(objects, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
// fmt.Printf("path=%q, isDir=%v\n", path, d.IsDir())
if !d.IsDir() == true {
content, err := fs.ReadFile(objects, path)
if err == nil {
filename := tempDir + "/" + path
_ = os.MkdirAll(filepath.Dir(filename), 0700)
f, err := os.Create(tempDir + "/" + path)
if err != nil {
fmt.Println(err)
return nil
}
_, err = f.WriteString(string(content))
if err != nil {
fmt.Println(err)
f.Close()
return nil
}
err = f.Close()
if err != nil {
fmt.Println(err)
return nil
}
}
}
return nil
})
}
func rmTemp() {
defer os.RemoveAll(tempDir)
}
func loadYaml(objects embed.FS) string {
data := ""
// fetches basic configuration, this uses embed.FS and sources from ./<objects>/
fs.WalkDir(objects, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
// fmt.Printf("path=%q, isDir=%v\n", path, d.IsDir())
content, err := fs.ReadFile(objects, path)
if err == nil {
data = data + string(content) + "\n"
}
return nil
})
// override mechanism, .magento-cli directories can contain additional configs
if _, err := os.Stat(configDir); !os.IsNotExist(err) {
files, err := ioutil.ReadDir(configDir)
if err != nil {
log.Fatal(err)
} else {
for _, f := range files {
if filepath.Ext(f.Name()) == yamlExt {
content, _ := ioutil.ReadFile(configDir + f.Name())
data = data + string(content) + "\n"
}
}
}
}
return data
}