Skip to content

Commit

Permalink
fix: LABEL instruction compiled by Docker cannot be parsed
Browse files Browse the repository at this point in the history
Fixes #16

Signed-off-by: Jeff MAURY <jmaury@redhat.com>
  • Loading branch information
jeffmaury committed Nov 16, 2023
1 parent 3c27c43 commit e6fb2fd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
21 changes: 21 additions & 0 deletions pkg/command/analyzer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2022 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc.
******************************************************************************/
package command

import "testing"

func TestCheckNginx(t *testing.T) {
for _, tag := range []string{"1.25.0", "1.25.1", "1.25.2", "1.25.3"} {
t.Run(tag, func(t *testing.T) {
AnalyzeImage("docker.io/nginx:" + tag)
})
}
}
35 changes: 29 additions & 6 deletions pkg/decompiler/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"github.com/moby/buildkit/frontend/dockerfile/parser"
"github.com/redhat-developer/docker-openshift-analyzer/pkg/utils"
"regexp"
"strings"
)

Expand All @@ -26,21 +27,43 @@ var CONTAINERFILE_INSTRUCTIONS = []string{
utils.SHELL_INSTRUCTION,
}

var LABEL_PATTERN = regexp.MustCompile("^LABEL\\s+(.*)=(.*)$")

func Line2Node(line string, root *parser.Node) error {
result, err := parser.Parse(strings.NewReader(line))
if err != nil {
return err
if strings.HasPrefix(line, utils.LABEL_INSTRUCTION) {
return parseLabel(line, root)
} else {
result, err := parser.Parse(strings.NewReader(line))
if err != nil {
return err
}
for _, node := range result.AST.Children {
root.AddChild(node, -1, -1)
}
}
return nil
}

func parseLabel(line string, root *parser.Node) error {
elements := LABEL_PATTERN.FindStringSubmatch(line)
parent := &parser.Node{
Value: "LABEL",
}
for _, node := range result.AST.Children {
root.AddChild(node, -1, -1)
node := parent
for _, element := range elements[1:] {
node.Next = &parser.Node{
Value: element,
}
node = node.Next
}
root.AddChild(parent, 0, 0)
return nil
}

func ExtractCmd(str string) string {
index := strings.Index(str, utils.NOP)
if index > 0 {
return str[index+len(utils.NOP):]
return strings.TrimSpace(str[index+len(utils.NOP):])
}
index = strings.Index(str, utils.RUN_PREFIX)
if index >= 0 {
Expand Down

0 comments on commit e6fb2fd

Please sign in to comment.