Skip to content
LICENTIVO / INTEGRATION

License a Go CLI tool for unattended jobs

Protect a command-line export with a license key. Run it from a scheduled job and check the exit code and the file it produced.

Windows x64 · Go 1.26 · PowerShellUpdated

Prepare the command-line project

In a new directory, save the example as main.go. Replace the product ID and trusted public keys. Issue one test license with export permission and another without it.

Go · main.go
package main

import (
	"context"
	"fmt"
	"os"

	licentivo "github.com/spf86/licentivo-sdk"
)

const productID = "YOUR_PRODUCT_ID"
const usage = "Usage: report-job export <output.csv>"

var trustedKeys = map[string]string{"YOUR_KEY_ID": "YOUR_PUBLIC_KEY"}

func run(args []string) int {
	if len(args) == 1 && args[0] == "--help" {
		fmt.Println(usage)
		return 0
	}
	if len(args) != 2 || args[0] != "export" {
		fmt.Fprintln(os.Stderr, usage)
		return 64
	}
	client, err := licentivo.OpenProduct(context.Background(), productID, trustedKeys, os.Getenv("LICENTIVO_LICENSE_KEY"), nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 2
	}
	defer client.Close()
	err = client.RunFeature("export", func() error {
		return os.WriteFile(args[1], []byte("item,total\nreports,1\n"), 0600)
	})
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 2
	}
	fmt.Println(args[1])
	return 0
}

func main() {
	os.Exit(run(os.Args[1:]))
}

Build the Go executable

Run these commands from the project directory. The help command works before activation and shows the arguments the scheduler will pass.

PowerShell
go mod init example.com/report-job
go get github.com/spf86/licentivo-sdk@v0.11.8
go build -o report-job.exe .
.\report-job.exe --help

Activate without prompting the job

Provision the first key through the job runner’s secret store as LICENTIVO_LICENSE_KEY. Run the first export as the account that will own the scheduled task. The executable never reads from stdin.

PowerShell
.\report-job.exe export first.csv
$LASTEXITCODE
Test-Path -LiteralPath .\first.csv

Run as a scheduled task

Save the script beside the executable in a directory writable by the job account. Use that same account for later runs with the key variable cleared. The timestamp gives each report a new filename. The script returns the program’s exit code to the scheduler.

PowerShell · run-report.ps1
$outputFile = Join-Path $PSScriptRoot ("report-{0:yyyyMMdd-HHmmssfff}.csv" -f (Get-Date))
& (Join-Path $PSScriptRoot 'report-job.exe') export $outputFile
exit $LASTEXITCODE

Read the result of a failed run

Exit 0 means success, 2 means authorization or the export failed, and 64 means invalid arguments. Diagnostics go to stderr. In a fresh user profile, run with the second key and a new output path. Expect exit 2 and no report.

In a local non-interactive PowerShell run, the script returned 0 and wrote one report with permission. Without permission it returned 2 and added no file. Invalid command arguments returned 64.

PowerShell
.\report-job.exe export denied.csv
$LASTEXITCODE
Test-Path -LiteralPath .\denied.csv