utils

package module
v0.0.0-...-df1f087 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2019 License: MIT Imports: 8 Imported by: 0

README

Go Report Card License GitHub last commit GitHub contributors Dependent repos (via libraries.io)

Go Utils

This repository contains the utils library, a set of useful tools to be used in go projects.

Getting Started

currently the utils has the below utility functions:

  • NewLog = it is used to save logs in a log file. Allowing you to choose the log file destination.
  • CopyFile = it copies a file from src to dst.
  • MsSQLSendAlarm = it can be used to send predefined alarms in the form of inserting text to tables in MSSQL DB used for Alarm notifications.
Installing

install using:

go get github.com/nadim-khalil/go-utils
How to use
NewLog

NewLog is used to save logs in a log file.

example:

if err != nil {
	logpath := "errors.log"
	utils.NewLog(logpath)
	utils.Log.Printf("error in xxx func: %v \r\n", err)
}

CopyFile

CopyFile copies a file from src to dst. If src and dst files exist, and are the same, then return success. Otherwise, attempt to create a hard link between the two files. If that fail, copy the file contents from src to dst.

example:

//to := "source file"
//from := "destination file"

fmt.Printf("Copying %s from %s\n", to, from)
err := utils.CopyFile(from, to)
if err != nil {
	fmt.Printf("CopyFile failed %q\n", err)
	logpath := "errors.log"
	utils.NewLog(logpath)
	utils.Log.Printf("CopyFile failed")
} else {
	fmt.Printf("CopyFile succeeded\n")
}
ChecksumCompare

ChecksumCompare can used to compare checksum of 2 files to make sure the files are being copied without any tampering

example:

//to := "source file"
//from := "destination file"

fmt.Printf("Checksum comparison %s and %s\n", to, from)
checksum, err := utils.ChecksumCompare(to, from)
if err != nil {
	fmt.Printf("ChecksumCompare failed %q\n", err)
	logpath := "errors.log"
	utils.NewLog(logpath)
	utils.Log.Printf("error detected, ChecksumCompare failed")
} else {
	if checksum {
		log.Println("Checksum comparison SUCCESS the 2 files are identical \n")
	} else {
		fmt.Printf("Checksum comparison FAIL the 2 files are not identical \n")
	}
}
MsSQLSendAlarm

MsSQLSendAlarm can be used to send predefined alarms in the form of inserting text to tables in MSSQL DB used for Alarm notifications

example:


//fill the variables in case you want to send Alarms to an SQL DB used for Alarms notifications
iniConnString := "server=x.x.x.x;user id=sa;password=xxxxx;port=1433"
iniSQLQuery := "insert into [x.x.x.x].Alarms.dbo.xxx_tbl (number,date,text,flag_sent) select 'xx',getdate(),'Failed xxxx',0"

if err != nil {

	fmt.Printf("error in getCurrentDir func: %v \r\n", err)
	if iniConnString != "" && iniSQLQuery != "" {
		utils.MsSQLSendAlarm(iniConnString, iniSQLQuery)
	}
}

Author

  • Nadim Khalil - My personal site - nadim.tk

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments

  • Hat tip to anyone whose code was used

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Log *log.Logger
)

Log

Functions

func ChecksumCompare

func ChecksumCompare(s, d string) (b bool, err error)

ChecksumCompare used to compare checksum of 2 files to make sure the files are being copied without any tampering

func CopyFile

func CopyFile(src, dst string) (err error)

CopyFile copies a file from src to dst. If src and dst files exist, and are the same, then return success. Otherise, attempt to create a hard link between the two files. If that fail, copy the file contents from src to dst.

func MsSQLSendAlarm

func MsSQLSendAlarm(_connString string, _query string)

MsSQLSendAlarm can be used to send predefined alarms in the form of inserting text to tables in MSSQL DB used for Alarm notifications _connString sample: "server=x.x.x.x;user id=sa;password=xxxxx;port=1433" _query sample: "insert into [x.x.x.x].Alarms.dbo.xxx_tbl (number,date,text,flag_sent) select 'xx',getdate(),'Failed xxxx',0"

func NewLog

func NewLog(logpath string)

NewLog is used to save logs in a log file

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL