github.com/GoogleContainerTools/kaniko@v1.23.0/pkg/commands/cmd.go (about)

     1  /*
     2  Copyright 2018 Google LLC
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package commands
    18  
    19  import (
    20  	"strings"
    21  
    22  	"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
    23  	v1 "github.com/google/go-containerregistry/pkg/v1"
    24  
    25  	"github.com/moby/buildkit/frontend/dockerfile/instructions"
    26  )
    27  
    28  type CmdCommand struct {
    29  	BaseCommand
    30  	cmd *instructions.CmdCommand
    31  }
    32  
    33  // ExecuteCommand executes the CMD command
    34  // Argument handling is the same as RUN.
    35  func (c *CmdCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
    36  	var newCommand []string
    37  	if c.cmd.PrependShell {
    38  		// This is the default shell on Linux
    39  		var shell []string
    40  		if len(config.Shell) > 0 {
    41  			shell = config.Shell
    42  		} else {
    43  			shell = append(shell, "/bin/sh", "-c")
    44  		}
    45  
    46  		newCommand = append(shell, strings.Join(c.cmd.CmdLine, " "))
    47  	} else {
    48  		newCommand = c.cmd.CmdLine
    49  	}
    50  
    51  	config.Cmd = newCommand
    52  	// ArgsEscaped is only used in windows environments
    53  	config.ArgsEscaped = false
    54  	return nil
    55  }
    56  
    57  // String returns some information about the command for the image config history
    58  func (c *CmdCommand) String() string {
    59  	return c.cmd.String()
    60  }