This repository has been archived by the owner on Jul 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
158 lines (122 loc) · 3.44 KB
/
config.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
package gogenlib
import (
"bufio"
"fmt"
"io/fs"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
"gopkg.in/yaml.v3"
)
var (
reader bufio.Reader = *bufio.NewReader(os.Stdin)
)
type Settings struct {
Verbose bool
RemoveExistingFiles bool
}
type Generators struct {
Name string
Identifier string
Filename string
}
type YamlStructure struct {
Settings Settings
Generators []Generators
}
func CreateEntry(filename string, existingIdentifiers []string) error {
if isInvalidString(filename) {
return fmt.Errorf("filename was empty or only whitespace")
}
newFilename, err := createNewTemplateFile(filename)
if err != nil {
return err
}
name := queryForString("Enter a descriptive name for the file", false)
identifier := queryForString("Enter an identifier for the file", false)
generator := Generators{
Name: name,
Identifier: identifier,
Filename: newFilename,
}
config := GetConfig()
config.Generators = append(config.Generators, generator)
return WriteConfig(config)
}
func queryForString(query string, readOnNextLine bool) string {
query = fmt.Sprintf("%s: ", query)
var queryEnding string
if readOnNextLine {
queryEnding = newlineSymbol()
}
prompt := fmt.Sprintf("%s%s", query, queryEnding)
for {
fmt.Print(prompt)
input, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Something went wrong when reading from the terminal")
os.Exit(1)
}
if isInvalidString(input) {
fmt.Println("Empty input is not allowed")
continue
}
return input
}
}
func isInvalidString(str string) bool {
return len(strings.TrimSpace(str)) == 0
}
func createNewTemplateFile(filename string) (string, error) {
bytes, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
}
newTemplateFile := path.Join(GetGogenTemplatesFolderPath(), filename)
err = ioutil.WriteFile(newTemplateFile, bytes, fs.ModePerm)
if err != nil {
return "", err
}
return newTemplateFile, nil
}
func GetConfig() YamlStructure {
settings := YamlStructure{}
config := readConfig()
err := yaml.Unmarshal([]byte(config), &settings)
exitOnError(err, "Something went wrong while trying to read the config yaml file")
return settings
}
func WriteConfig(yamlStructure YamlStructure) error {
text, err := yaml.Marshal(yamlStructure)
exitOnError(err, "Something went wrong while trying to marshal the yaml structure")
return overwriteYaml(text)
}
func overwriteYaml(content []byte) error {
configFile := GetGogenConfigFilePath()
err := os.Remove(configFile)
exitOnError(err, "could not remove old config file before writing new one")
return ioutil.WriteFile(configFile, content, os.ModePerm)
}
func readConfig() []byte {
downloadedConfig, existed := EnsureConfigFoldersExists()
if !existed {
return downloadedConfig
}
configFileComplete := GetGogenConfigFilePath()
config, err := os.ReadFile(configFileComplete)
exitOnError(err, "Could not read config file from user config directory.\n"+
"Please see if you have permission to read this file")
return config
}
func downloadTemplateConfigFile() []byte {
configFile := GetGogenConfigFilePath()
resp, err := http.Get("https://raw.githubusercontent.com/andreaswachs/gogen/main/config/config.yaml")
exitOnError(err, "Failed to download default config file.")
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
exitOnError(err, "Failed to read the downloaded default config file.")
os.WriteFile(configFile, body, os.ModePerm)
return body
}