Skip to content

Commit

Permalink
extract bobfile into own package
Browse files Browse the repository at this point in the history
  • Loading branch information
joonas-fi committed Sep 2, 2024
1 parent 8c76e5c commit 1c822d2
Show file tree
Hide file tree
Showing 18 changed files with 133 additions and 97 deletions.
9 changes: 5 additions & 4 deletions cmd/bob/baseimageconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/function61/gokit/encoding/jsonfile"
"github.com/function61/gokit/os/osutil"
"github.com/function61/turbobob/pkg/bobfile"
)

const (
Expand All @@ -16,9 +17,9 @@ const (

// base image conf JSON - able to provide hints for useful commands, setting up cache paths etc.
type BaseImageConfig struct {
DevShellCommands []DevShellCommand `json:"dev_shell_commands"`
PathsToCache []string `json:"paths_to_cache"` // will be set up as symlinks to a persistent mountpoint, so that subsequent containers benefit from cache
Langserver *LangserverSpec `json:"langserver"`
DevShellCommands []bobfile.DevShellCommand `json:"dev_shell_commands"`
PathsToCache []string `json:"paths_to_cache"` // will be set up as symlinks to a persistent mountpoint, so that subsequent containers benefit from cache
Langserver *LangserverSpec `json:"langserver"`

FileDescriptionBoilerplate string `json:"for_description_of_this_file_see"` // URL to Bob homepage

Expand Down Expand Up @@ -54,7 +55,7 @@ func loadBaseImageConfWhenInsideContainer() (*BaseImageConfig, error) {

// non-optional because the implementation makes it a bit hard to check if the file exists
// (vs. Docker run error), and our current callsite needs non-optional anyway
func loadNonOptionalBaseImageConf(builder BuilderSpec) (*BaseImageConfig, error) {
func loadNonOptionalBaseImageConf(builder bobfile.BuilderSpec) (*BaseImageConfig, error) {
dockerImage, err := func() (string, error) {
kind, data, err := parseBuilderUsesType(builder.Uses)
if err != nil {
Expand Down
19 changes: 10 additions & 9 deletions cmd/bob/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
"time"

"github.com/function61/gokit/os/osutil"
"github.com/function61/turbobob/pkg/bobfile"
"github.com/function61/turbobob/pkg/versioncontrol"
"github.com/spf13/cobra"
)

type BuildContext struct {
Bobfile *Bobfile
Bobfile *bobfile.Bobfile
OriginDir string // where the repo exists
WorkspaceDir string // where the revision is being built
PublishArtefacts bool
Expand All @@ -31,7 +32,7 @@ type BuildContext struct {
IsDefaultBranch bool // whether we are in "main" / "master" or equivalent branch
}

func runBuilder(builder BuilderSpec, buildCtx *BuildContext, opDesc string, cmdToRun []string) error {
func runBuilder(builder bobfile.BuilderSpec, buildCtx *BuildContext, opDesc string, cmdToRun []string) error {
wd, errWd := os.Getwd()
if errWd != nil {
return errWd
Expand Down Expand Up @@ -125,7 +126,7 @@ func runBuilder(builder BuilderSpec, buildCtx *BuildContext, opDesc string, cmdT
return nil
}

func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildContext) error {
func buildAndPushOneDockerImage(dockerImage bobfile.DockerImageSpec, buildCtx *BuildContext) error {
tagWithoutVersion := dockerImage.Image
tag := tagWithoutVersion + ":" + buildCtx.RevisionId.FriendlyRevisionId
tagLatest := tagWithoutVersion + ":latest"
Expand Down Expand Up @@ -362,7 +363,7 @@ func build(buildCtx *BuildContext) error {
}

// three-pass process. the flow is well documented in *BuilderCommands* type
pass := func(opDesc string, getCommand func(cmds BuilderCommands) []string) error {
pass := func(opDesc string, getCommand func(cmds bobfile.BuilderCommands) []string) error {
for _, builder := range buildCtx.Bobfile.Builders {
if buildCtx.BuilderNameFilter != "" && builder.Name != buildCtx.BuilderNameFilter {
continue
Expand All @@ -381,9 +382,9 @@ func build(buildCtx *BuildContext) error {
return nil
}

preparePass := func(cmds BuilderCommands) []string { return cmds.Prepare }
buildPass := func(cmds BuilderCommands) []string { return cmds.Build }
publishPass := func(cmds BuilderCommands) []string { return cmds.Publish }
preparePass := func(cmds bobfile.BuilderCommands) []string { return cmds.Prepare }
buildPass := func(cmds bobfile.BuilderCommands) []string { return cmds.Build }
publishPass := func(cmds bobfile.BuilderCommands) []string { return cmds.Publish }

if err := pass("prepare", preparePass); err != nil {
return err // err context ok
Expand Down Expand Up @@ -426,7 +427,7 @@ func constructBuildContext(
fastBuild bool,
areWeInCi bool,
) (*BuildContext, error) {
bobfile, err := readBobfile()
bobfile, err := bobfile.Read()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -579,7 +580,7 @@ func buildInsideEntry() *cobra.Command {
}

func buildInside(fastBuild bool) error {
bobfile, err := readBobfile()
bobfile, err := bobfile.Read()
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/bob/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (

"github.com/function61/gokit/encoding/jsonfile"
"github.com/function61/gokit/os/osutil"
"github.com/function61/turbobob/pkg/bobfile"
"github.com/function61/turbobob/pkg/versioncontrol"
"github.com/spf13/cobra"
)

func devCommand(builderName string, envsAreRequired bool, ignoreNag bool) ([]string, error) {
bobfile, err := readBobfile()
bobfile, err := bobfile.Read()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -264,7 +265,7 @@ func revisionIdForDev() *versioncontrol.RevisionId {
}
}

func buildArchOnlyForCurrentlyRunningArch(archesToBuildFor OsArchesSpec) OsArchesSpec {
func buildArchOnlyForCurrentlyRunningArch(archesToBuildFor bobfile.OsArchesSpec) bobfile.OsArchesSpec {
devMachineArch := osArchCodeToOsArchesSpec(currentRunningGoOsArchToOsArchCode())

// to speed up dev, build only for the arch we're running now, but only if arches
Expand Down
6 changes: 4 additions & 2 deletions cmd/bob/devingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"fmt"
"os"
"strings"

"github.com/function61/turbobob/pkg/bobfile"
)

// uses Traefik-compliant notation to set up HTTP ingress to route traffic to the dev container
func setupDevIngress(
builder *BuilderSpec,
builder *bobfile.BuilderSpec,
ingressSettings devIngressSettings,
bobfile *Bobfile,
bobfile *bobfile.Bobfile,
) ([]string, string) {
if builder.DevHttpIngress == "" {
return nil, ""
Expand Down
7 changes: 4 additions & 3 deletions cmd/bob/devshim.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/function61/gokit/encoding/jsonfile"
"github.com/function61/gokit/os/osutil"
"github.com/function61/turbobob/pkg/bobfile"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -72,9 +73,9 @@ func shimSetup() error {
return err
}

bobfile, err := readBobfile()
bobfile, err := bobfile.Read()
if err != nil {
return fmt.Errorf("readBobfile: %w", err)
return err
}

shimConf, err := readShimConfig()
Expand Down Expand Up @@ -147,7 +148,7 @@ func shimSetup() error {
}

// so we can recall frequently needed commands fast + sets up HISTFILE ENV var
func injectHistory(builder BuilderSpec, alreadyDone bool, baseImgConf BaseImageConfig) error {
func injectHistory(builder bobfile.BuilderSpec, alreadyDone bool, baseImgConf BaseImageConfig) error {
userHomeDir, err := os.UserHomeDir()
if err != nil {
return err
Expand Down
19 changes: 10 additions & 9 deletions cmd/bob/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

. "github.com/function61/gokit/builtin"
"github.com/function61/turbobob/pkg/bobfile"
"github.com/function61/turbobob/pkg/dockertag"
"github.com/function61/turbobob/pkg/versioncontrol"
)
Expand All @@ -24,21 +25,21 @@ func isDevContainerRunning(containerName string) bool {
return strings.TrimRight(string(result), "\n") == "true"
}

func devContainerName(bobfile *Bobfile, builder BuilderSpec) string {
func devContainerName(bobfile *bobfile.Bobfile, builder bobfile.BuilderSpec) string {
return containerNameInternal("dev", bobfile, builder)
}

func langServerContainerName(bobfile *Bobfile, builder BuilderSpec) string {
func langServerContainerName(bobfile *bobfile.Bobfile, builder bobfile.BuilderSpec) string {
return containerNameInternal("langserver", bobfile, builder)
}

// do not use directly
func containerNameInternal(kind string, bobfile *Bobfile, builder BuilderSpec) string {
func containerNameInternal(kind string, bobfile *bobfile.Bobfile, builder bobfile.BuilderSpec) string {
return fmt.Sprintf("tb%s-%s-%s", kind, bobfile.ProjectName, builder.Name)

}

func builderImageName(bobfile *Bobfile, builder BuilderSpec) string {
func builderImageName(bobfile *bobfile.Bobfile, builder bobfile.BuilderSpec) string {
builderType, ref, err := parseBuilderUsesType(builder.Uses)
if err != nil {
panic(err)
Expand All @@ -54,7 +55,7 @@ func builderImageName(bobfile *Bobfile, builder BuilderSpec) string {
}
}

func buildBuilder(bobfile *Bobfile, builder *BuilderSpec) error {
func buildBuilder(bobfile *bobfile.Bobfile, builder *bobfile.BuilderSpec) error {
imageName := builderImageName(bobfile, *builder)

builderUsesType, dockerfilePath, err := parseBuilderUsesType(builder.Uses)
Expand Down Expand Up @@ -114,9 +115,9 @@ func buildBuilder(bobfile *Bobfile, builder *BuilderSpec) error {
func dockerRelayEnvVars(
dockerArgs []string,
revisionId *versioncontrol.RevisionId,
builder BuilderSpec,
builder bobfile.BuilderSpec,
envsAreRequired bool,
osArches OsArchesSpec,
osArches bobfile.OsArchesSpec,
fastbuild bool,
debug bool,
) ([]string, error) {
Expand Down Expand Up @@ -166,7 +167,7 @@ func dockerRelayEnvVars(
return dockerArgs, nil
}

func loginToDockerRegistry(dockerImage DockerImageSpec, cache *dockerRegistryLoginCache) error {
func loginToDockerRegistry(dockerImage bobfile.DockerImageSpec, cache *dockerRegistryLoginCache) error {
credentialsObtainer := getDockerCredentialsObtainer(dockerImage)
creds, err := credentialsObtainer.Obtain()
if err != nil {
Expand All @@ -179,7 +180,7 @@ func loginToDockerRegistry(dockerImage DockerImageSpec, cache *dockerRegistryLog

tagParsed := dockertag.Parse(dockerImage.Image)
if tagParsed == nil {
return ErrUnableToParseDockerTag
return bobfile.ErrUnableToParseDockerTag
}

registryDefaulted := tagParsed.Registry
Expand Down
6 changes: 4 additions & 2 deletions cmd/bob/dockercredentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"os"
"regexp"

"github.com/function61/turbobob/pkg/bobfile"
)

type DockerCredentials struct {
Expand All @@ -28,7 +30,7 @@ func (d *credsFromENV) Obtain() (*DockerCredentials, error) {

credsParts := credsFromENVRe.FindStringSubmatch(serialized)
if len(credsParts) != 3 {
return nil, ErrInvalidDockerCredsEnvFormat
return nil, bobfile.ErrInvalidDockerCredsEnvFormat
}

return &DockerCredentials{
Expand All @@ -37,7 +39,7 @@ func (d *credsFromENV) Obtain() (*DockerCredentials, error) {
}, nil
}

func getDockerCredentialsObtainer(dockerImage DockerImageSpec) DockerCredentialObtainer {
func getDockerCredentialsObtainer(dockerImage bobfile.DockerImageSpec) DockerCredentialObtainer {
if dockerImage.AuthType == nil {
return &credsFromENV{}
}
Expand Down
25 changes: 13 additions & 12 deletions cmd/bob/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

. "github.com/function61/gokit/builtin"
"github.com/function61/gokit/os/osutil"
"github.com/function61/turbobob/pkg/bobfile"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -82,26 +83,26 @@ func writeDefaultBobfile(producesDockerImage bool) error {
// guess project name from current workdir's basename
projectName := filepath.Base(cwd)

dockerImages := []DockerImageSpec{}
dockerImages := []bobfile.DockerImageSpec{}
if producesDockerImage {
dockerImages = append(dockerImages, DockerImageSpec{
dockerImages = append(dockerImages, bobfile.DockerImageSpec{
Image: "yourcompany/" + projectName,
DockerfilePath: "Dockerfile",
})
}

defaults := Bobfile{
FileDescriptionBoilerplate: fileDescriptionBoilerplate,
VersionMajor: currentVersionMajor,
defaults := bobfile.Bobfile{
FileDescriptionBoilerplate: bobfile.FileDescriptionBoilerplate,
VersionMajor: bobfile.CurrentVersionMajor,
ProjectName: projectName,
Builders: []BuilderSpec{
Builders: []bobfile.BuilderSpec{
{
Name: "default",
Uses: "dockerfile://build-default.Dockerfile",
MountSource: "",
MountDestination: "/app",
PassEnvs: []string{},
Commands: BuilderCommands{
Commands: bobfile.BuilderCommands{
Dev: []string{"bash"},
},
DevProTips: []string{},
Expand All @@ -115,27 +116,27 @@ func writeDefaultBobfile(producesDockerImage bool) error {
return writeBobfileIfNotExists(defaults)
}

func writeBobfileIfNotExists(content Bobfile) error {
exists, errExistsCheck := osutil.Exists(bobfileName)
func writeBobfileIfNotExists(content bobfile.Bobfile) error {
exists, errExistsCheck := osutil.Exists(bobfile.Name)
if errExistsCheck != nil {
return errExistsCheck
}

if exists {
return ErrInitBobfileExists
return bobfile.ErrInitBobfileExists
}

asJson, errJson := json.MarshalIndent(&content, "", "\t")
if errJson != nil {
return errJson
}

if err := os.MkdirAll(filepath.Dir(bobfileName), 0755); err != nil {
if err := os.MkdirAll(filepath.Dir(bobfile.Name), 0755); err != nil {
return err
}

return ioutil.WriteFile(
bobfileName,
bobfile.Name,
[]byte(fmt.Sprintf("%s\n", asJson)),
osutil.FileMode(osutil.OwnerRW, osutil.GroupRW, osutil.OtherNone))
}
Expand Down
11 changes: 6 additions & 5 deletions cmd/bob/initguess.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"

. "github.com/function61/gokit/builtin"
"github.com/function61/turbobob/pkg/bobfile"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/moby/buildkit/frontend/dockerfile/parser"
)
Expand Down Expand Up @@ -63,16 +64,16 @@ func initGuessFromDockerfile() error {
return err
}

return writeBobfileIfNotExists(Bobfile{
FileDescriptionBoilerplate: fileDescriptionBoilerplate,
VersionMajor: currentVersionMajor,
return writeBobfileIfNotExists(bobfile.Bobfile{
FileDescriptionBoilerplate: bobfile.FileDescriptionBoilerplate,
VersionMajor: bobfile.CurrentVersionMajor,
ProjectName: projectName,
Builders: []BuilderSpec{
Builders: []bobfile.BuilderSpec{
{
Name: "default",
Uses: "docker://" + builderStage.BaseName,
MountDestination: copyCommand.DestPath,
Commands: BuilderCommands{
Commands: bobfile.BuilderCommands{
Build: buildCommand,
Dev: []string{"bash"}, // just a guess
},
Expand Down
Loading

0 comments on commit 1c822d2

Please sign in to comment.