scaffolding-go

September 29, 2020 · View on GitHub

Build Status

scaffolding-go

Chef Habitat scaffolding-go is a way to build a Chef Habitat plan that overrides some of the default functions that Chef Habitat uses during its build process.

For more information on how to use the scaffolding-go see below in the Use as a Tool section. Also refer to documentation

Maintainers

Type of Package

Library package

Use as Dependency

Library packages can be set as runtime or build time dependencies, however they are typically used as buildtime dependencies. See Defining your dependencies for more information.

To add core/scaffolding-go as a dependency, you can add one of the following to your plan file.

Buildtime Dependency

pkg_build_deps=(core/scaffolding-go)

Runtime Dependency

pkg_deps=(core/scaffolding-go)

Use as a Library

Installation

To install this plan, run the following command:

hab pkg install core/scaffolding-go

hab pkg install core/scaffolding-go
» Installing core/scaffolding-go
 Determining latest version of core/scaffolding-go in the 'stable' channel
 Found newer installed version (core/scaffolding-go/0.2.0/20200929104501) than remote version (core/scaffolding-go/0.2.0/20200824092119)
 Using core/scaffolding-go/0.2.0/20200929104501
 Install of core/scaffolding-go/0.2.0/20200929104501 complete with 0 new packages installed.

Viewing library files

To view the library files first get the habitat installation directory

hab pkg path core/scaffolding-go
/hab/pkgs/core/scaffolding-go/0.2.0/20200929104501

Then list the libraries, for example:

ls -1 $(hab pkg path core/scaffolding-go)/lib
go_module.sh
gopath_mode.sh
scaffolding.sh

Use as a Tool

The go scaffolding can be used to package an application from a remote repository or from local source. It has two available modes: (1) GOPATH Mode and (2) Module Mode.

These modes will be described below followed by descriptions of scaffolding variables and callbacks.

GOPATH Mode

In this mode, the Go Scaffolding will automatically configure the GOPATH environment variable and directory structure.

By adding the pkg_source variable you will be telling the go scaffolding to package the application from a remote repository.

An example of a plan.sh:

pkg_name=hello-go
pkg_origin=afiune
pkg_version="0.1.0"
pkg_scaffolding=core/scaffolding-go
pkg_source="http://github.com/afiune/hello-go"

If you are building an application from local source, you have to avoid defining the pkg_source variable, this way the go scaffolding will prapare a Go Workspace to build and package your local application.

Optionally, you can define the scaffolding_go_base_path if you are planning to keep your code in a source repository somewhere.

An example of a plan.sh:

pkg_name=hello-go
pkg_origin=afiune
pkg_version="0.1.0"
pkg_scaffolding=core/scaffolding-go
scaffolding_go_base_path=github.com/afiune

Go Module Mode

This mode allows you to build your go application outside a Go Workspace, it requires the existence of the go.mod file.

An example of a plan.sh:

pkg_name=hello-go
pkg_origin=afiune
pkg_version="0.1.0"
pkg_scaffolding=core/scaffolding-go
scaffolding_go_module=on

See https://www.habitat.sh/docs/concepts-scaffolding/ to learn how to get started with Scaffolding.

Variables

VariableTypeValueDefault
scaffolding_go_gopathString(Optional) Value for GOPATH$SRC_PATH
scaffolding_go_base_pathString(Optional) The base path that will be used in the import path construction. Eg: github.com/myorglocalhost/user
scaffolding_go_build_depsArray(Optional) Array of URLs to go getUndefined
scaffolding_go_moduleString(Optional) Enable or disable go module support: off or onauto

Callbacks

  • scaffolding_go_get - Executes go get against the pkg_source and the contents of the scaffolding_go_build_deps array to install any additional dependencies which would not otherwise be resolved.
  • scaffolding_go_before - Initialize the Go Workspace package path only if no pkg_source was specified.
  • scaffolding_go_download - Calls scaffolding_go_get by default. This callback adds a callback you can use to override download behaviors.
  • scaffolding_go_clean - Performs go clean -r -i to recursively clean build and build deps.
  • scaffolding_go_build - This will attempt to use a Makefile if one is found and assumes there is a default make target. If no Makefile is found, go build is executed against the project.
  • scaffolding_go_install - Installs the application and runtime deps into "${pkg_prefix}/${bin}"

Default Overrides

The following default callbacks have overrides: