refactor: added more comments to functions

many more are required but in too tired to do more
This commit is contained in:
Roxie Gibson 2021-08-02 08:02:18 +01:00
parent 38d8b51bd5
commit fa16ce20eb
Signed by untrusted user: roxxers
GPG Key ID: 5D0140EDEE123F4D
2 changed files with 19 additions and 2 deletions

View File

@ -12,16 +12,23 @@ import (
)
// Type aliases to make code hints easier to understand
// AppEnv is a map of the values in an apps env config
type AppEnv = map[string]string
// AppName is AppName
type AppName = string
// AppFile represents app env files on disk without reading the contents
type AppFile struct {
Path string
Server string
}
// AppFiles is a slice of appfiles
type AppFiles map[AppName]AppFile
// App reprents an app with its env file read into memory
type App struct {
Name AppName
Type string
@ -30,12 +37,14 @@ type App struct {
File AppFile
}
// StackName gets what the docker safe stack name is for the app
func (a App) StackName() string {
return SanitiseAppName(a.Name)
}
// SORTING TYPES
// ByServer sort a slice of Apps
type ByServer []App
func (a ByServer) Len() int { return len(a) }
@ -44,6 +53,7 @@ func (a ByServer) Less(i, j int) bool {
return strings.ToLower(a[i].File.Server) < strings.ToLower(a[j].File.Server)
}
// ByServerAndType sort a slice of Apps
type ByServerAndType []App
func (a ByServerAndType) Len() int { return len(a) }
@ -51,11 +61,11 @@ func (a ByServerAndType) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByServerAndType) Less(i, j int) bool {
if a[i].File.Server == a[j].File.Server {
return strings.ToLower(a[i].Type) < strings.ToLower(a[j].Type)
} else {
return strings.ToLower(a[i].File.Server) < strings.ToLower(a[j].File.Server)
}
return strings.ToLower(a[i].File.Server) < strings.ToLower(a[j].File.Server)
}
// ByType sort a slice of Apps
type ByType []App
func (a ByType) Len() int { return len(a) }
@ -64,6 +74,7 @@ func (a ByType) Less(i, j int) bool {
return strings.ToLower(a[i].Type) < strings.ToLower(a[j].Type)
}
// ByName sort a slice of Apps
type ByName []App
func (a ByName) Len() int { return len(a) }
@ -101,6 +112,7 @@ func newApp(env AppEnv, name string, appFile AppFile) (App, error) {
}, nil
}
// LoadAppFiles gets all app files for a given set of servers or all servers
func LoadAppFiles(servers ...string) (AppFiles, error) {
appFiles := make(AppFiles)
if len(servers) == 1 {
@ -146,6 +158,7 @@ func GetApp(apps AppFiles, name AppName) (App, error) {
return app, nil
}
// GetApps returns a slice of Apps with their env files read from a given slice of AppFiles
func GetApps(appFiles AppFiles) ([]App, error) {
var apps []App
for name := range appFiles {
@ -158,6 +171,7 @@ func GetApps(appFiles AppFiles) ([]App, error) {
return apps, nil
}
// CopyAppEnvSample copies the example env file for the app into the users env files
func CopyAppEnvSample(appType, appName, server string) error {
envSamplePath := path.Join(ABRA_DIR, "apps", appType, ".env.sample")
envSample, err := ioutil.ReadFile(envSamplePath)
@ -178,10 +192,12 @@ func CopyAppEnvSample(appType, appName, server string) error {
return nil
}
// SanitiseAppName makes a app name usable with Docker by replacing illegal characters
func SanitiseAppName(name string) string {
return strings.ReplaceAll(name, ".", "_")
}
// GetAppStatuses queries servers to check the deployment status of given apps
func GetAppStatuses(appFiles AppFiles) (map[string]string, error) {
servers := appFiles.GetServers()
ch := make(chan client.StackStatus, len(servers))

View File

@ -100,6 +100,7 @@ func getAllFoldersInDirectory(directory string) ([]string, error) {
return folders, nil
}
// EnsureAbraDirExists checks for the abra config folder and throws error if not
func EnsureAbraDirExists() error {
if _, err := os.Stat(ABRA_DIR); os.IsNotExist(err) {
if err := os.Mkdir(ABRA_DIR, 0777); err != nil {