Skip to content

Commit b4c3c00

Browse files
committed
extract bobfile into own package
1 parent 1603fab commit b4c3c00

File tree

18 files changed

+133
-97
lines changed

18 files changed

+133
-97
lines changed

cmd/bob/baseimageconf.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/function61/gokit/encoding/jsonfile"
1010
"github.com/function61/gokit/os/osutil"
11+
"github.com/function61/turbobob/pkg/bobfile"
1112
)
1213

1314
const (
@@ -16,9 +17,9 @@ const (
1617

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

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

@@ -54,7 +55,7 @@ func loadBaseImageConfWhenInsideContainer() (*BaseImageConfig, error) {
5455

5556
// non-optional because the implementation makes it a bit hard to check if the file exists
5657
// (vs. Docker run error), and our current callsite needs non-optional anyway
57-
func loadNonOptionalBaseImageConf(builder BuilderSpec) (*BaseImageConfig, error) {
58+
func loadNonOptionalBaseImageConf(builder bobfile.BuilderSpec) (*BaseImageConfig, error) {
5859
dockerImage, err := func() (string, error) {
5960
kind, data, err := parseBuilderUsesType(builder.Uses)
6061
if err != nil {

cmd/bob/build.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import (
1111
"time"
1212

1313
"github.com/function61/gokit/os/osutil"
14+
"github.com/function61/turbobob/pkg/bobfile"
1415
"github.com/function61/turbobob/pkg/versioncontrol"
1516
"github.com/spf13/cobra"
1617
)
1718

1819
type BuildContext struct {
19-
Bobfile *Bobfile
20+
Bobfile *bobfile.Bobfile
2021
OriginDir string // where the repo exists
2122
WorkspaceDir string // where the revision is being built
2223
PublishArtefacts bool
@@ -31,7 +32,7 @@ type BuildContext struct {
3132
IsDefaultBranch bool // whether we are in "main" / "master" or equivalent branch
3233
}
3334

34-
func runBuilder(builder BuilderSpec, buildCtx *BuildContext, opDesc string, cmdToRun []string) error {
35+
func runBuilder(builder bobfile.BuilderSpec, buildCtx *BuildContext, opDesc string, cmdToRun []string) error {
3536
wd, errWd := os.Getwd()
3637
if errWd != nil {
3738
return errWd
@@ -125,7 +126,7 @@ func runBuilder(builder BuilderSpec, buildCtx *BuildContext, opDesc string, cmdT
125126
return nil
126127
}
127128

128-
func buildAndPushOneDockerImage(dockerImage DockerImageSpec, buildCtx *BuildContext) error {
129+
func buildAndPushOneDockerImage(dockerImage bobfile.DockerImageSpec, buildCtx *BuildContext) error {
129130
tagWithoutVersion := dockerImage.Image
130131
tag := tagWithoutVersion + ":" + buildCtx.RevisionId.FriendlyRevisionId
131132
tagLatest := tagWithoutVersion + ":latest"
@@ -362,7 +363,7 @@ func build(buildCtx *BuildContext) error {
362363
}
363364

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

384-
preparePass := func(cmds BuilderCommands) []string { return cmds.Prepare }
385-
buildPass := func(cmds BuilderCommands) []string { return cmds.Build }
386-
publishPass := func(cmds BuilderCommands) []string { return cmds.Publish }
385+
preparePass := func(cmds bobfile.BuilderCommands) []string { return cmds.Prepare }
386+
buildPass := func(cmds bobfile.BuilderCommands) []string { return cmds.Build }
387+
publishPass := func(cmds bobfile.BuilderCommands) []string { return cmds.Publish }
387388

388389
if err := pass("prepare", preparePass); err != nil {
389390
return err // err context ok
@@ -426,7 +427,7 @@ func constructBuildContext(
426427
fastBuild bool,
427428
areWeInCi bool,
428429
) (*BuildContext, error) {
429-
bobfile, err := readBobfile()
430+
bobfile, err := bobfile.Read()
430431
if err != nil {
431432
return nil, err
432433
}
@@ -579,7 +580,7 @@ func buildInsideEntry() *cobra.Command {
579580
}
580581

581582
func buildInside(fastBuild bool) error {
582-
bobfile, err := readBobfile()
583+
bobfile, err := bobfile.Read()
583584
if err != nil {
584585
return err
585586
}

cmd/bob/dev.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import (
1111

1212
"github.com/function61/gokit/encoding/jsonfile"
1313
"github.com/function61/gokit/os/osutil"
14+
"github.com/function61/turbobob/pkg/bobfile"
1415
"github.com/function61/turbobob/pkg/versioncontrol"
1516
"github.com/spf13/cobra"
1617
)
1718

1819
func devCommand(builderName string, envsAreRequired bool, ignoreNag bool) ([]string, error) {
19-
bobfile, err := readBobfile()
20+
bobfile, err := bobfile.Read()
2021
if err != nil {
2122
return nil, err
2223
}
@@ -264,7 +265,7 @@ func revisionIdForDev() *versioncontrol.RevisionId {
264265
}
265266
}
266267

267-
func buildArchOnlyForCurrentlyRunningArch(archesToBuildFor OsArchesSpec) OsArchesSpec {
268+
func buildArchOnlyForCurrentlyRunningArch(archesToBuildFor bobfile.OsArchesSpec) bobfile.OsArchesSpec {
268269
devMachineArch := osArchCodeToOsArchesSpec(currentRunningGoOsArchToOsArchCode())
269270

270271
// to speed up dev, build only for the arch we're running now, but only if arches

cmd/bob/devingress.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"fmt"
55
"os"
66
"strings"
7+
8+
"github.com/function61/turbobob/pkg/bobfile"
79
)
810

911
// uses Traefik-compliant notation to set up HTTP ingress to route traffic to the dev container
1012
func setupDevIngress(
11-
builder *BuilderSpec,
13+
builder *bobfile.BuilderSpec,
1214
ingressSettings devIngressSettings,
13-
bobfile *Bobfile,
15+
bobfile *bobfile.Bobfile,
1416
) ([]string, string) {
1517
if builder.DevHttpIngress == "" {
1618
return nil, ""

cmd/bob/devshim.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/function61/gokit/encoding/jsonfile"
1414
"github.com/function61/gokit/os/osutil"
15+
"github.com/function61/turbobob/pkg/bobfile"
1516
"github.com/spf13/cobra"
1617
)
1718

@@ -72,9 +73,9 @@ func shimSetup() error {
7273
return err
7374
}
7475

75-
bobfile, err := readBobfile()
76+
bobfile, err := bobfile.Read()
7677
if err != nil {
77-
return fmt.Errorf("readBobfile: %w", err)
78+
return err
7879
}
7980

8081
shimConf, err := readShimConfig()
@@ -147,7 +148,7 @@ func shimSetup() error {
147148
}
148149

149150
// so we can recall frequently needed commands fast + sets up HISTFILE ENV var
150-
func injectHistory(builder BuilderSpec, alreadyDone bool, baseImgConf BaseImageConfig) error {
151+
func injectHistory(builder bobfile.BuilderSpec, alreadyDone bool, baseImgConf BaseImageConfig) error {
151152
userHomeDir, err := os.UserHomeDir()
152153
if err != nil {
153154
return err

cmd/bob/docker.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
. "github.com/function61/gokit/builtin"
13+
"github.com/function61/turbobob/pkg/bobfile"
1314
"github.com/function61/turbobob/pkg/dockertag"
1415
"github.com/function61/turbobob/pkg/versioncontrol"
1516
)
@@ -24,21 +25,21 @@ func isDevContainerRunning(containerName string) bool {
2425
return strings.TrimRight(string(result), "\n") == "true"
2526
}
2627

27-
func devContainerName(bobfile *Bobfile, builder BuilderSpec) string {
28+
func devContainerName(bobfile *bobfile.Bobfile, builder bobfile.BuilderSpec) string {
2829
return containerNameInternal("dev", bobfile, builder)
2930
}
3031

31-
func langServerContainerName(bobfile *Bobfile, builder BuilderSpec) string {
32+
func langServerContainerName(bobfile *bobfile.Bobfile, builder bobfile.BuilderSpec) string {
3233
return containerNameInternal("langserver", bobfile, builder)
3334
}
3435

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

3940
}
4041

41-
func builderImageName(bobfile *Bobfile, builder BuilderSpec) string {
42+
func builderImageName(bobfile *bobfile.Bobfile, builder bobfile.BuilderSpec) string {
4243
builderType, ref, err := parseBuilderUsesType(builder.Uses)
4344
if err != nil {
4445
panic(err)
@@ -54,7 +55,7 @@ func builderImageName(bobfile *Bobfile, builder BuilderSpec) string {
5455
}
5556
}
5657

57-
func buildBuilder(bobfile *Bobfile, builder *BuilderSpec) error {
58+
func buildBuilder(bobfile *bobfile.Bobfile, builder *bobfile.BuilderSpec) error {
5859
imageName := builderImageName(bobfile, *builder)
5960

6061
builderUsesType, dockerfilePath, err := parseBuilderUsesType(builder.Uses)
@@ -114,9 +115,9 @@ func buildBuilder(bobfile *Bobfile, builder *BuilderSpec) error {
114115
func dockerRelayEnvVars(
115116
dockerArgs []string,
116117
revisionId *versioncontrol.RevisionId,
117-
builder BuilderSpec,
118+
builder bobfile.BuilderSpec,
118119
envsAreRequired bool,
119-
osArches OsArchesSpec,
120+
osArches bobfile.OsArchesSpec,
120121
fastbuild bool,
121122
debug bool,
122123
) ([]string, error) {
@@ -166,7 +167,7 @@ func dockerRelayEnvVars(
166167
return dockerArgs, nil
167168
}
168169

169-
func loginToDockerRegistry(dockerImage DockerImageSpec, cache *dockerRegistryLoginCache) error {
170+
func loginToDockerRegistry(dockerImage bobfile.DockerImageSpec, cache *dockerRegistryLoginCache) error {
170171
credentialsObtainer := getDockerCredentialsObtainer(dockerImage)
171172
creds, err := credentialsObtainer.Obtain()
172173
if err != nil {
@@ -179,7 +180,7 @@ func loginToDockerRegistry(dockerImage DockerImageSpec, cache *dockerRegistryLog
179180

180181
tagParsed := dockertag.Parse(dockerImage.Image)
181182
if tagParsed == nil {
182-
return ErrUnableToParseDockerTag
183+
return bobfile.ErrUnableToParseDockerTag
183184
}
184185

185186
registryDefaulted := tagParsed.Registry

cmd/bob/dockercredentials.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"errors"
55
"os"
66
"regexp"
7+
8+
"github.com/function61/turbobob/pkg/bobfile"
79
)
810

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

2931
credsParts := credsFromENVRe.FindStringSubmatch(serialized)
3032
if len(credsParts) != 3 {
31-
return nil, ErrInvalidDockerCredsEnvFormat
33+
return nil, bobfile.ErrInvalidDockerCredsEnvFormat
3234
}
3335

3436
return &DockerCredentials{
@@ -37,7 +39,7 @@ func (d *credsFromENV) Obtain() (*DockerCredentials, error) {
3739
}, nil
3840
}
3941

40-
func getDockerCredentialsObtainer(dockerImage DockerImageSpec) DockerCredentialObtainer {
42+
func getDockerCredentialsObtainer(dockerImage bobfile.DockerImageSpec) DockerCredentialObtainer {
4143
if dockerImage.AuthType == nil {
4244
return &credsFromENV{}
4345
}

cmd/bob/init.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
. "github.com/function61/gokit/builtin"
1111
"github.com/function61/gokit/os/osutil"
12+
"github.com/function61/turbobob/pkg/bobfile"
1213
"github.com/spf13/cobra"
1314
)
1415

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

85-
dockerImages := []DockerImageSpec{}
86+
dockerImages := []bobfile.DockerImageSpec{}
8687
if producesDockerImage {
87-
dockerImages = append(dockerImages, DockerImageSpec{
88+
dockerImages = append(dockerImages, bobfile.DockerImageSpec{
8889
Image: "yourcompany/" + projectName,
8990
DockerfilePath: "Dockerfile",
9091
})
9192
}
9293

93-
defaults := Bobfile{
94-
FileDescriptionBoilerplate: fileDescriptionBoilerplate,
95-
VersionMajor: currentVersionMajor,
94+
defaults := bobfile.Bobfile{
95+
FileDescriptionBoilerplate: bobfile.FileDescriptionBoilerplate,
96+
VersionMajor: bobfile.CurrentVersionMajor,
9697
ProjectName: projectName,
97-
Builders: []BuilderSpec{
98+
Builders: []bobfile.BuilderSpec{
9899
{
99100
Name: "default",
100101
Uses: "dockerfile://build-default.Dockerfile",
101102
MountSource: "",
102103
MountDestination: "/app",
103104
PassEnvs: []string{},
104-
Commands: BuilderCommands{
105+
Commands: bobfile.BuilderCommands{
105106
Dev: []string{"bash"},
106107
},
107108
DevProTips: []string{},
@@ -115,27 +116,27 @@ func writeDefaultBobfile(producesDockerImage bool) error {
115116
return writeBobfileIfNotExists(defaults)
116117
}
117118

118-
func writeBobfileIfNotExists(content Bobfile) error {
119-
exists, errExistsCheck := osutil.Exists(bobfileName)
119+
func writeBobfileIfNotExists(content bobfile.Bobfile) error {
120+
exists, errExistsCheck := osutil.Exists(bobfile.Name)
120121
if errExistsCheck != nil {
121122
return errExistsCheck
122123
}
123124

124125
if exists {
125-
return ErrInitBobfileExists
126+
return bobfile.ErrInitBobfileExists
126127
}
127128

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

133-
if err := os.MkdirAll(filepath.Dir(bobfileName), 0755); err != nil {
134+
if err := os.MkdirAll(filepath.Dir(bobfile.Name), 0755); err != nil {
134135
return err
135136
}
136137

137138
return ioutil.WriteFile(
138-
bobfileName,
139+
bobfile.Name,
139140
[]byte(fmt.Sprintf("%s\n", asJson)),
140141
osutil.FileMode(osutil.OwnerRW, osutil.GroupRW, osutil.OtherNone))
141142
}

cmd/bob/initguess.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"path/filepath"
1010

1111
. "github.com/function61/gokit/builtin"
12+
"github.com/function61/turbobob/pkg/bobfile"
1213
"github.com/moby/buildkit/frontend/dockerfile/instructions"
1314
"github.com/moby/buildkit/frontend/dockerfile/parser"
1415
)
@@ -63,16 +64,16 @@ func initGuessFromDockerfile() error {
6364
return err
6465
}
6566

66-
return writeBobfileIfNotExists(Bobfile{
67-
FileDescriptionBoilerplate: fileDescriptionBoilerplate,
68-
VersionMajor: currentVersionMajor,
67+
return writeBobfileIfNotExists(bobfile.Bobfile{
68+
FileDescriptionBoilerplate: bobfile.FileDescriptionBoilerplate,
69+
VersionMajor: bobfile.CurrentVersionMajor,
6970
ProjectName: projectName,
70-
Builders: []BuilderSpec{
71+
Builders: []bobfile.BuilderSpec{
7172
{
7273
Name: "default",
7374
Uses: "docker://" + builderStage.BaseName,
7475
MountDestination: copyCommand.DestPath,
75-
Commands: BuilderCommands{
76+
Commands: bobfile.BuilderCommands{
7677
Build: buildCommand,
7778
Dev: []string{"bash"}, // just a guess
7879
},

0 commit comments

Comments
 (0)