initial
This commit is contained in:
303
internal/report/create.go
Normal file
303
internal/report/create.go
Normal file
@@ -0,0 +1,303 @@
|
||||
package report
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
"wnzl-snow/db"
|
||||
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
func CreateInventoryReport(logger *slog.Logger, Database db.Database, ctx context.Context) ([]byte, error) {
|
||||
//var xlsx *excelize.File
|
||||
sheetName := "Inventory Report"
|
||||
var buffer bytes.Buffer
|
||||
var cell string
|
||||
|
||||
logger.Debug("Querying inventory table")
|
||||
results, err := Database.Queries().GetReportInventory(ctx)
|
||||
if err != nil {
|
||||
logger.Error("Unable to query inventory table", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(results) == 0 {
|
||||
logger.Error("Empty inventory results")
|
||||
return nil, fmt.Errorf("Empty inventory results")
|
||||
}
|
||||
|
||||
// Create excel workbook
|
||||
xlsx := excelize.NewFile()
|
||||
err = xlsx.SetSheetName("Sheet1", sheetName)
|
||||
if err != nil {
|
||||
logger.Error("Error setting sheet name", "error", err, "sheet_name", sheetName)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Set the document properties
|
||||
err = xlsx.SetDocProps(&excelize.DocProperties{
|
||||
Creator: "json2excel",
|
||||
Created: time.Now().Format(time.RFC3339),
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("Error setting document properties", "error", err, "sheet_name", sheetName)
|
||||
}
|
||||
|
||||
// Use reflection to determine column headings from the first item
|
||||
firstItem := results[0]
|
||||
v := reflect.ValueOf(firstItem)
|
||||
typeOfItem := v.Type()
|
||||
|
||||
// Create column headers dynamically
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
column := string(rune('A'+i)) + "1" // A1, B1, C1, etc.
|
||||
xlsx.SetCellValue(sheetName, column, typeOfItem.Field(i).Name)
|
||||
}
|
||||
|
||||
// Set autofilter on heading row
|
||||
cell, _ = excelize.CoordinatesToCellName(v.NumField(), 1)
|
||||
filterRange := "A1:" + cell
|
||||
logger.Debug("Setting autofilter", "range", filterRange)
|
||||
// As per docs any filters applied need to be manually processed by us (eg hiding rows with blanks)
|
||||
err = xlsx.AutoFilter(sheetName, filterRange, nil)
|
||||
if err != nil {
|
||||
logger.Error("Error setting autofilter", "error", err)
|
||||
}
|
||||
|
||||
// Bold top row
|
||||
headerStyle, err := xlsx.NewStyle(&excelize.Style{
|
||||
Font: &excelize.Font{
|
||||
Bold: true,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("Error generating header style", "error", err)
|
||||
} else {
|
||||
err = xlsx.SetRowStyle(sheetName, 1, 1, headerStyle)
|
||||
if err != nil {
|
||||
logger.Error("Error setting header style", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Populate the Excel file with data from the Inventory table
|
||||
for i, item := range results {
|
||||
v = reflect.ValueOf(item)
|
||||
for j := 0; j < v.NumField(); j++ {
|
||||
column := string(rune('A'+j)) + strconv.Itoa(i+2) // Start from row 2
|
||||
value := getFieldValue(v.Field(j))
|
||||
xlsx.SetCellValue(sheetName, column, value)
|
||||
}
|
||||
}
|
||||
|
||||
// Freeze top row
|
||||
err = xlsx.SetPanes(sheetName, &excelize.Panes{
|
||||
Freeze: true,
|
||||
Split: false,
|
||||
XSplit: 0,
|
||||
YSplit: 1,
|
||||
TopLeftCell: "A2",
|
||||
ActivePane: "bottomLeft",
|
||||
Selection: []excelize.Selection{
|
||||
{SQRef: "A2", ActiveCell: "A2", Pane: "bottomLeft"},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("Error freezing top row", "error", err)
|
||||
}
|
||||
|
||||
// Set column autowidth
|
||||
/*
|
||||
err = SetColAutoWidth(xlsx, sheetName)
|
||||
if err != nil {
|
||||
fmt.Printf("Error setting auto width : '%s'\n", err)
|
||||
}
|
||||
*/
|
||||
|
||||
// Save the Excel file into a byte buffer
|
||||
if err := xlsx.Write(&buffer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return buffer.Bytes(), nil
|
||||
}
|
||||
|
||||
func CreateUpdatesReport(logger *slog.Logger, Database db.Database, ctx context.Context) ([]byte, error) {
|
||||
//var xlsx *excelize.File
|
||||
sheetName := "Updates Report"
|
||||
var buffer bytes.Buffer
|
||||
var cell string
|
||||
|
||||
logger.Debug("Querying updates table")
|
||||
results, err := Database.Queries().GetReportUpdates(ctx)
|
||||
if err != nil {
|
||||
logger.Error("Unable to query updates table", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(results) == 0 {
|
||||
logger.Error("Empty updates results")
|
||||
return nil, fmt.Errorf("Empty updates results")
|
||||
}
|
||||
|
||||
// Create excel workbook
|
||||
xlsx := excelize.NewFile()
|
||||
err = xlsx.SetSheetName("Sheet1", sheetName)
|
||||
if err != nil {
|
||||
logger.Error("Error setting sheet name", "error", err, "sheet_name", sheetName)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Set the document properties
|
||||
err = xlsx.SetDocProps(&excelize.DocProperties{
|
||||
Creator: "json2excel",
|
||||
Created: time.Now().Format(time.RFC3339),
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("Error setting document properties", "error", err, "sheet_name", sheetName)
|
||||
}
|
||||
|
||||
// Use reflection to determine column headings from the first item
|
||||
firstItem := results[0]
|
||||
v := reflect.ValueOf(firstItem)
|
||||
typeOfItem := v.Type()
|
||||
|
||||
// Create column headers dynamically
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
column := string(rune('A'+i)) + "1" // A1, B1, C1, etc.
|
||||
xlsx.SetCellValue(sheetName, column, typeOfItem.Field(i).Name)
|
||||
}
|
||||
|
||||
// Set autofilter on heading row
|
||||
cell, _ = excelize.CoordinatesToCellName(v.NumField(), 1)
|
||||
filterRange := "A1:" + cell
|
||||
logger.Debug("Setting autofilter", "range", filterRange)
|
||||
// As per docs any filters applied need to be manually processed by us (eg hiding rows with blanks)
|
||||
err = xlsx.AutoFilter(sheetName, filterRange, nil)
|
||||
if err != nil {
|
||||
logger.Error("Error setting autofilter", "error", err)
|
||||
}
|
||||
|
||||
// Bold top row
|
||||
headerStyle, err := xlsx.NewStyle(&excelize.Style{
|
||||
Font: &excelize.Font{
|
||||
Bold: true,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("Error generating header style", "error", err)
|
||||
} else {
|
||||
err = xlsx.SetRowStyle(sheetName, 1, 1, headerStyle)
|
||||
if err != nil {
|
||||
logger.Error("Error setting header style", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Populate the Excel file with data from the Inventory table
|
||||
for i, item := range results {
|
||||
v = reflect.ValueOf(item)
|
||||
for j := 0; j < v.NumField(); j++ {
|
||||
column := string(rune('A'+j)) + strconv.Itoa(i+2) // Start from row 2
|
||||
value := getFieldValue(v.Field(j))
|
||||
xlsx.SetCellValue(sheetName, column, value)
|
||||
}
|
||||
}
|
||||
|
||||
// Freeze top row
|
||||
err = xlsx.SetPanes(sheetName, &excelize.Panes{
|
||||
Freeze: true,
|
||||
Split: false,
|
||||
XSplit: 0,
|
||||
YSplit: 1,
|
||||
TopLeftCell: "A2",
|
||||
ActivePane: "bottomLeft",
|
||||
Selection: []excelize.Selection{
|
||||
{SQRef: "A2", ActiveCell: "A2", Pane: "bottomLeft"},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("Error freezing top row", "error", err)
|
||||
}
|
||||
|
||||
// Set column autowidth
|
||||
/*
|
||||
err = SetColAutoWidth(xlsx, sheetName)
|
||||
if err != nil {
|
||||
fmt.Printf("Error setting auto width : '%s'\n", err)
|
||||
}
|
||||
*/
|
||||
|
||||
// Save the Excel file into a byte buffer
|
||||
if err := xlsx.Write(&buffer); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return buffer.Bytes(), nil
|
||||
}
|
||||
|
||||
// Helper function to get the actual value of sql.Null types
|
||||
func getFieldValue(field reflect.Value) interface{} {
|
||||
switch field.Kind() {
|
||||
case reflect.Struct:
|
||||
// Handle sql.Null types based on their concrete type
|
||||
switch field.Interface().(type) {
|
||||
case sql.NullString:
|
||||
ns := field.Interface().(sql.NullString)
|
||||
if ns.Valid {
|
||||
return ns.String
|
||||
}
|
||||
return ""
|
||||
case sql.NullInt64:
|
||||
ni := field.Interface().(sql.NullInt64)
|
||||
if ni.Valid {
|
||||
return ni.Int64
|
||||
}
|
||||
return -1
|
||||
case sql.NullFloat64:
|
||||
nf := field.Interface().(sql.NullFloat64)
|
||||
if nf.Valid {
|
||||
return nf.Float64
|
||||
}
|
||||
return nil
|
||||
case sql.NullBool:
|
||||
nb := field.Interface().(sql.NullBool)
|
||||
if nb.Valid {
|
||||
return nb.Bool
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
return field.Interface() // Return the value as-is for non-sql.Null types
|
||||
}
|
||||
|
||||
// Taken from https://github.com/qax-os/excelize/issues/92#issuecomment-821578446
|
||||
func SetColAutoWidth(xlsx *excelize.File, sheetName string) error {
|
||||
// Autofit all columns according to their text content
|
||||
cols, err := xlsx.GetCols(sheetName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for idx, col := range cols {
|
||||
largestWidth := 0
|
||||
for _, rowCell := range col {
|
||||
cellWidth := utf8.RuneCountInString(rowCell) + 2 // + 2 for margin
|
||||
if cellWidth > largestWidth {
|
||||
largestWidth = cellWidth
|
||||
}
|
||||
}
|
||||
//fmt.Printf("SetColAutoWidth calculated largest width for column index '%d' is '%d'\n", idx, largestWidth)
|
||||
name, err := excelize.ColumnNumberToName(idx + 1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
xlsx.SetColWidth(sheetName, name, name, float64(largestWidth))
|
||||
}
|
||||
// No errors at this point
|
||||
return nil
|
||||
}
|
80
internal/secrets/secrets.go
Normal file
80
internal/secrets/secrets.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package secrets
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"io"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
type Secrets struct {
|
||||
Logger *slog.Logger
|
||||
EncryptionKey []byte
|
||||
}
|
||||
|
||||
func New(logger *slog.Logger, key []byte) *Secrets {
|
||||
return &Secrets{
|
||||
Logger: logger,
|
||||
EncryptionKey: key,
|
||||
}
|
||||
}
|
||||
|
||||
// Encrypt function that encrypts data using AES256-GCM and returns base64 encoded ciphertext
|
||||
func (s *Secrets) Encrypt(plainText []byte) (string, error) {
|
||||
block, err := aes.NewCipher(s.EncryptionKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Create a new GCM cipher
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Create a nonce
|
||||
nonce := make([]byte, gcm.NonceSize())
|
||||
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Encrypt the plaintext using AES256-GCM
|
||||
cipherText := gcm.Seal(nonce, nonce, plainText, nil)
|
||||
|
||||
// Return the base64 encoded ciphertext
|
||||
return base64.StdEncoding.EncodeToString(cipherText), nil
|
||||
}
|
||||
|
||||
// Decrypt function that decrypts base64 encoded AES256-GCM ciphertext
|
||||
func (s *Secrets) Decrypt(base64CipherText string) ([]byte, error) {
|
||||
// Decode the base64 ciphertext
|
||||
cipherText, err := base64.StdEncoding.DecodeString(base64CipherText)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(s.EncryptionKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create a new GCM cipher
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Extract the nonce from the ciphertext
|
||||
nonceSize := gcm.NonceSize()
|
||||
nonce, cipherText := cipherText[:nonceSize], cipherText[nonceSize:]
|
||||
|
||||
// Decrypt the ciphertext
|
||||
plainText, err := gcm.Open(nil, nonce, cipherText, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return plainText, nil
|
||||
}
|
67
internal/settings/settings.go
Normal file
67
internal/settings/settings.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"wnzl-snow/internal/utils"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Settings struct {
|
||||
SettingsPath string
|
||||
Logger *slog.Logger
|
||||
Values *SettingsYML
|
||||
}
|
||||
|
||||
// SettingsYML struct holds various runtime data that is too cumbersome to specify via command line, eg replacement properties
|
||||
type SettingsYML struct {
|
||||
Settings struct {
|
||||
TenantsToFilter []string `yaml:"tenants_to_filter"`
|
||||
NodeChargeClusters []string `yaml:"node_charge_clusters"`
|
||||
SrmActiveActiveVms []string `yaml:"srm_activeactive_vms"`
|
||||
VcenterAddresses []string `yaml:"vcenter_addresses"`
|
||||
} `yaml:"settings"`
|
||||
}
|
||||
|
||||
func New(logger *slog.Logger, settingsPath string) *Settings {
|
||||
return &Settings{
|
||||
SettingsPath: utils.GetFilePath(settingsPath),
|
||||
Logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Settings) ReadYMLSettings() error {
|
||||
// Create config structure
|
||||
var settings SettingsYML
|
||||
|
||||
// Check for empty filename
|
||||
if len(s.SettingsPath) == 0 {
|
||||
return errors.New("settings file path not specified")
|
||||
}
|
||||
|
||||
//path := utils.GetFilePath(settingsPath)
|
||||
|
||||
// Open config file
|
||||
file, err := os.Open(s.SettingsPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to open settings file : '%s'", err)
|
||||
}
|
||||
s.Logger.Debug("Opened settings yaml file", "file_path", s.SettingsPath)
|
||||
defer file.Close()
|
||||
|
||||
// Init new YAML decode
|
||||
d := yaml.NewDecoder(file)
|
||||
|
||||
// Start YAML decoding from file
|
||||
if err := d.Decode(&settings); err != nil {
|
||||
return fmt.Errorf("unable to decode settings file : '%s'", err)
|
||||
}
|
||||
|
||||
s.Logger.Debug("Updating settings", "settings", settings)
|
||||
s.Values = &settings
|
||||
|
||||
return nil
|
||||
}
|
15
internal/tasks/tasks.go
Normal file
15
internal/tasks/tasks.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"wnzl-snow/db"
|
||||
"wnzl-snow/internal/settings"
|
||||
)
|
||||
|
||||
// CronTask stores runtime information to be used by tasks
|
||||
type CronTask struct {
|
||||
Logger *slog.Logger
|
||||
Database db.Database
|
||||
Settings *settings.Settings
|
||||
//VcCreds *vcenter.VcenterLogin
|
||||
}
|
139
internal/utils/certOperations.go
Normal file
139
internal/utils/certOperations.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/pem"
|
||||
"log"
|
||||
"math/big"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GenerateCerts(tlsCert string, tlsKey string) {
|
||||
// @see https://shaneutt.com/blog/golang-ca-and-signed-cert-go/
|
||||
// @see https://golang.org/src/crypto/tls/generate_cert.go
|
||||
validFrom := ""
|
||||
validFor := 365 * 24 * time.Hour
|
||||
isCA := true
|
||||
|
||||
// Get the hostname
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Check that the directory exists
|
||||
relativePath := filepath.Dir(tlsCert)
|
||||
log.Printf("GenerateCerts relative path for file creation is '%s'\n", relativePath)
|
||||
_, err = os.Stat(relativePath)
|
||||
if os.IsNotExist(err) {
|
||||
log.Printf("Certificate path does not exist, creating %s before generating certificate\n", relativePath)
|
||||
os.MkdirAll(relativePath, os.ModePerm)
|
||||
}
|
||||
|
||||
// Generate a private key
|
||||
priv, err := rsa.GenerateKey(rand.Reader, rsaBits)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to generate private key: %v", err)
|
||||
}
|
||||
|
||||
var notBefore time.Time
|
||||
if len(validFrom) == 0 {
|
||||
notBefore = time.Now()
|
||||
} else {
|
||||
notBefore, err = time.Parse("Jan 2 15:04:05 2006", validFrom)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse creation date: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
notAfter := notBefore.Add(validFor)
|
||||
|
||||
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
||||
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to generate serial number: %v", err)
|
||||
}
|
||||
|
||||
template := x509.Certificate{
|
||||
SerialNumber: serialNumber,
|
||||
Subject: pkix.Name{
|
||||
Organization: []string{"DTMS"},
|
||||
},
|
||||
NotBefore: notBefore,
|
||||
NotAfter: notAfter,
|
||||
|
||||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
||||
BasicConstraintsValid: true,
|
||||
}
|
||||
|
||||
template.DNSNames = append(template.DNSNames, hostname)
|
||||
|
||||
// Add in all the non-local IPs
|
||||
ifaces, err := net.Interfaces()
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Error enumerating interfaces: %v\n", err)
|
||||
}
|
||||
|
||||
for _, i := range ifaces {
|
||||
addrs, err := i.Addrs()
|
||||
if err != nil {
|
||||
log.Printf("Oops: %v\n", err)
|
||||
}
|
||||
|
||||
for _, address := range addrs {
|
||||
// check the address type and if it is not a loopback then add it to the list
|
||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil {
|
||||
template.IPAddresses = append(template.IPAddresses, ipnet.IP)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if isCA {
|
||||
template.IsCA = true
|
||||
template.KeyUsage |= x509.KeyUsageCertSign
|
||||
}
|
||||
|
||||
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create certificate: %v", err)
|
||||
}
|
||||
|
||||
certOut, err := os.Create(tlsCert)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open %s for writing: %v", tlsCert, err)
|
||||
}
|
||||
if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
|
||||
log.Fatalf("Failed to write data to %s: %v", tlsCert, err)
|
||||
}
|
||||
if err := certOut.Close(); err != nil {
|
||||
log.Fatalf("Error closing %s: %v", tlsCert, err)
|
||||
}
|
||||
log.Printf("wrote %s\n", tlsCert)
|
||||
|
||||
keyOut, err := os.OpenFile(tlsKey, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open %s for writing: %v", tlsKey, err)
|
||||
return
|
||||
}
|
||||
privBytes, err := x509.MarshalPKCS8PrivateKey(priv)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to marshal private key: %v", err)
|
||||
}
|
||||
if err := pem.Encode(keyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}); err != nil {
|
||||
log.Fatalf("Failed to write data to %s: %v", tlsKey, err)
|
||||
}
|
||||
if err := keyOut.Close(); err != nil {
|
||||
log.Fatalf("Error closing %s: %v", tlsKey, err)
|
||||
}
|
||||
log.Printf("wrote %s\n", tlsKey)
|
||||
}
|
68
internal/utils/utils.go
Normal file
68
internal/utils/utils.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
const rsaBits = 4096
|
||||
|
||||
func GetFilePath(path string) string {
|
||||
// Check for empty filename
|
||||
if len(path) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
// check if filename exists
|
||||
if _, err := os.Stat(path); os.IsNotExist((err)) {
|
||||
slog.Info("File not found, searching in same directory as binary", "filename", path)
|
||||
// if not, check that it exists in the same directory as the currently executing binary
|
||||
ex, err2 := os.Executable()
|
||||
if err2 != nil {
|
||||
slog.Error("Error determining binary path", "error", err)
|
||||
return ""
|
||||
}
|
||||
binaryPath := filepath.Dir(ex)
|
||||
path = filepath.Join(binaryPath, path)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// Get preferred outbound ip of this machine
|
||||
// @see https://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go
|
||||
func GetOutboundIP() net.IP {
|
||||
conn, err := net.Dial("udp", "8.8.8.8:80")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
||||
|
||||
return localAddr.IP
|
||||
}
|
||||
|
||||
// Check if a file exists from https://stackoverflow.com/questions/12518876/how-to-check-if-a-file-exists-in-go
|
||||
func FileExists(filename string) bool {
|
||||
info, err := os.Stat(filename)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return !info.IsDir()
|
||||
}
|
||||
|
||||
func SleepWithContext(ctx context.Context, d time.Duration) {
|
||||
timer := time.NewTimer(d)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if !timer.Stop() {
|
||||
<-timer.C
|
||||
}
|
||||
case <-timer.C:
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user