Skip to content

🎯 CheckStructured

Checks whether composer.json and composer.lock are in sync and returns a structured result (validity + categorized messages/warnings/errors).

When to use

Use in CI pipelines that need to decide whether to allow a build based on the Valid boolean and report error details separately.

Signature

go
func (c *Composer) CheckStructured() (*CheckResult, error)

Parameters

ParameterTypeDescription
NoneThis method takes no parameters

Return value

  • *CheckResult: Check result, including Valid and categorized messages
  • error: Error from executing the command (note: when there is an error, Valid is set to false, but the result is still returned)
go
type CheckResult struct {
    Valid    bool     `json:"valid"`
    Messages []string `json:"messages,omitempty"`
    Warnings []string `json:"warnings,omitempty"`
    Errors   []string `json:"errors,omitempty"`
}

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.Fatal(err)
    }

    result, err := comp.CheckStructured()
    if err != nil {
        log.Printf("check encountered an error: %v", err)
    }

    if !result.Valid {
        fmt.Println("Check failed, errors:")
        for _, e := range result.Errors {
            fmt.Println("- " + e)
        }
    } else {
        fmt.Println("Check passed")
    }
}

Advanced

  • Parsing is done by ParseCheckOutput: lines matching error/Error/FAIL are categorized as errors, warning/Warning/WARN as warnings, and the rest as messages
  • For the raw text version, see Check
  • To validate the composer.json schema strictly, use ValidateStructured (see result_types.go)

Released under the MIT License