github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/custom/dependencies.go (about)

     1  /*
     2  Copyright 2019 The Skaffold Authors
     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 custom
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"fmt"
    23  	"os/exec"
    24  	"strings"
    25  
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/list"
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
    28  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    29  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    30  )
    31  
    32  // GetDependencies returns dependencies listed for a custom artifact
    33  func GetDependencies(ctx context.Context, workspace string, artifactName string, a *latest.CustomArtifact, cfg docker.Config) ([]string, error) {
    34  	switch {
    35  	case a.Dependencies.Dockerfile != nil:
    36  		return docker.GetDependencies(ctx, getDockerBuildConfig(workspace, artifactName, a), cfg)
    37  
    38  	case a.Dependencies.Command != "":
    39  		split := strings.Split(a.Dependencies.Command, " ")
    40  		cmd := exec.CommandContext(ctx, split[0], split[1:]...)
    41  		cmd.Dir = workspace
    42  		output, err := util.RunCmdOut(ctx, cmd)
    43  		if err != nil {
    44  			return nil, fmt.Errorf("getting dependencies from command: %q: %w", a.Dependencies.Command, err)
    45  		}
    46  		var deps []string
    47  		if err := json.Unmarshal(output, &deps); err != nil {
    48  			return nil, fmt.Errorf("unmarshalling dependency output into string array: %w", err)
    49  		}
    50  		return deps, nil
    51  
    52  	default:
    53  		return list.Files(workspace, a.Dependencies.Paths, a.Dependencies.Ignore)
    54  	}
    55  }
    56  
    57  func getDockerBuildConfig(ws string, artifact string, a *latest.CustomArtifact) docker.BuildConfig {
    58  	dockerfile := a.Dependencies.Dockerfile
    59  	return docker.NewBuildConfig(ws, artifact, dockerfile.Path, dockerfile.BuildArgs)
    60  }