From 5c8f0c4a72cbe143bdc0bc405d53baff7ade66d9 Mon Sep 17 00:00:00 2001 From: Horiodino Date: Tue, 12 Nov 2024 13:07:44 +0530 Subject: [PATCH 1/2] fix --df-swap functionality for bsf oci #119 Signed-off-by: Horiodino --- cmd/oci/oci.go | 22 +++++++++++------ pkg/builddocker/build.go | 13 +++------- pkg/builddocker/build_test.go | 46 ++++++++++++----------------------- 3 files changed, 34 insertions(+), 47 deletions(-) diff --git a/cmd/oci/oci.go b/cmd/oci/oci.go index 50dc6a81..ca5339ab 100644 --- a/cmd/oci/oci.go +++ b/cmd/oci/oci.go @@ -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") @@ -83,9 +82,18 @@ 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) } @@ -93,7 +101,6 @@ var OCICmd = &cobra.Command{ } else { fmt.Println(styles.HintStyle.Render("hint:", "use --tag flag to define a tag")) } - os.Exit(1) } sc, fh, err := binit.GetBSFInitializers() @@ -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) @@ -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" @@ -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 } diff --git a/pkg/builddocker/build.go b/pkg/builddocker/build.go index 4aa50de5..0a41e997 100644 --- a/pkg/builddocker/build.go +++ b/pkg/builddocker/build.go @@ -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 } @@ -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 diff --git a/pkg/builddocker/build_test.go b/pkg/builddocker/build_test.go index 405a28d3..3f6fa9c6 100644 --- a/pkg/builddocker/build_test.go +++ b/pkg/builddocker/build_test.go @@ -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, @@ -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 { From c1cb38d3148311e8eeacdc14de85ea39cfce4d79 Mon Sep 17 00:00:00 2001 From: Horiodino Date: Thu, 14 Nov 2024 12:21:41 +0530 Subject: [PATCH 2/2] deprecate --df-swap cmd Signed-off-by: Horiodino --- cmd/oci/oci.go | 58 +++--------------------------- pkg/builddocker/build.go | 67 ----------------------------------- pkg/builddocker/build_test.go | 66 ---------------------------------- 3 files changed, 4 insertions(+), 187 deletions(-) delete mode 100644 pkg/builddocker/build_test.go diff --git a/cmd/oci/oci.go b/cmd/oci/oci.go index ca5339ab..9c7ed6ab 100644 --- a/cmd/oci/oci.go +++ b/cmd/oci/oci.go @@ -21,8 +21,8 @@ import ( ) var ( - platform, output, tag, path, destcreds string - push, loadDocker, loadPodman, devDeps, dfSwap, digest bool + platform, output, tag, destcreds string + push, loadDocker, loadPodman, devDeps, digest bool ) var supportedPlatforms = []string{"linux/amd64", "linux/arm64"} @@ -33,10 +33,8 @@ func init() { OCICmd.Flags().BoolVarP(&loadDocker, "load-docker", "", false, "Load the image into docker daemon") OCICmd.Flags().BoolVarP(&loadPodman, "load-podman", "", false, "Load the image into podman") OCICmd.Flags().BoolVarP(&push, "push", "", false, "Push the image to the registry") + OCICmd.Flags().StringVarP(&tag, "tag", "t", "", "New tag for the image") OCICmd.Flags().BoolVarP(&devDeps, "dev", "", false, "Build base image for Dev Dependencies") - OCICmd.Flags().BoolVarP(&dfSwap, "df-swap", "", false, "Modify base images in Dockerfile") - OCICmd.Flags().StringVarP(&tag, "tag", "t", "", "The tag that will be replaced with original tag in Dockerfile") - OCICmd.Flags().StringVar(&path, "path", "", "The path to Dockerfile") OCICmd.Flags().BoolVar(&digest, "digest", false, "push image by digest") OCICmd.Flags().StringVar(&destcreds, "dest-creds", "", "Authenticate to the registry") } @@ -73,7 +71,7 @@ var OCICmd = &cobra.Command{ platform = p - if tag != "" && !dfSwap { + if tag != "" { newName, err := getNewName(artifact, tag) if err != nil { fmt.Println(styles.ErrorStyle.Render("error: ", err.Error())) @@ -82,27 +80,6 @@ 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, 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")) - } - } - sc, fh, err := binit.GetBSFInitializers() if err != nil { fmt.Println(styles.ErrorStyle.Render("error: ", err.Error())) @@ -284,33 +261,6 @@ func ProcessPlatformAndConfig(conf *hcl2nix.Config, plat string, envName string) return artifact, plat, nil } -func modifyDockerfileWithTag(path, tag, ociArtifactName string) error { - var dockerfilePath string - if path != "" { - dockerfilePath = path + "/Dockerfile" - } else { - dockerfilePath = "./Dockerfile" - } - - file, err := os.Open(dockerfilePath) - if err != nil { - return err - } - defer file.Close() - - resLines, err := builddocker.ModifyDockerfile(file, ociArtifactName, tag) - if err != nil { - return err - } - - err = os.WriteFile(dockerfilePath, []byte(strings.Join(resLines, "\n")), 0644) - if err != nil { - return err - } - - return nil -} - func getNewName(artifact hcl2nix.OCIArtifact, tag string) (string, error) { var newName string if strings.Contains(artifact.Name, ":") { diff --git a/pkg/builddocker/build.go b/pkg/builddocker/build.go index 0a41e997..7da9fe7a 100644 --- a/pkg/builddocker/build.go +++ b/pkg/builddocker/build.go @@ -1,11 +1,9 @@ package builddocker import ( - "bufio" "fmt" "html/template" "io" - "os" "os/exec" "strings" @@ -44,71 +42,6 @@ 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, ociArgument string, tag string) ([]string, error) { - lines, err := readDockerFile(file) - if err != nil { - return nil, err - } - - reslines, err := editDockerfile(lines, ociArgument, tag) - if err != nil { - return nil, err - } - - return reslines, nil -} - -func readDockerFile(file *os.File) ([]string, error) { - scanner := bufio.NewScanner(file) - lines := []string{} - for scanner.Scan() { - lines = append(lines, scanner.Text()) - } - if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("error reading Dockerfile: %v", err) - } - return lines, nil -} - -func editDockerfile(lines []string, ociArtifactName, tag string) ([]string, error) { - var searchTag string - searchTag = ociArtifactName - - var selectedFrom string - var selectedIndex int - for i, line := range lines { - if strings.Contains(line, searchTag) { - selectedFrom = line - selectedIndex = i - break - } - } - - if selectedFrom == "" { - return nil, fmt.Errorf("no FROM command found with tag %s", searchTag) - } - - fromParts := strings.Fields(selectedFrom) - if len(fromParts) < 2 { - return nil, fmt.Errorf("invalid FROM command format") - } - - var newFrom string - if strings.Contains(fromParts[1], ":") { - imageParts := strings.Split(fromParts[1], ":") - newFrom = fmt.Sprintf("FROM %s:%s", imageParts[0], tag) - } else { - newFrom = fmt.Sprintf("FROM %s:%s", fromParts[1], tag) - } - for _, part := range fromParts[2:] { - newFrom = fmt.Sprintf("%s %s", newFrom, part) - } - - lines[selectedIndex] = newFrom - return lines, nil -} - func convertExportCfgToDockerfileCfg(env hcl2nix.OCIArtifact, platform string) dockerfileCfg { switch platform { case "linux/amd64": diff --git a/pkg/builddocker/build_test.go b/pkg/builddocker/build_test.go deleted file mode 100644 index 3f6fa9c6..00000000 --- a/pkg/builddocker/build_test.go +++ /dev/null @@ -1,66 +0,0 @@ -package builddocker - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestEditDockerFile(t *testing.T) { - tests := []struct { - ociArtifactName string - name string - lines []string - tag string - expectedRes []string - expectError bool - }{ - { - name: "FROM Command with bsf tag", - ociArtifactName: "docker.io/buildsafe/bsf", - lines: []string{ - "FROM docker.io/buildsafe/bsf:v1.1.1 AS build", - "RUN apt-get update", - }, - tag: "latest", - expectedRes: []string{ - "FROM docker.io/buildsafe/bsf:latest AS build", - "RUN apt-get update", - }, - expectError: false, - }, - { - name: "No FROM Command with bsf tag", - ociArtifactName: "docker.io/buildsafe/bsf", - lines: []string{ - "FROM ubuntu:latest", - "RUN apt-get update", - }, - tag: "latest", - expectedRes: nil, - expectError: true, - }, - { - name: "No FROM Command", - ociArtifactName: "docker.io/buildsafe/bsf", - lines: []string{ - "RUN apt-get update", - }, - tag: "latest", - expectedRes: nil, - expectError: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - res, err := editDockerfile(tt.lines, tt.ociArtifactName, tt.tag) - if tt.expectError { - assert.Error(t, err) - } else { - assert.NoError(t, err) - assert.Equal(t, tt.expectedRes, res) - } - }) - } -}