Skip to content

📊 FundWithJSON

Retrieves funding information for donatable packages in the project as structured JSON, returning the parsed []FundingInfo.

When to use

Use it when you want to programmatically display funding links within an application, generate a sponsorship list page, or further process funding information. Equivalent to running composer fund --format=json and automatically parsing it.

Signature

go
func (c *Composer) FundWithJSON() ([]FundingInfo, error)

Parameters

This method does not accept any parameters.

Return value

  • []FundingInfo: funding info slice. Each entry contains the package name Name, links URLs, and whether funding is declared Funding.
  • error: returns the corresponding error message when command execution or JSON parsing fails.

FundingInfo structure:

go
type FundingInfo struct {
	Name    string   `json:"name"`
	URLs    []string `json:"urls"`
	Funding bool     `json:"funding"`
}

Example

go
package main

import (
	"fmt"
	"log"

	"github.com/scagogogo/composer-skills/pkg/composer"
)

func main() {
	comp, err := composer.New(composer.DefaultOptions())
	if err != nil {
		log.Fatalf("initialization failed: %v", err)
	}

	fundingInfo, err := comp.FundWithJSON()
	if err != nil {
		log.Fatalf("failed to get funding info: %v", err)
	}

	for _, info := range fundingInfo {
		if info.Funding {
			fmt.Printf("Package: %s\n", info.Name)
			for _, url := range info.URLs {
				fmt.Printf("  - %s\n", url)
			}
		}
	}
}

Advanced

  • This method is the implementation foundation of GetFundingURLs.
  • For plain text output only, use Fund.
  • For a boolean check only, use HasFunding.
  • To query a specific package, use FundWithPackage; for custom options, use FundWithOptions.

Released under the MIT License