Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
Housekeeping
Browse files Browse the repository at this point in the history
  • Loading branch information
mt1976 committed Jan 8, 2023
1 parent 9feeb58 commit 013f216
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 159 deletions.
24 changes: 12 additions & 12 deletions core/database_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func connect(mssqlConfig map[string]string) (*sql.DB, error) {
logs.Error("Connection attempt with "+database+connString+" failed:", err)
}
if database != "master" {
//logs.Success("Connected to " + server + " " + database)
// log.Println("Information : Connected to " + server + " " + database)
logs.Success("Connected to " + server + " " + database)
log.Println("Information : Connected to " + server + " " + database)
}
keepalive, _ := time.ParseDuration("-1h")
dbInstance.SetConnMaxLifetime(keepalive)
Expand Down Expand Up @@ -221,10 +221,10 @@ func Database_CreateObjects(DB *sql.DB, dbConfig map[string]string, sourcePath s
//spew.Dump(DB.Stats())
// Get a full list of all views
_, requiredViews, _ := GetDataList(sourcePath)
createTemplate, err := ReadDataFile("templateCreate.sql", "/config/database/templates")
if err != nil {
log.Println(err.Error())
}
// createTemplate, err := ReadDataFile("templateCreate.sql", "/config/database/templates")
// if err != nil {
// log.Println(err.Error())
// }
dropTemplate, err := ReadDataFile("templateDrop.sql", "/config/database/templates")
if err != nil {
log.Println(err.Error())
Expand All @@ -251,7 +251,7 @@ func Database_CreateObjects(DB *sql.DB, dbConfig map[string]string, sourcePath s
fmt.Printf("requiredViews: %v\n", requiredViews)
for _, view := range requiredViews {
thisDrop := dropTemplate
thisCreate := createTemplate
//thisCreate := createTemplate

//if the last 3 characters of requiredViews is "sql" then we are dealing with a sql file
if view[len(view)-3:] == "sql" {
Expand All @@ -274,17 +274,17 @@ func Database_CreateObjects(DB *sql.DB, dbConfig map[string]string, sourcePath s

//log.Println("***", sqlBody, "***")

thisCreate = ReplaceWildcard(thisCreate, "!SQL.DB", databaseName)
thisCreate = ReplaceWildcard(thisCreate, "!SQL.SCHEMA", schemaName)
thisCreate = ReplaceWildcard(thisCreate, "!SQL.VIEW", objectName)
thisCreate = ReplaceWildcard(thisCreate, "!SQL.SOURCE", parentschema)
// thisCreate = ReplaceWildcard(thisCreate, "!SQL.DB", databaseName)
// thisCreate = ReplaceWildcard(thisCreate, "!SQL.SCHEMA", schemaName)
// thisCreate = ReplaceWildcard(thisCreate, "!SQL.VIEW", objectName)
// thisCreate = ReplaceWildcard(thisCreate, "!SQL.SOURCE", parentschema)

sqlBody = ReplaceWildcard(sqlBody, "!SQL.DB", databaseName)
sqlBody = ReplaceWildcard(sqlBody, "!SQL.SCHEMA", schemaName)
sqlBody = ReplaceWildcard(sqlBody, "!SQL.VIEW", objectName)
sqlBody = ReplaceWildcard(sqlBody, "!SQL.SOURCE", parentschema)

thisCreate = ReplaceWildcard(thisCreate, "!SQL.BODY", sqlBody)
//thisCreate = ReplaceWildcard(thisCreate, "!SQL.BODY", sqlBody)
//fmt.Println("index:", i, fileID, thisDrop, thisCreate)
// fmt.Println("***************************************")
// fmt.Println(fileID, objectName)
Expand Down
12 changes: 6 additions & 6 deletions core/financialSupport.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ func Financial_AbbreviationsToInt(str string) int {
str = strings.ToUpper(str)
number := ""
number = strings.ReplaceAll(str, "$", "")
number = strings.ReplaceAll(str, "€", "")
number = strings.ReplaceAll(str, "£", "")
number = strings.ReplaceAll(number, "€", "")
number = strings.ReplaceAll(number, "£", "")
fact := strings.ToUpper(number[len(number)-1:])
number = strings.ReplaceAll(str, "M", "")
number = strings.ReplaceAll(str, "K", "")
number = strings.ReplaceAll(str, "T", "")
number = strings.ReplaceAll(str, "B", "")
number = strings.ReplaceAll(fact, "M", "")
number = strings.ReplaceAll(number, "K", "")
number = strings.ReplaceAll(number, "T", "")
number = strings.ReplaceAll(number, "B", "")

intNum, err := strconv.Atoi(number)
if err != nil {
Expand Down
46 changes: 23 additions & 23 deletions core/globals_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package core

import (
"database/sql"
"io/ioutil"
"os"
"strings"
"time"
Expand Down Expand Up @@ -137,34 +136,35 @@ func writeDataFile(fileName string, path string, content string) (bool, error) {
//log.Println("Write :", filePath)

message := []byte(content)
err := ioutil.WriteFile(filePath, message, 0644)
err := os.WriteFile(filePath, message, 0644)
if err != nil {
logs.Fatal("Write Error", err)
return false, err
}
return false, nil
}

func deleteDataFile(fileName string, path string) int {
pwd, _ := os.Getwd()
filePath := pwd + "/" + fileName
if len(path) != 0 {
filePath = pwd + path + "/" + fileName
}
//log.Println("Delete :", filePath)

// delete file

if fileExists(filePath) {
var err = os.Remove(filePath)
if err != nil {
logs.Fatal("File Error", err)
return -1
}
}
logs.Information("File Deleted", fileName+" - "+path)
return 1
}
// deleteDataFile deletes a file - For Future Use
// func deleteDataFile(fileName string, path string) int {
// pwd, _ := os.Getwd()
// filePath := pwd + "/" + fileName
// if len(path) != 0 {
// filePath = pwd + path + "/" + fileName
// }
// //log.Println("Delete :", filePath)

// // delete file

// if fileExists(filePath) {
// var err = os.Remove(filePath)
// if err != nil {
// logs.Fatal("File Error", err)
// return -1
// }
// }
// logs.Information("File Deleted", fileName+" - "+path)
// return 1
// }

func copyDataFile(fileName string, fromPath string, toPath string) bool {

Expand Down Expand Up @@ -194,7 +194,7 @@ func GetDataList(basePath string) (int, []string, error) {
// log.Println(basePath, kind, direction, requestPath)
pwd, _ := os.Getwd()
//logs.Accessing(pwd + basePath)
files, err := ioutil.ReadDir(pwd + basePath)
files, err := os.ReadDir(pwd + basePath)
if err != nil {
logs.Fatal("Directory Error", err)
}
Expand Down
16 changes: 16 additions & 0 deletions core/math_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,19 @@ func FloatToString(in float64) string {
out := strconv.FormatFloat(in, 'f', 2, 64)
return out
}

// Max returns the larger of x or y.
func Max(x, y int) int {
if x < y {
return y
}
return x
}

// Min returns the smaller of x or y.
func Min(x, y int) int {
if x > y {
return y
}
return x
}
12 changes: 6 additions & 6 deletions core/strings_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ func ReplaceWildcard(orig string, replaceThis string, withThis string) string {
* number of times
*
*/
func leftPad(s string, padStr string, pLen int) string {
return strings.Repeat(padStr, pLen) + s
}
func rightPad(s string, padStr string, pLen int) string {
return s + strings.Repeat(padStr, pLen)
}
// func leftPad(s string, padStr string, pLen int) string {
// return strings.Repeat(padStr, pLen) + s
// }
// func rightPad(s string, padStr string, pLen int) string {
// return s + strings.Repeat(padStr, pLen)
// }

/* the Pad2Len functions are generally assumed to be padded with short sequences of strings
* in many cases with a single character sequence
Expand Down
128 changes: 16 additions & 112 deletions core/support_impl.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package core

import (
"bytes"
"errors"
"fmt"
"io/fs"
"io/ioutil"
"log"
"net"
"net/http"
Expand All @@ -19,34 +17,18 @@ import (
"github.com/mt1976/ebEstimates/logs"
)

// Converts a date, as a user readable date
func dateToUserDate(in string) string {
// // Converts a date, as a user readable date
// func dateToUserDate(in string) string {

t, err := time.Parse(DATEFORMAT, in)
if err != nil {
log.Println(err.Error())
}
// t, err := time.Parse(DATEFORMAT, in)
// if err != nil {
// log.Println(err.Error())
// }

ext := t.Format(DATEFORMATUSER)

return ext
}

// Max returns the larger of x or y.
func Max(x, y int) int {
if x < y {
return y
}
return x
}
// ext := t.Format(DATEFORMATUSER)

// Min returns the smaller of x or y.
func Min(x, y int) int {
if x > y {
return y
}
return x
}
// return ext
// }

// GetURLparam returns a selected parmeter value from the a given URI
func GetURLparam(r *http.Request, paramID string) string {
Expand Down Expand Up @@ -143,7 +125,7 @@ func GetNavigationID(inUserRole string) string {
//log.Println("Testing", templateName, FileExists(templateName))
//log.Println("Testing", roleTemplate, FileExists(roleTemplate))
if FileExists(roleTemplate) {
//templateName = roleTemplate
templateName = roleTemplate
}
//log.Println("NAVIGATION", templateName)
return templateName
Expand All @@ -158,84 +140,6 @@ func FileExists(filename string) bool {
return !info.IsDir()
}

// ipRange - a structure that holds the start and end of a range of ip addresses
type ipRange struct {
start net.IP
end net.IP
}

// inRange - check to see if a given ip address is within a range given
func inRange(r ipRange, ipAddress net.IP) bool {
// strcmp type byte comparison
if bytes.Compare(ipAddress, r.start) >= 0 && bytes.Compare(ipAddress, r.end) < 0 {
return true
}
return false
}

var privateRanges = []ipRange{
ipRange{
start: net.ParseIP("10.0.0.0"),
end: net.ParseIP("10.255.255.255"),
},
ipRange{
start: net.ParseIP("100.64.0.0"),
end: net.ParseIP("100.127.255.255"),
},
ipRange{
start: net.ParseIP("172.16.0.0"),
end: net.ParseIP("172.31.255.255"),
},
ipRange{
start: net.ParseIP("192.0.0.0"),
end: net.ParseIP("192.0.0.255"),
},
ipRange{
start: net.ParseIP("192.168.0.0"),
end: net.ParseIP("192.168.255.255"),
},
ipRange{
start: net.ParseIP("198.18.0.0"),
end: net.ParseIP("198.19.255.255"),
},
}

// isPrivateSubnet - check to see if this ip is in a private subnet
func isPrivateSubnet(ipAddress net.IP) bool {
// my use case is only concerned with ipv4 atm
if ipCheck := ipAddress.To4(); ipCheck != nil {
// iterate over all our ranges
for _, r := range privateRanges {
// check if this ip is in a private range
if inRange(r, ipAddress) {
return true
}
}
}
return false
}

// GetIPAdress returns the current IP address
func GetIPAdress(r *http.Request) string {
var ipAddress string
for _, h := range []string{"X-Forwarded-For", "X-Real-Ip"} {
for _, ip := range strings.Split(r.Header.Get(h), ",") {
// header can contain spaces too, strip those out.
ip = strings.TrimSpace(ip)
realIP := net.ParseIP(ip)
if !realIP.IsGlobalUnicast() || isPrivateSubnet(realIP) {
// bad address, go to next
continue
} else {
ipAddress = ip
goto Done
}
}
}
Done:
return ipAddress
}

// ReadUserIP returns the end-users IP address
func ReadUserIP(r *http.Request) string {
IPAddress := r.Header.Get("X-Real-Ip")
Expand Down Expand Up @@ -323,16 +227,16 @@ func ReadDataFile(fileName string, path string) (string, error) {
//log.Println("Read :", filePath)
// Read entire file content, giving us little control but
// making it very simple. No need to close the file.
content, err := ioutil.ReadFile(filePath)
content, err := os.ReadFile(filePath)
if err != nil {
log.Fatal(err)
}
// Convert []byte to string and print to screen
return string(content), err
}

func GetDirectoryContentAbsolute(path string) ([]fs.FileInfo, error) {
files, err := ioutil.ReadDir(path + "/")
func GetDirectoryContentAbsolute(path string) ([]fs.DirEntry, error) {
files, err := os.ReadDir(path + "/")
if err != nil {
log.Fatal(err)
}
Expand All @@ -356,7 +260,7 @@ func ReadDataFileAbsolute(fileName string, path string) ([]byte, string, error)
//log.Println("Read :", filePath)
// Read entire file content, giving us little control but
// making it very simple. No need to close the file.
content, err := ioutil.ReadFile(absFileName)
content, err := os.ReadFile(absFileName)
if err != nil {
log.Fatal(err)
}
Expand All @@ -369,7 +273,7 @@ func FileSystem_WriteData_Absolute(fileName string, path string, content string)
absFileName := path + "/" + fileName

message := []byte(content)
err := ioutil.WriteFile(absFileName, message, 0644)
err := os.WriteFile(absFileName, message, 0644)
if err != nil {
log.Fatal(err)
return -1
Expand Down Expand Up @@ -553,7 +457,7 @@ func SetApplicationSQLDatabase(db string) {
}

func DataLoaderArtifactRepository() string {
return DataLoaderArtifactRepository()
return ""
}

func OBSOLETE_MessageQueueDeliveryPath() string {
Expand Down

0 comments on commit 013f216

Please sign in to comment.