Skip to content

Commit

Permalink
fix: LABEL instruction compiled by Docker cannot be parsed (#22)
Browse files Browse the repository at this point in the history
* fix: LABEL instruction compiled by Docker cannot be parsed

Fixes #16

Signed-off-by: Jeff MAURY <[email protected]>

* Update pkg/decompiler/utils/utils.go

Co-authored-by: Philippe Martin <[email protected]>

---------

Signed-off-by: Jeff MAURY <[email protected]>
Co-authored-by: Philippe Martin <[email protected]>
  • Loading branch information
jeffmaury and feloy authored Nov 17, 2023
1 parent d732be8 commit 6448cfa
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
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)
})
}
}
24 changes: 23 additions & 1 deletion 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,7 +27,12 @@ var CONTAINERFILE_INSTRUCTIONS = []string{
utils.SHELL_INSTRUCTION,
}

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

func Line2Node(line string, root *parser.Node) error {
if strings.HasPrefix(line, utils.LABEL_INSTRUCTION) {
return parseLabel(line, root)
}
result, err := parser.Parse(strings.NewReader(line))
if err != nil {
return err
Expand All @@ -37,10 +43,26 @@ func Line2Node(line string, root *parser.Node) error {
return nil
}

func parseLabel(line string, root *parser.Node) error {
elements := LABEL_PATTERN.FindStringSubmatch(line)
parent := &parser.Node{
Value: "LABEL",
}
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 6448cfa

Please sign in to comment.