From cb7702873fd610d79c441a9d7694232bbdf5c6c0 Mon Sep 17 00:00:00 2001 From: Ben Hale Date: Wed, 4 Nov 2020 11:29:13 -0800 Subject: [PATCH] Make Public This change makes a bit more of the API public in order the re-use it elsewhere. Signed-off-by: Ben Hale --- cmd/octo/main.go | 8 +++----- octo/actions.go | 4 ++-- octo/builder_dependencies.go | 14 +++++++------- octo/buildpack_dependencies.go | 6 +++--- octo/contribution.go | 2 +- octo/create_builder.go | 10 +++++----- octo/create_package.go | 14 +++++++------- octo/dependabot.go | 4 ++-- octo/descriptor.go | 6 +++--- octo/draft_release.go | 18 +++++++++--------- octo/octo.go | 18 +++++++----------- octo/offline_packages.go | 12 ++++++------ octo/package_dependencies.go | 8 ++++---- octo/test.go | 22 +++++++++++----------- octo/update_pipeline.go | 4 ++-- octo/util.go | 8 ++++---- 16 files changed, 76 insertions(+), 82 deletions(-) diff --git a/cmd/octo/main.go b/cmd/octo/main.go index fbb92a1f..c680a470 100644 --- a/cmd/octo/main.go +++ b/cmd/octo/main.go @@ -27,20 +27,18 @@ import ( ) func main() { - w := octo.Octo{} - flagSet := pflag.NewFlagSet("octo", pflag.ExitOnError) - flagSet.StringVar(&w.DescriptorPath, "descriptor", "", "path to input descriptor") + descriptor := flagSet.String("descriptor", "", "path to input descriptor") if err := flagSet.Parse(os.Args[1:]); err != nil { log.Fatal(fmt.Errorf("unable to parse flags\n%w", err)) } - if w.DescriptorPath == "" { + if descriptor == nil { log.Fatal("--descriptor is required") } - if err := w.Contribute(); err != nil { + if err := octo.Contribute(*descriptor); err != nil { log.Fatal(fmt.Errorf("unable to build\n%w", err)) } } diff --git a/octo/actions.go b/octo/actions.go index 71044c36..73acd82f 100644 --- a/octo/actions.go +++ b/octo/actions.go @@ -58,11 +58,11 @@ func ContributeActions(descriptor Descriptor) ([]Contribution, error) { { Id: "version", Name: "Compute Version", - Run: statikString("/compute-version.sh"), + Run: StatikString("/compute-version.sh"), }, { Name: "Create Action", - Run: statikString("/create-action.sh"), + Run: StatikString("/create-action.sh"), Env: map[string]string{ "PUSH": "${{ github.event_name != 'pull_request' }}", "SOURCE": a.Source, diff --git a/octo/builder_dependencies.go b/octo/builder_dependencies.go index c0640371..33258e59 100644 --- a/octo/builder_dependencies.go +++ b/octo/builder_dependencies.go @@ -91,16 +91,16 @@ func contributeBuildImage(descriptor Descriptor, image string, classifier string }, { Name: "Install update-build-image-dependency", - Run: statikString("/install-update-build-image-dependency.sh"), + Run: StatikString("/install-update-build-image-dependency.sh"), }, { Name: "Install crane", - Run: statikString("/install-crane.sh"), + Run: StatikString("/install-crane.sh"), Env: map[string]string{"CRANE_VERSION": CraneVersion}, }, { Name: "Install yj", - Run: statikString("/install-yj.sh"), + Run: StatikString("/install-yj.sh"), Env: map[string]string{"YJ_VERSION": YJVersion}, }, { @@ -109,7 +109,7 @@ func contributeBuildImage(descriptor Descriptor, image string, classifier string { Id: "build-image", Name: "Update Build Image Dependency", - Run: statikString("/update-build-image-dependency.sh"), + Run: StatikString("/update-build-image-dependency.sh"), Env: map[string]string{ "IMAGE": image, "CLASSIFIER": classifier, @@ -160,11 +160,11 @@ func contributeLifecycle(descriptor Descriptor) (Contribution, error) { }, { Name: "Install update-lifecycle-dependency", - Run: statikString("/install-update-lifecycle-dependency.sh"), + Run: StatikString("/install-update-lifecycle-dependency.sh"), }, { Name: "Install yj", - Run: statikString("/install-yj.sh"), + Run: StatikString("/install-yj.sh"), Env: map[string]string{"YJ_VERSION": YJVersion}, }, { @@ -183,7 +183,7 @@ func contributeLifecycle(descriptor Descriptor) (Contribution, error) { { Id: "lifecycle", Name: "Update Lifecycle Dependency", - Run: statikString("/update-lifecycle-dependency.sh"), + Run: StatikString("/update-lifecycle-dependency.sh"), Env: map[string]string{ "VERSION": "${{ steps.dependency.outputs.version }}", }, diff --git a/octo/buildpack_dependencies.go b/octo/buildpack_dependencies.go index fb1c73c8..12f42cd5 100644 --- a/octo/buildpack_dependencies.go +++ b/octo/buildpack_dependencies.go @@ -46,11 +46,11 @@ func ContributeBuildpackDependencies(descriptor Descriptor) ([]Contribution, err }, { Name: "Install update-buildpack-dependency", - Run: statikString("/install-update-buildpack-dependency.sh"), + Run: StatikString("/install-update-buildpack-dependency.sh"), }, { Name: "Install yj", - Run: statikString("/install-yj.sh"), + Run: StatikString("/install-yj.sh"), Env: map[string]string{"YJ_VERSION": YJVersion}, }, { @@ -64,7 +64,7 @@ func ContributeBuildpackDependencies(descriptor Descriptor) ([]Contribution, err { Id: "buildpack", Name: "Update Buildpack Dependency", - Run: statikString("/update-buildpack-dependency.sh"), + Run: StatikString("/update-buildpack-dependency.sh"), Env: map[string]string{ "ID": d.Id, "SHA256": "${{ steps.dependency.outputs.sha256 }}", diff --git a/octo/contribution.go b/octo/contribution.go index c39d5237..5e851663 100644 --- a/octo/contribution.go +++ b/octo/contribution.go @@ -110,7 +110,7 @@ func NewHttpCredentialActions(credentials []HTTPCredentials) []actions.Step { s = append(s, actions.Step{ Name: fmt.Sprintf("HTTP login %s", c.Host), If: "${{ github.event_name != 'pull_request' || ! github.event.pull_request.head.repo.fork }}", - Run: statikString("/update-netrc.sh"), + Run: StatikString("/update-netrc.sh"), Env: map[string]string{ "HOST": c.Host, "USERNAME": c.Username, diff --git a/octo/create_builder.go b/octo/create_builder.go index 2bd116d9..ba1d38c5 100644 --- a/octo/create_builder.go +++ b/octo/create_builder.go @@ -46,12 +46,12 @@ func ContributeCreateBuilder(descriptor Descriptor) (*Contribution, error) { }, { Name: "Install crane", - Run: statikString("/install-crane.sh"), + Run: StatikString("/install-crane.sh"), Env: map[string]string{"CRANE_VERSION": CraneVersion}, }, { Name: "Install pack", - Run: statikString("/install-pack.sh"), + Run: StatikString("/install-pack.sh"), Env: map[string]string{"PACK_VERSION": PackVersion}, }, { @@ -60,12 +60,12 @@ func ContributeCreateBuilder(descriptor Descriptor) (*Contribution, error) { { Id: "version", Name: "Compute Version", - Run: statikString("/compute-version.sh"), + Run: StatikString("/compute-version.sh"), }, { Id: "builder", Name: "Create Builder", - Run: statikString("/create-builder.sh"), + Run: StatikString("/create-builder.sh"), Env: map[string]string{ "BUILDER": descriptor.Builder.Repository, "PUBLISH": "true", @@ -74,7 +74,7 @@ func ContributeCreateBuilder(descriptor Descriptor) (*Contribution, error) { }, { Name: "Update release with digest", - Run: statikString("/update-release-digest.sh"), + Run: StatikString("/update-release-digest.sh"), Env: map[string]string{ "DIGEST": "${{ steps.builder.outputs.digest }}", "GITHUB_TOKEN": descriptor.GitHubToken, diff --git a/octo/create_package.go b/octo/create_package.go index 58b90954..a0cb210e 100644 --- a/octo/create_package.go +++ b/octo/create_package.go @@ -55,16 +55,16 @@ func ContributeCreatePackage(descriptor Descriptor) (*Contribution, error) { }, { Name: "Install create-package", - Run: statikString("/install-create-package.sh"), + Run: StatikString("/install-create-package.sh"), }, { Name: "Install crane", - Run: statikString("/install-crane.sh"), + Run: StatikString("/install-crane.sh"), Env: map[string]string{"CRANE_VERSION": CraneVersion}, }, { Name: "Install pack", - Run: statikString("/install-pack.sh"), + Run: StatikString("/install-pack.sh"), Env: map[string]string{"PACK_VERSION": PackVersion}, }, { @@ -85,11 +85,11 @@ func ContributeCreatePackage(descriptor Descriptor) (*Contribution, error) { { Id: "version", Name: "Compute Version", - Run: statikString("/compute-version.sh"), + Run: StatikString("/compute-version.sh"), }, { Name: "Create Package", - Run: statikString("/create-package.sh"), + Run: StatikString("/create-package.sh"), Env: map[string]string{ "INCLUDE_DEPENDENCIES": strconv.FormatBool(descriptor.Package.IncludeDependencies), "VERSION": "${{ steps.version.outputs.version }}", @@ -98,7 +98,7 @@ func ContributeCreatePackage(descriptor Descriptor) (*Contribution, error) { { Id: "package", Name: "Package Buildpack", - Run: statikString("/package-buildpack.sh"), + Run: StatikString("/package-buildpack.sh"), Env: map[string]string{ "PACKAGE": descriptor.Package.Repository, "PUBLISH": "true", @@ -107,7 +107,7 @@ func ContributeCreatePackage(descriptor Descriptor) (*Contribution, error) { }, { Name: "Update release with digest", - Run: statikString("/update-release-digest.sh"), + Run: StatikString("/update-release-digest.sh"), Env: map[string]string{ "DIGEST": "${{ steps.package.outputs.digest }}", "GITHUB_TOKEN": descriptor.GitHubToken, diff --git a/octo/dependabot.go b/octo/dependabot.go index dae7aaf3..1d616874 100644 --- a/octo/dependabot.go +++ b/octo/dependabot.go @@ -37,8 +37,8 @@ func ContributeDependabot(descriptor Descriptor) (Contribution, error) { } file := filepath.Join(descriptor.Path, "go.mod") - if e, err := exists(file); err != nil { - return Contribution{}, fmt.Errorf("unable to determine if %s exists\n%w", file, err) + if e, err := Exists(file); err != nil { + return Contribution{}, fmt.Errorf("unable to determine if %s Exists\n%w", file, err) } else if e { d.Updates = append(d.Updates, dependabot.Update{ PackageEcosystem: dependabot.GoModulesPackageEcosystem, diff --git a/octo/descriptor.go b/octo/descriptor.go index 71a5a4f7..d561b58f 100644 --- a/octo/descriptor.go +++ b/octo/descriptor.go @@ -112,7 +112,7 @@ func NewDescriptor(path string) (Descriptor, error) { if !filepath.IsAbs(d.Path) { if d.Path, err = filepath.Abs(filepath.Join(filepath.Dir(path), d.Path)); err != nil { - return Descriptor{}, fmt.Errorf("unable to find absolute path\n%w", err) + return Descriptor{}, fmt.Errorf("unable to Find absolute path\n%w", err) } } @@ -132,12 +132,12 @@ func NewDescriptor(path string) (Descriptor, error) { d.Test.Steps = []actions.Step{ { Name: "Install richgo", - Run: statikString("/install-richgo.sh"), + Run: StatikString("/install-richgo.sh"), Env: map[string]string{"RICHGO_VERSION": RichGoVersion}, }, { Name: "Run Tests", - Run: statikString("/run-tests.sh"), + Run: StatikString("/run-tests.sh"), Env: map[string]string{"RICHGO_FORCE_COLOR": "1"}, }, } diff --git a/octo/draft_release.go b/octo/draft_release.go index d43d3efe..cb4372bc 100644 --- a/octo/draft_release.go +++ b/octo/draft_release.go @@ -96,21 +96,21 @@ func ContributeDraftRelease(descriptor Descriptor) ([]Contribution, error) { } file := filepath.Join(descriptor.Path, "builder.toml") - builderExists, err := exists(file) + builderExists, err := Exists(file) if err != nil { - return nil, fmt.Errorf("unable to determine if %s exists\n%w", file, err) + return nil, fmt.Errorf("unable to determine if %s Exists\n%w", file, err) } file = filepath.Join(descriptor.Path, "buildpack.toml") - buildpackExists, err := exists(file) + buildpackExists, err := Exists(file) if err != nil { - return nil, fmt.Errorf("unable to determine if %s exists\n%w", file, err) + return nil, fmt.Errorf("unable to determine if %s Exists\n%w", file, err) } file = filepath.Join(descriptor.Path, "package.toml") - packageExists, err := exists(file) + packageExists, err := Exists(file) if err != nil { - return nil, fmt.Errorf("unable to determine if %s exists\n%w", file, err) + return nil, fmt.Errorf("unable to determine if %s Exists\n%w", file, err) } if builderExists || buildpackExists { @@ -122,7 +122,7 @@ func ContributeDraftRelease(descriptor Descriptor) ([]Contribution, error) { j.Steps = append(j.Steps, actions.Step{ Name: "Install crane", - Run: statikString("/install-crane.sh"), + Run: StatikString("/install-crane.sh"), Env: map[string]string{"CRANE_VERSION": CraneVersion}, }, ) @@ -131,7 +131,7 @@ func ContributeDraftRelease(descriptor Descriptor) ([]Contribution, error) { j.Steps = append(j.Steps, actions.Step{ Name: "Install yj", - Run: statikString("/install-yj.sh"), + Run: StatikString("/install-yj.sh"), Env: map[string]string{"YJ_VERSION": YJVersion}, }, actions.Step{ @@ -139,7 +139,7 @@ func ContributeDraftRelease(descriptor Descriptor) ([]Contribution, error) { }, actions.Step{ Name: "Update draft release with buildpack information", - Run: statikString("/update-draft-release-buildpack.sh"), + Run: StatikString("/update-draft-release-buildpack.sh"), Env: map[string]string{ "GITHUB_TOKEN": descriptor.GitHubToken, "RELEASE_ID": "${{ steps.release-drafter.outputs.id }}", diff --git a/octo/octo.go b/octo/octo.go index b7c9e6a3..9124b158 100644 --- a/octo/octo.go +++ b/octo/octo.go @@ -38,14 +38,10 @@ const ( var RemovedFiles []string -type Octo struct { - DescriptorPath string -} - -func (o Octo) Contribute() error { - descriptor, err := NewDescriptor(o.DescriptorPath) +func Contribute(path string) error { + descriptor, err := NewDescriptor(path) if err != nil { - return fmt.Errorf("unable to read descriptor\n%w", err) + return fmt.Errorf("unable to read descriptor %s\n%w", path, err) } var contributions []Contribution @@ -124,14 +120,14 @@ func (o Octo) Contribute() error { contributions = append(contributions, c) } - if err := o.Remove(descriptor, RemovedFiles); err != nil { + if err := Remove(descriptor, RemovedFiles); err != nil { return err } - return o.Write(descriptor, contributions) + return Write(descriptor, contributions) } -func (Octo) Remove(descriptor Descriptor, removals []string) error { +func Remove(descriptor Descriptor, removals []string) error { for _, r := range removals { file := filepath.Join(descriptor.Path, r) fmt.Printf("Removing %s\n", r) @@ -144,7 +140,7 @@ func (Octo) Remove(descriptor Descriptor, removals []string) error { return nil } -func (Octo) Write(descriptor Descriptor, contributions []Contribution) error { +func Write(descriptor Descriptor, contributions []Contribution) error { t := gotree.New(descriptor.Path) sort.Slice(contributions, func(i, j int) bool { diff --git a/octo/offline_packages.go b/octo/offline_packages.go index b45fdd27..421ec744 100644 --- a/octo/offline_packages.go +++ b/octo/offline_packages.go @@ -53,7 +53,7 @@ func contributeOfflinePackage(descriptor Descriptor, offlinePackage OfflinePacka Steps: []actions.Step{ { Name: "Install crane", - Run: statikString("/install-crane.sh"), + Run: StatikString("/install-crane.sh"), Env: map[string]string{"CRANE_VERSION": CraneVersion}, }, { @@ -66,7 +66,7 @@ func contributeOfflinePackage(descriptor Descriptor, offlinePackage OfflinePacka { Id: "version", Name: "Checkout next version", - Run: statikString("/checkout-next-version.sh"), + Run: StatikString("/checkout-next-version.sh"), Env: map[string]string{ "SOURCE": offlinePackage.Source, "TARGET": offlinePackage.Target, @@ -91,18 +91,18 @@ func contributeOfflinePackage(descriptor Descriptor, offlinePackage OfflinePacka { Name: "Install create-package", If: "${{ ! steps.version.outputs.skip }}", - Run: statikString("/install-create-package.sh"), + Run: StatikString("/install-create-package.sh"), }, { Name: "Install pack", If: "${{ ! steps.version.outputs.skip }}", - Run: statikString("/install-pack.sh"), + Run: StatikString("/install-pack.sh"), Env: map[string]string{"PACK_VERSION": PackVersion}, }, { Name: "Create Package", If: "${{ ! steps.version.outputs.skip }}", - Run: statikString("/create-package.sh"), + Run: StatikString("/create-package.sh"), Env: map[string]string{ "INCLUDE_DEPENDENCIES": "true", "VERSION": "${{ steps.version.outputs.version }}", @@ -111,7 +111,7 @@ func contributeOfflinePackage(descriptor Descriptor, offlinePackage OfflinePacka { Name: "Package Buildpack", If: "${{ ! steps.version.outputs.skip }}", - Run: statikString("/package-buildpack.sh"), + Run: StatikString("/package-buildpack.sh"), Env: map[string]string{ "PACKAGE": offlinePackage.Target, "PUBLISH": "true", diff --git a/octo/package_dependencies.go b/octo/package_dependencies.go index 70de719d..e54f6b04 100644 --- a/octo/package_dependencies.go +++ b/octo/package_dependencies.go @@ -79,16 +79,16 @@ func contributePackageDependency(descriptor Descriptor, name string) (Contributi }, { Name: "Install update-package-dependency", - Run: statikString("/install-update-package-dependency.sh"), + Run: StatikString("/install-update-package-dependency.sh"), }, { Name: "Install crane", - Run: statikString("/install-crane.sh"), + Run: StatikString("/install-crane.sh"), Env: map[string]string{"CRANE_VERSION": CraneVersion}, }, { Name: "Install yj", - Run: statikString("/install-yj.sh"), + Run: StatikString("/install-yj.sh"), Env: map[string]string{"YJ_VERSION": YJVersion}, }, { @@ -97,7 +97,7 @@ func contributePackageDependency(descriptor Descriptor, name string) (Contributi { Id: "package", Name: "Update Package Dependency", - Run: statikString("/update-package-dependency.sh"), + Run: StatikString("/update-package-dependency.sh"), Env: map[string]string{"DEPENDENCY": name}, }, { diff --git a/octo/test.go b/octo/test.go index cc646ad0..c9308d80 100644 --- a/octo/test.go +++ b/octo/test.go @@ -41,8 +41,8 @@ func ContributeTest(descriptor Descriptor) (*Contribution, error) { Jobs: map[string]actions.Job{}, } - if f, err := find(descriptor.Path, regexp.MustCompile(`.+\.go`).MatchString); err != nil { - return nil, fmt.Errorf("unable to find .go files in %s\n%w", descriptor.Path, err) + if f, err := Find(descriptor.Path, regexp.MustCompile(`.+\.go`).MatchString); err != nil { + return nil, fmt.Errorf("unable to Find .go files in %s\n%w", descriptor.Path, err) } else if len(f) > 0 { j := actions.Job{ Name: "Unit Test", @@ -78,7 +78,7 @@ func ContributeTest(descriptor Descriptor) (*Contribution, error) { Steps: []actions.Step{ { Name: "Install pack", - Run: statikString("/install-pack.sh"), + Run: StatikString("/install-pack.sh"), Env: map[string]string{"PACK_VERSION": PackVersion}, }, { @@ -87,11 +87,11 @@ func ContributeTest(descriptor Descriptor) (*Contribution, error) { { Id: "version", Name: "Compute Version", - Run: statikString("/compute-version.sh"), + Run: StatikString("/compute-version.sh"), }, { Name: "Create Builder", - Run: statikString("/create-builder.sh"), + Run: StatikString("/create-builder.sh"), Env: map[string]string{ "BUILDER": "test", "VERSION": "${{ steps.version.outputs.version }}", @@ -112,11 +112,11 @@ func ContributeTest(descriptor Descriptor) (*Contribution, error) { Steps: []actions.Step{ { Name: "Install create-package", - Run: statikString("/install-create-package.sh"), + Run: StatikString("/install-create-package.sh"), }, { Name: "Install pack", - Run: statikString("/install-pack.sh"), + Run: StatikString("/install-pack.sh"), Env: map[string]string{"PACK_VERSION": PackVersion}, }, { @@ -140,11 +140,11 @@ func ContributeTest(descriptor Descriptor) (*Contribution, error) { { Id: "version", Name: "Compute Version", - Run: statikString("/compute-version.sh"), + Run: StatikString("/compute-version.sh"), }, { Name: "Create Package", - Run: statikString("/create-package.sh"), + Run: StatikString("/create-package.sh"), Env: map[string]string{ "INCLUDE_DEPENDENCIES": "true", "VERSION": "${{ steps.version.outputs.version }}", @@ -152,7 +152,7 @@ func ContributeTest(descriptor Descriptor) (*Contribution, error) { }, { Name: "Package Buildpack", - Run: statikString("/package-buildpack.sh"), + Run: StatikString("/package-buildpack.sh"), Env: map[string]string{ "PACKAGE": "test", "VERSION": "${{ steps.version.outputs.version }}", @@ -172,5 +172,5 @@ func ContributeTest(descriptor Descriptor) (*Contribution, error) { return nil, err } - return &c, err + return &c, nil } diff --git a/octo/update_pipeline.go b/octo/update_pipeline.go index f24e67f6..a926fb9b 100644 --- a/octo/update_pipeline.go +++ b/octo/update_pipeline.go @@ -41,7 +41,7 @@ func ContributeUpdatePipeline(descriptor Descriptor) (Contribution, error) { }, { Name: "Install octo", - Run: statikString("/install-octo.sh"), + Run: StatikString("/install-octo.sh"), }, { Uses: "actions/checkout@v2", @@ -49,7 +49,7 @@ func ContributeUpdatePipeline(descriptor Descriptor) (Contribution, error) { { Id: "pipeline", Name: "Update Pipeline", - Run: statikString("/update-pipeline.sh"), + Run: StatikString("/update-pipeline.sh"), Env: map[string]string{ "GITHUB_TOKEN": descriptor.GitHubToken, "DESCRIPTOR": filepath.Join(".github", "pipeline-descriptor.yml"), diff --git a/octo/util.go b/octo/util.go index 18303c29..d17130b1 100644 --- a/octo/util.go +++ b/octo/util.go @@ -26,7 +26,7 @@ import ( _ "github.com/paketo-buildpacks/pipeline-builder/octo/statik" ) -func exists(path string) (bool, error) { +func Exists(path string) (bool, error) { if _, err := os.Stat(path); os.IsNotExist(err) { return false, nil } else if err != nil { @@ -36,9 +36,9 @@ func exists(path string) (bool, error) { } } -type predicate func(string) bool +type Predicate func(string) bool -func find(path string, predicate predicate) ([]string, error) { +func Find(path string, predicate Predicate) ([]string, error) { var matches []string err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { @@ -58,7 +58,7 @@ func find(path string, predicate predicate) ([]string, error) { var statik, _ = fs.New() -func statikString(path string) string { +func StatikString(path string) string { in, err := statik.Open(path) if err != nil { panic(err)