goss-org / goss

Quick and Easy server testing/validation
https://goss.rocks
Apache License 2.0
5.5k stars 470 forks source link

Use goss as a go package outside of the CLI #886

Closed retr0h closed 3 months ago

retr0h commented 3 months ago

I did not see much information about this, but I want to use the goss package in another project I'm working on, basically I am writing the reverse of Goss (i.e. goss up).

I can embed the CLI into my project using ember, but I'd prefer not to do this. As the CLI seems to be the gateway into Goss, I wanted to know if this was a supported use case.

ripienaar commented 3 months ago

You can use goss as a package:

    var out bytes.Buffer

    opts := []gossutil.ConfigOption{
        gossutil.WithMaxConcurrency(1),
        gossutil.WithResultWriter(&out),
        gossutil.WithSpecFile(w.properties.Gossfile),
    }

    cfg, err := gossutil.NewConfig(opts...)
    if err != nil {
        return err
    }

    _, err = goss.Validate(cfg)
    if err != nil {
        return err
    }

    res := &outputs.StructuredOutput{}
    err = json.Unmarshal(out.Bytes(), res)
    if err != nil {
        return err
    }

    if res.Summary.Failed > 0 {
        return fmt.Errorf("check failed: %v", res.SummaryLine, pd)
    }

    return nil
retr0h commented 3 months ago

Thanks @ripienaar