Skip to content

Commit

Permalink
deprecate --df-swap cmd
Browse files Browse the repository at this point in the history
Signed-off-by: Horiodino <[email protected]>
  • Loading branch information
Horiodino committed Nov 14, 2024
1 parent 5c8f0c4 commit c1cb38d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 187 deletions.
58 changes: 4 additions & 54 deletions cmd/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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()))
Expand All @@ -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()))
Expand Down Expand Up @@ -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, ":") {
Expand Down
67 changes: 0 additions & 67 deletions pkg/builddocker/build.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package builddocker

import (
"bufio"
"fmt"
"html/template"
"io"
"os"
"os/exec"
"strings"

Expand Down Expand Up @@ -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":
Expand Down
66 changes: 0 additions & 66 deletions pkg/builddocker/build_test.go

This file was deleted.

0 comments on commit c1cb38d

Please sign in to comment.