github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/build/buildpacks/init.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 buildpacks
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    26  )
    27  
    28  // For testing
    29  var (
    30  	Validate = validate
    31  )
    32  
    33  // Name is the name of the Buildpack builder
    34  var Name = "Buildpacks"
    35  
    36  // ArtifactConfig holds information about a Buildpack project
    37  type ArtifactConfig struct {
    38  	File    string `json:"path,omitempty"`
    39  	Builder string `json:"builder,omitempty"`
    40  }
    41  
    42  // Name returns the name of the builder
    43  func (c ArtifactConfig) Name() string {
    44  	return Name
    45  }
    46  
    47  // Describe returns the initBuilder's string representation, used when prompting the user to choose a builder.
    48  func (c ArtifactConfig) Describe() string {
    49  	return fmt.Sprintf("%s (%s)", c.Name(), c.File)
    50  }
    51  
    52  // ArtifactType returns the type of the artifact to be built.
    53  func (c ArtifactConfig) ArtifactType(_ string) latest.ArtifactType {
    54  	return latest.ArtifactType{
    55  		BuildpackArtifact: &latest.BuildpackArtifact{
    56  			Builder: c.Builder,
    57  		},
    58  	}
    59  }
    60  
    61  // ConfiguredImage returns the target image configured by the builder, or empty string if no image is configured
    62  func (c ArtifactConfig) ConfiguredImage() string {
    63  	// Target image is not configured in buildpacks
    64  	return ""
    65  }
    66  
    67  // Path returns the path to the build definition
    68  func (c ArtifactConfig) Path() string {
    69  	return c.File
    70  }
    71  
    72  // validate checks if a file is a valid Buildpack configuration.
    73  func validate(path string) bool {
    74  	switch filepath.Base(path) {
    75  	// Buildpacks project descriptor.
    76  	case "project.toml":
    77  		return true
    78  
    79  	// NodeJS.
    80  	case "package.json":
    81  		return true
    82  
    83  	// Go.
    84  	case "go.mod":
    85  		return true
    86  
    87  	// Java.
    88  	case "pom.xml", "build.gradle", "build.gradle.kts":
    89  		return true
    90  
    91  	// Python.
    92  	// TODO(dgageot): When the Procfile is missing, we might want to inform the user
    93  	// that this still might be a valid python project.
    94  	case "requirements.txt":
    95  		if _, err := os.Stat(filepath.Join(filepath.Dir(path), "Procfile")); err == nil {
    96  			return true
    97  		}
    98  	}
    99  
   100  	// .NET project
   101  	return strings.HasSuffix(filepath.Base(path), ".csproj")
   102  }