TSNotify Series 15 - Windows Task Scheduler Event ID Based Notifier

Опубликовано: 12 Март 2025
на канале: Maharlikans Code
50
6

In this TSNotify Series 15, we will learn how to trigger email notification based on the Windows Task Scheduler Event ID, getting prepared the necessary package dependencies such as Cobra CLI with step by step guide in Golang programming language.

#MaharlikansCode
#TSNotifySeries15
#TaskSchedulerEventIDNotify
#Golang
#LifeAsSoftwareDeveloper
#Maharlikans
#FilipinoSoftwareDeveloper

If you go with extra mile for buying me a cup of coffee, I appreciate it guys: https://ko-fi.com/maharlikanscode

Source Codes:
cmd/license.go:
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
"tsnotify/config"
"github.com/fatih/color"
"github.com/itrepablik/itrlog"
"github.com/itrepablik/lisensya"
"github.com/spf13/cobra"
)
var HostName string = ""
var getCmd = &cobra.Command{
Use: "license",
Short: "License related commands.",
Long: `The list of possible license related commands:

LIST OF AVAILABLE COMMANDS:
tsnotify license new
tsnotify license revoke
`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
HostName, err := lisensya.GetHostName()
if err != nil {
itrlog.Fatal("error getting hostname: ", err)
}
argMain := args[0]
switch argMain {
case "new":
isUserAuth := PromptAccount()
if isUserAuth {
checkPaidLicKey, err := VerifyLicenseKey("Yes")
if err != nil {
msg := `error generating new license key: ` + err.Error()
color.Red(msg)
itrlog.Fatal(msg)
}
var expiryInDays int = config.LicenseExpiryInDays
if checkPaidLicKey {
expiryInDays = 0
}
isNewLicenseOK, err := lisensya.GenerateLicenseKey(config.APITSNotifyGenLicenseKey, config.AppName,
config.MyEncryptDecryptSK, config.LicenseExpiryDelimiter, expiryInDays,
HostName, itrUserName, config.CLISignature)
if err != nil {
msg := `error generating new license key: ` + err.Error()
color.Red(msg)
itrlog.Fatal(msg, err)
}
if isNewLicenseOK {
msgSuccess := `You've successfully generated your tsnotify's license key, you can now use this software.`
itrlog.Infow(msgSuccess, "log_time", time.Now().Format(logDTFormat))
fmt.Println(msgSuccess)
} else {
msgErr := `Oops!, possible issues while generating new license keys`
itrlog.Errorw(msgErr, "log_time", time.Now().Format(logDTFormat))
fmt.Println(msgErr)
}
} else {
msgErr := `Either your username or password is wrong or account doesn't exist at all`
itrlog.Errorw(msgErr, "log_time", time.Now().Format(logDTFormat))
fmt.Println(msgErr)
}
case "revoke":
isUserAuth := PromptAccount()
if isUserAuth {
licenseKey, err := lisensya.ReadLicenseKey(config.AppName)
if err != nil {
itrlog.Fatalw("error getting current license key: ", err)
}
rLicKey, err := lisensya.RevokeLicenseKey(config.APITSNotifyRevokeLicenseKey, config.AppName, HostName, licenseKey, itrUserName, config.CLISignature)
if err != nil {
itrlog.Fatalw("error revoking your existing license key: ", err)
}
if rLicKey {
msgSuccess := `Successfully revoked your current tsnotify's license key.`
itrlog.Infow(msgSuccess, "log_time", time.Now().Format(logDTFormat))
fmt.Println(msgSuccess)
} else {
msgErr := `Oops!, so far probably you don't have any existing license key to be revoked.`
itrlog.Errorw(msgErr, "log_time", time.Now().Format(logDTFormat))
fmt.Println(msgErr)
}
} else {
msgErr := `Either your username or password is wrong or account doesn't exist at all`
itrlog.Errorw(msgErr, "log_time", time.Now().Format(logDTFormat))
fmt.Println(msgErr)
}
default:
fmt.Println("Invalid command, please type 'tsnotify license -h' to see the list of available commands")
}
},
}

func init() {
rootCmd.AddCommand(getCmd)
}

func VerifyLicenseKey(isPaidLicenseKey string) (bool, error) {
message := map[string]interface{}{
"username": itrUserName,
"isPaidLicenseKey": isPaidLicenseKey,
"cli_signature": config.CLISignature,
}
bytesRepresentation, err := json.Marshal(message)
if err != nil {
return false, err
}
resp, err := http.Post(config.APIVerifyLicenseKey, "application/json", bytes.NewBuffer(bytesRepresentation))
if err != nil {
return false, err
}
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
i := result["isSuccess"]
mStatus := fmt.Sprint(i)
isSuccess, _ := strconv.ParseBool(mStatus)
return isSuccess, nil
}

func PromptAccount() bool {
passWord := ""
fmt.Println("Login using your ITR's account:")
color.Magenta("Username: " + itrUserName)
passWord, err := PromptPassword()
if err != nil {
fmt.Printf("Invalid Password %v \n", err)
}
return VerifyAccount(itrUserName, passWord)
}