Skip to content

Commit

Permalink
fix --df-swap functionality for bsf oci #119
Browse files Browse the repository at this point in the history
Signed-off-by: Horiodino <[email protected]>
  • Loading branch information
Horiodino authored and dr-housemd committed Nov 18, 2024
1 parent 0d7040c commit 2b6d852
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 47 deletions.
22 changes: 14 additions & 8 deletions cmd/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ var (
platform, output, tag, path, destcreds string
push, loadDocker, loadPodman, devDeps, dfSwap, digest bool
)
var (
supportedPlatforms = []string{"linux/amd64", "linux/arm64"}
)

var supportedPlatforms = []string{"linux/amd64", "linux/arm64"}

func init() {
OCICmd.Flags().StringVarP(&platform, "platform", "p", "", "The platform to build the image for")
Expand Down Expand Up @@ -83,17 +82,25 @@ var OCICmd = &cobra.Command{
artifact.Name = newName
}

var ociArtifactName string

for _, ociArtifact := range conf.OCIArtifact {
if ociArtifact.Artifact == args[0] {
prefix := strings.Split(ociArtifact.Name, ":")[0]
ociArtifactName = prefix
}
}

if dfSwap {
if tag != "" {
if err = modifyDockerfileWithTag(path, tag); err != nil {
if err = modifyDockerfileWithTag(path, tag, ociArtifactName); err != nil {
fmt.Println(styles.ErrorStyle.Render("error: ", err.Error()))
os.Exit(1)
}
fmt.Println(styles.SucessStyle.Render("dockerfile succesfully updated with tag:", tag))
} else {
fmt.Println(styles.HintStyle.Render("hint:", "use --tag flag to define a tag"))
}
os.Exit(1)
}

sc, fh, err := binit.GetBSFInitializers()
Expand All @@ -102,7 +109,6 @@ var OCICmd = &cobra.Command{
os.Exit(1)
}
err = generate.Generate(fh, sc)

if err != nil {
fmt.Println(styles.ErrorStyle.Render("error: ", err.Error()))
os.Exit(1)
Expand Down Expand Up @@ -278,7 +284,7 @@ func ProcessPlatformAndConfig(conf *hcl2nix.Config, plat string, envName string)
return artifact, plat, nil
}

func modifyDockerfileWithTag(path, tag string) error {
func modifyDockerfileWithTag(path, tag, ociArtifactName string) error {
var dockerfilePath string
if path != "" {
dockerfilePath = path + "/Dockerfile"
Expand All @@ -292,7 +298,7 @@ func modifyDockerfileWithTag(path, tag string) error {
}
defer file.Close()

resLines, err := builddocker.ModifyDockerfile(file, devDeps, tag)
resLines, err := builddocker.ModifyDockerfile(file, ociArtifactName, tag)
if err != nil {
return err
}
Expand Down
13 changes: 4 additions & 9 deletions pkg/builddocker/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,16 @@ func GenerateDockerfile(w io.Writer, env hcl2nix.OCIArtifact, platform string) e
}

return nil

}

// ModifyDockerfile modifies the Dockerfile with the specified tag
func ModifyDockerfile(file *os.File, dev bool, tag string) ([]string, error) {
func ModifyDockerfile(file *os.File, ociArgument string, tag string) ([]string, error) {
lines, err := readDockerFile(file)
if err != nil {
return nil, err
}

reslines, err := editDockerfile(lines, dev, tag)
reslines, err := editDockerfile(lines, ociArgument, tag)
if err != nil {
return nil, err
}
Expand All @@ -72,13 +71,9 @@ func readDockerFile(file *os.File) ([]string, error) {
return lines, nil
}

func editDockerfile(lines []string, dev bool, tag string) ([]string, error) {
func editDockerfile(lines []string, ociArtifactName, tag string) ([]string, error) {
var searchTag string
if dev {
searchTag = "bsfimage:dev"
} else {
searchTag = "bsfimage:runtime"
}
searchTag = ociArtifactName

var selectedFrom string
var selectedIndex int
Expand Down
46 changes: 16 additions & 30 deletions pkg/builddocker/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,44 @@ import (

func TestEditDockerFile(t *testing.T) {
tests := []struct {
name string
lines []string
isDev bool
tag string
expectedRes []string
expectError bool
ociArtifactName string
name string
lines []string
tag string
expectedRes []string
expectError bool
}{
{
name: "Dev",
name: "FROM Command with bsf tag",
ociArtifactName: "docker.io/buildsafe/bsf",
lines: []string{
"FROM ubuntu:18.04 # bsfimage:dev",
"FROM docker.io/buildsafe/bsf:v1.1.1 AS build",
"RUN apt-get update",
},
isDev: true,
tag: "latest",
tag: "latest",
expectedRes: []string{
"FROM ubuntu:latest # bsfimage:dev",
"FROM docker.io/buildsafe/bsf:latest AS build",
"RUN apt-get update",
},
expectError: false,
},
{
name: "Runtime",
lines: []string{
"FROM ubuntu:18.04 # bsfimage:runtime",
"RUN apt-get update",
},
isDev: false,
tag: "latest",
expectedRes: []string{
"FROM ubuntu:latest # bsfimage:runtime",
"RUN apt-get update",
},
expectError: false,
},
{
name: "No FROM Command with bsf tag",
name: "No FROM Command with bsf tag",
ociArtifactName: "docker.io/buildsafe/bsf",
lines: []string{
"FROM ubuntu:latest",
"RUN apt-get update",
},
isDev: true,
tag: "latest",
expectedRes: nil,
expectError: true,
},
{
name: "No FROM Command",
name: "No FROM Command",
ociArtifactName: "docker.io/buildsafe/bsf",
lines: []string{
"RUN apt-get update",
},
isDev: true,
tag: "latest",
expectedRes: nil,
expectError: true,
Expand All @@ -68,7 +54,7 @@ func TestEditDockerFile(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res, err := editDockerfile(tt.lines, tt.isDev, tt.tag)
res, err := editDockerfile(tt.lines, tt.ociArtifactName, tt.tag)
if tt.expectError {
assert.Error(t, err)
} else {
Expand Down

0 comments on commit 2b6d852

Please sign in to comment.