Skip to content

Instantly share code, notes, and snippets.

@jabrown85
Created January 21, 2021 18:57
Show Gist options
  • Save jabrown85/10fe11a345387a58db69467e82f1f7dc to your computer and use it in GitHub Desktop.
Save jabrown85/10fe11a345387a58db69467e82f1f7dc to your computer and use it in GitHub Desktop.
Brain dump on lifecycle organization
// analyzer.go
type AnalyzerService interface {
Analyze(group buildpack.BuildpackGroup, layersDir string, skipLayers...)
}
type analyzerService struct {
// this would implement AnalyzerService
}
func NewAnalyzerService(....) (*analyzerService) {
return analyzerService{}
}
// cmd/analyzer.go
"github.com/buildpacks/lifecycle"
"github.com/buildpacks/lifecycle/buildpacks"
"github.com/buildpacks/lifecycle/cache"
"github.com/buildpacks/lifecycle/image"
type Main struct {
AnalyzerService lifecycle.AnalyzerService
}
func main() {
// parse flags
m := setupMainFromFlagsEnvEtc()
// setup pluggable interfaces with implementations
imageResolver := image.NewImageResolver(a.docker, a.keychain)
cacheStore := cache.NewCacheStore(a.cacheImgName, a.cacheDir)
analyzerService := lifecycle.NewAnalyzerService()
// these could also be args to NewAnalyzerService if they are not optional
analyzerService.ImageResolver = imageResolver
analyzerService.CacheStore = cacheStore
// set this on the struct so we can do our work using just the interface
m.AnalyzerService = analyzerService
// do the real work
m.Analyze()
}
func (m *Main) Analyze() {
group, _ := buildpacks.ReadBuildpackGroup(m.groupPath)
// rely only on the interface, this method is now testable
m.AnalyzerService.Analyze(group, m.layersDir, m.skipLayers...more things)
}
// cmd/analyzer_test.go
"github.com/buildpacks/lifecycle"
"github.com/buildpacks/lifecycle/buildpacks"
"github.com/buildpacks/lifecycle/fake"
TestAnalyzerCmd() {
// setup fakes in analyzer service
analyzerService := lifecycle.NewAnalyzerService()
analyzerService.ImageResolver = fake.ImageResolver()
analyzerService.CacheStore = fake.CacheStore()
// eventually maybe we start taking in readers instead of string paths?
analyzerService.FileReader = tmpDirReader("fixtures", "this_test")
group := buildpacks.BuildpackGroup{}
main := Main{}
// use our stubbed out analyzer service
main.AnalyzerService = analyzerService
// test
main.Analyze(group, ...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment