mirror of
https://github.com/trufflesecurity/trufflehog.git
synced 2024-11-14 00:47:21 +00:00
f3152b6885
* Implement CommonSourceUnitUnmarshaller * Add SourceUnitUnmarshaller to all sources using All sources, with the exception of git, will use the CommonSourceUnit as they only contain a single type of unit to scan. * Fix method comments to adhere to Go's style guide
37 lines
1.1 KiB
Go
37 lines
1.1 KiB
Go
package sources
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
// Ensure CommonSourceUnit implements SourceUnit at compile time.
|
|
var _ SourceUnit = CommonSourceUnit{}
|
|
|
|
// CommonSourceUnit is a common implementation of SourceUnit that Sources can
|
|
// use instead of implementing their own types.
|
|
type CommonSourceUnit struct {
|
|
ID string `json:"source_unit_id"`
|
|
}
|
|
|
|
// SourceUnitID implements the SourceUnit interface.
|
|
func (c CommonSourceUnit) SourceUnitID() string {
|
|
return c.ID
|
|
}
|
|
|
|
// CommonSourceUnitUnmarshaller is an implementation of SourceUnitUnmarshaller
|
|
// for the CommonSourceUnit. A source can embed this struct to gain the
|
|
// functionality of converting []byte to a CommonSourceUnit.
|
|
type CommonSourceUnitUnmarshaller struct{}
|
|
|
|
// UnmarshalSourceUnit implements the SourceUnitUnmarshaller interface.
|
|
func (c CommonSourceUnitUnmarshaller) UnmarshalSourceUnit(data []byte) (SourceUnit, error) {
|
|
var unit CommonSourceUnit
|
|
if err := json.Unmarshal(data, &unit); err != nil {
|
|
return nil, err
|
|
}
|
|
if unit.ID == "" {
|
|
return nil, fmt.Errorf("not a CommonSourceUnit")
|
|
}
|
|
return unit, nil
|
|
}
|