Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dockerfile edit #61

Merged
merged 5 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cmd/dockerfile/df.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var (
func init() {
DFCmd.Flags().StringVarP(&output, "output", "o", "", "location of the dockerfile generated")
DFCmd.Flags().StringVarP(&platform, "platform", "p", "", "The platform to build the image for")

}

// DFCmd represents the generate command
Expand All @@ -31,15 +30,16 @@ var DFCmd = &cobra.Command{
Short: "dockerfile generates a dockerfile for the app",
Aliases: []string{"df"},
Long: `
bsf dockerfile <environment name>
bsf dockerfile <environment name> --platform <platform>
bsf dockerfile <environment name> --platform <platform> --output <output filename>
bsf dockerfile <artifact>
bsf dockerfile <artifact> --platform <platform>
bsf dockerfile <artifact> --platform <platform> --output <output filename>
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println(styles.HintStyle.Render("hint:", "run `bsf dockerfile <environment name>` to export the environment"))
fmt.Println(styles.HintStyle.Render("hint:", "run `bsf dockerfile <artifact>` to export the environment"))
os.Exit(1)
}

env, p, err := ocicmd.ProcessPlatformAndConfig(platform, args[0])
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error: ", err.Error()))
Expand Down
21 changes: 19 additions & 2 deletions cmd/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
)

var (
platform, output string
push, loadDocker, loadPodman, devDeps bool
platform, output, tag, path string
push, loadDocker, loadPodman, devDeps, dfSwap bool
)
var (
supportedPlatforms = []string{"linux/amd64", "linux/arm64"}
Expand All @@ -36,6 +36,9 @@ func init() {
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().BoolVarP(&devDeps, "dev", "", false, "Build base image for Dev Dependencies")
OCICmd.Flags().BoolVarP(&dfSwap, "df-swap", "", false, "Build base image for Dev Dependencies")
rakshitgondwal marked this conversation as resolved.
Show resolved Hide resolved
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 represents the export command
Expand All @@ -61,6 +64,20 @@ var OCICmd = &cobra.Command{
}
platform = p

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

sc, fh, err := binit.GetBSFInitializers()
if err != nil {
fmt.Println(styles.ErrorStyle.Render("error: ", err.Error()))
Expand Down
77 changes: 77 additions & 0 deletions pkg/builddocker/build.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package builddocker

import (
"bufio"
"fmt"
"html/template"
"io"
Expand Down Expand Up @@ -44,6 +45,82 @@ func GenerateDockerfile(w io.Writer, env hcl2nix.OCIArtifact, platform string) e
return err
}

return nil

}

// ModifyDockerfile modifies the Dockerfile with the specified tag
func ModifyDockerfile(path, tag string, dev bool) error {
var dockerfilePath string
rakshitgondwal marked this conversation as resolved.
Show resolved Hide resolved
if path != "" {
dockerfilePath = path + "/Dockerfile"
} else {
dockerfilePath = "./Dockerfile"
}

if _, err := os.Stat(dockerfilePath); os.IsNotExist(err) {
return fmt.Errorf("dockerfile not found")
}

file, err := os.Open(dockerfilePath)
if err != nil {
return fmt.Errorf("error opening Dockerfile: %v", err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
rakshitgondwal marked this conversation as resolved.
Show resolved Hide resolved
lines := []string{}
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading Dockerfile: %v", err)
}

var searchTag string
rakshitgondwal marked this conversation as resolved.
Show resolved Hide resolved
if dev {
searchTag = "# bsfimage:dev"
} else {
searchTag = "# bsfimage:runtime"
}

var selectedFrom string
var selectedIndex int
for i, line := range lines {
if strings.Contains(line, searchTag) {
selectedFrom = line
selectedIndex = i
break
}
}

if selectedFrom == "" {
return fmt.Errorf("no FROM command found with tag %s", searchTag)
}

fromParts := strings.Fields(selectedFrom)
if len(fromParts) < 2 {
return 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

err = os.WriteFile(dockerfilePath, []byte(strings.Join(lines, "\n")), 0644)
if err != nil {
return fmt.Errorf("error writing to Dockerfile: %v", err)
}

return nil
}

Expand Down
Loading