Developer Guide

pdtm allows you to use its internal packages to build custom update workflows or manage tools programmatically within your own Go applications.

Library Usage

You can import github.com/projectdiscovery/pdtm/pkg/utils to interact with the updater logic.

Example: Updating a Tool Programmatically

The following example demonstrates how to create a CLI tool that updates nuclei using pdtm's utility functions.

package main

import (
    "fmt"
    "os"
    "path/filepath"

    "github.com/projectdiscovery/goflags"
    "github.com/projectdiscovery/pdtm/pkg/utils"
)

type options struct {
    DisableUpdateCheck bool
}

func main() {
    options := &options{}
    flagSet := goflags.NewFlagSet()
    toolName := "nuclei"

    // Create a flag group for updates
    flagSet.CreateGroup("update", "Update",
        // Register a callback that triggers the update logic for the specific tool
        flagSet.CallbackVarP(utils.GetUpdaterCallback(toolName), "update", "up", fmt.Sprintf("update %v to the latest released version", toolName)),
        flagSet.BoolVarP(&options.DisableUpdateCheck, "disable-update-check", "duc", false, "disable automatic update check"),
    )

    if err := flagSet.Parse(); err != nil {
        panic(err)
    }

    // Perform a version check if updates are not disabled
    if !options.DisableUpdateCheck {
        home, err := os.UserHomeDir()
        if err != nil {
            panic(err)
        }
        // Define where tools are located
        basePath := filepath.Join(home, ".pdtm/go/bin")

        // Get the version check callback and execute it
        msg := utils.GetVersionCheckCallback(toolName, basePath)()
        fmt.Println(msg)
    }
}

Key Functions

  • utils.GetUpdaterCallback(toolName string): Returns a function that, when executed, attempts to update the specified tool to the latest version.
  • utils.GetVersionCheckCallback(toolName, basePath string): Returns a function that checks the currently installed version against the latest available version.