Skip to content

Define dockerfile instructions set centrally. #103

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 5 additions & 6 deletions pkg/docker/dockerfile/ast/line_parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import (
"encoding/json"
"errors"
"fmt"
df "github.com/mintoolkit/mint/pkg/docker/dockerfile"
"strings"
"unicode"
"unicode/utf8"

"github.com/mintoolkit/mint/pkg/docker/instruction"
)

type NameValError struct {
Expand Down Expand Up @@ -76,8 +75,8 @@ func parseSubCommand(rest string, d *Directive) (*Node, map[string]bool, error)
return &Node{Children: []*Node{child}}, nil, nil
}

// helper to parse words (i.e space delimited or quoted strings) in a statement.
// The quotes are preserved as part of this function and they are stripped later
// helper to parse words (i.e. space delimited or quoted strings) in a statement.
// The quotes are preserved as part of this function, and they are stripped later
// as part of processWords().
func parseWords(rest string, d *Directive) []string {
const (
Expand Down Expand Up @@ -224,12 +223,12 @@ func appendKeyValueNode(node, rootNode, prevNode *Node) (*Node, *Node) {
}

func parseEnv(rest string, d *Directive) (*Node, map[string]bool, error) {
node, err := parseNameVal(rest, instruction.Env, d)
node, err := parseNameVal(rest, df.InstTypeEnv, d)
return node, nil, err
}

func parseLabel(rest string, d *Directive) (*Node, map[string]bool, error) {
node, err := parseNameVal(rest, instruction.Label, d)
node, err := parseNameVal(rest, df.InstTypeLabel, d)
return node, nil, err
}

Expand Down
38 changes: 19 additions & 19 deletions pkg/docker/dockerfile/ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"bufio"
"bytes"
"fmt"
df "github.com/mintoolkit/mint/pkg/docker/dockerfile"
"io"
"regexp"
"strconv"
"strings"
"unicode"

"github.com/mintoolkit/mint/pkg/docker/instruction"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -190,24 +190,24 @@ func init() {
// functions. Errors are propagated up by Parse() and the resulting AST can
// be incorporated directly into the existing AST as a next.
dispatch = map[string]func(string, *Directive) (*Node, map[string]bool, error){
instruction.Add: parseMaybeJSONToList,
instruction.Arg: parseNameOrNameVal,
instruction.Cmd: parseMaybeJSON,
instruction.Copy: parseMaybeJSONToList,
instruction.Entrypoint: parseMaybeJSON,
instruction.Env: parseEnv,
instruction.Expose: parseStringsWhitespaceDelimited,
instruction.From: parseStringsWhitespaceDelimited,
instruction.Healthcheck: parseHealthConfig,
instruction.Label: parseLabel,
instruction.Maintainer: parseString,
instruction.Onbuild: parseSubCommand,
instruction.Run: parseMaybeJSON,
instruction.Shell: parseMaybeJSON,
instruction.StopSignal: parseString,
instruction.User: parseString,
instruction.Volume: parseMaybeJSONToList,
instruction.Workdir: parseString,
df.InstTypeAdd: parseMaybeJSONToList,
df.InstTypeArg: parseNameOrNameVal,
df.InstTypeCmd: parseMaybeJSON,
df.InstTypeCopy: parseMaybeJSONToList,
df.InstTypeEntrypoint: parseMaybeJSON,
df.InstTypeEnv: parseEnv,
df.InstTypeExpose: parseStringsWhitespaceDelimited,
df.InstTypeFrom: parseStringsWhitespaceDelimited,
df.InstTypeHealthcheck: parseHealthConfig,
df.InstTypeLabel: parseLabel,
df.InstTypeMaintainer: parseString,
df.InstTypeOnbuild: parseSubCommand,
df.InstTypeRun: parseMaybeJSON,
df.InstTypeShell: parseMaybeJSON,
df.InstTypeStopSignal: parseString,
df.InstTypeUser: parseString,
df.InstTypeVolume: parseMaybeJSONToList,
df.InstTypeWorkdir: parseString,
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/docker/dockerfile/ast/split_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func splitCommand(line string) (string, []string, string, error) {

// Make sure we get the same results irrespective of leading/trailing spaces
cmdline := tokenWhitespace.Split(strings.TrimSpace(line), 2)
cmd := strings.ToLower(cmdline[0])
cmd := strings.ToUpper(cmdline[0])

if len(cmdline) == 2 {
var err error
Expand Down
54 changes: 32 additions & 22 deletions pkg/docker/dockerfile/dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,47 @@ import (
v "github.com/mintoolkit/mint/pkg/version"
)

// note: dup (todo: refactor)
const (
//FROM
InstTypeFrom = "FROM"
//MAINTAINER:
instPrefixMaintainer = "MAINTAINER "
InstTypeMaintainer = "MAINTAINER"
InstPrefixMaintainer = "MAINTAINER "
//ENTRYPOINT:
instTypeEntrypoint = "ENTRYPOINT"
instPrefixEntrypoint = "ENTRYPOINT "
InstTypeEntrypoint = "ENTRYPOINT"
InstPrefixEntrypoint = "ENTRYPOINT "
//CMD:
instTypeCmd = "CMD"
instPrefixCmd = "CMD "
InstTypeCmd = "CMD"
InstPrefixCmd = "CMD "
//USER:
instTypeUser = "USER"
instPrefixUser = "USER "
InstTypeUser = "USER"
InstPrefixUser = "USER "
//EXPOSE:
instTypeExpose = "EXPOSE"
instPrefixExpose = "EXPOSE "
InstTypeExpose = "EXPOSE"
InstPrefixExpose = "EXPOSE "
//WORKDIR:
instTypeWorkdir = "WORKDIR"
instPrefixWorkdir = "WORKDIR "
InstTypeWorkdir = "WORKDIR"
InstPrefixWorkdir = "WORKDIR "
//HEALTHCHECK:
instTypeHealthcheck = "HEALTHCHECK"
instPrefixHealthcheck = "HEALTHCHECK "
InstTypeHealthcheck = "HEALTHCHECK"
InstPrefixHealthcheck = "HEALTHCHECK "
InstPrefixBasicEncHealthcheck = "HEALTHCHECK --"
//ONBUILD:
instTypeOnbuild = "ONBUILD"
InstTypeOnbuild = "ONBUILD"
//RUN:
instTypeRun = "RUN"
instPrefixRun = "RUN "
InstTypeRun = "RUN"
InstPrefixRun = "RUN "
//ADD:
instTypeAdd = "ADD"
InstTypeAdd = "ADD"
//COPY:
instTypeCopy = "COPY"
InstTypeCopy = "COPY"

InstTypeVolume = "VOLUME"
InstTypeEnv = "ENV"
InstTypeLabel = "LABEL"
InstTypeStopSignal = "STOPSIGNAL"
InstTypeShell = "SHELL"
InstTypeArg = "ARG" //shouldn't see it as a standalone instruction
)

// GenerateFromInfo builds and saves a Dockerfile file object
Expand Down Expand Up @@ -115,20 +125,20 @@ func GenerateFromInfo(
}

if workingDir != "" {
dfData.WriteString(instPrefixWorkdir)
dfData.WriteString(InstPrefixWorkdir)
dfData.WriteString(workingDir)
dfData.WriteByte('\n')
}

if user != "" {
dfData.WriteString(instPrefixUser)
dfData.WriteString(InstPrefixUser)
dfData.WriteString(user)
dfData.WriteByte('\n')
}

if len(exposedPorts) > 0 {
for portInfo := range exposedPorts {
dfData.WriteString(instPrefixExpose)
dfData.WriteString(InstPrefixExpose)
dfData.WriteString(string(portInfo))
dfData.WriteByte('\n')
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/docker/dockerfile/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parser

import (
"errors"
df "github.com/mintoolkit/mint/pkg/docker/dockerfile"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -71,7 +72,7 @@ func FromFile(fpath string) (*spec.Dockerfile, error) {
inst.Errors = append(inst.Errors, node.Errors...)
}

if inst.Name == instruction.Onbuild &&
if inst.Name == df.InstTypeOnbuild &&
node.Next != nil &&
len(node.Next.Children) > 0 {
inst.IsOnBuild = true
Expand All @@ -89,7 +90,7 @@ func FromFile(fpath string) (*spec.Dockerfile, error) {
inst.IsJSONForm = true
}

if inst.Name == instruction.From {
if inst.Name == df.InstTypeFrom {
currentStage = spec.NewBuildStage()
currentStage.FromInstruction = inst
currentStage.Index = len(dockerfile.Stages)
Expand Down Expand Up @@ -219,9 +220,9 @@ func FromFile(fpath string) (*spec.Dockerfile, error) {
inst.StageIndex = instStageIndex
currentStage.AllInstructions = append(currentStage.AllInstructions, inst)

if inst.Name == instruction.Onbuild {
if inst.Name == df.InstTypeOnbuild {
currentStage.OnBuildInstructions = append(currentStage.OnBuildInstructions, inst)
} else if inst.Name == instruction.Copy {
} else if inst.Name == df.InstTypeCopy {
for _, flag := range inst.Flags {
if strings.HasPrefix(flag, "--from=") {
fparts := strings.SplitN(flag, "=", 2)
Expand Down Expand Up @@ -252,7 +253,7 @@ func FromFile(fpath string) (*spec.Dockerfile, error) {

if inst.IsValid {
switch inst.Name {
case instruction.Arg:
case df.InstTypeArg:
for _, iarg := range inst.Args {
if iarg == "" {
continue
Expand All @@ -268,7 +269,7 @@ func FromFile(fpath string) (*spec.Dockerfile, error) {
//only one ARG is supposed to be defined, but we'll use all
//the 'ARG' value count lint check will detect the extra values
//the k=v ARG values are also not parsed (unlike ENV k=v values)
case instruction.Env:
case df.InstTypeEnv:
for i := 0; i < len(inst.Args) && (i+1) < len(inst.Args); i += 2 {
if len(inst.Args[i]) == 0 {
continue
Expand All @@ -280,7 +281,7 @@ func FromFile(fpath string) (*spec.Dockerfile, error) {
}
}
} else {
if inst.Name == instruction.Arg {
if inst.Name == df.InstTypeArg {
if inst.IsValid && len(inst.Args) > 0 {
dockerfile.ArgInstructions = append(dockerfile.ArgInstructions, inst)
parts := strings.Split(inst.Args[0], "=")
Expand Down
25 changes: 25 additions & 0 deletions pkg/docker/dockerfile/parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package parser

import (
"github.com/stretchr/testify/require"
"os"
"testing"
)

func TestFromFile(t *testing.T) {
tmpFile, err := os.CreateTemp(".", "Dockerfile")
require.NoError(t, err)
defer os.Remove(tmpFile.Name())

for _, s := range []string{"MAINTAINER", "Maintainer", "maintainer"} {
tmpFile.WriteString(s + " author@example.com")
Dockerfile, err := FromFile(tmpFile.Name())

require.NoError(t, err)
require.Equal(t, Dockerfile.AllInstructions[0].Name, "MAINTAINER")
for _, inst := range Dockerfile.AllInstructions {
require.Equal(t, inst.IsValid, true)
require.Equal(t, inst.Errors, []string(nil))
}
}
}
Loading
Loading