github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/initializer/transparent.go (about)

     1  /*
     2  Copyright 2020 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 initializer
    18  
    19  import (
    20  	"context"
    21  	"io"
    22  
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
    24  	initConfig "github.com/GoogleContainerTools/skaffold/pkg/skaffold/initializer/config"
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/initializer/prompt"
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    27  )
    28  
    29  var (
    30  	confirmInitOptions = prompt.ConfirmInitOptions
    31  )
    32  
    33  // Transparent executes the `skaffold init` flow, but always enables the --force flag.
    34  // It will also always prompt the user to confirm at the end of the flow.
    35  func Transparent(ctx context.Context, out io.Writer, c initConfig.Config) (*latest.SkaffoldConfig, error) {
    36  	// we set force to true because we want to have this happen invisibly to the user if possible
    37  	c.Force = true
    38  
    39  	if c.ComposeFile != "" {
    40  		if err := runKompose(ctx, c.ComposeFile); err != nil {
    41  			return nil, err
    42  		}
    43  	}
    44  
    45  	a, err := AnalyzeProject(c)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	newConfig, newManifests, err := Initialize(out, c, a)
    51  	// If the --analyze flag is used, we return early with the result of PrintAnalysis()
    52  	// TODO(marlongamez): Figure out a cleaner way to do this. Might have to change return values to include the different Initializers.
    53  	if err != nil || c.Analyze {
    54  		return nil, err
    55  	}
    56  
    57  	// Prompt the user with information about what will happen if they continue with this config.
    58  	if !c.Opts.AssumeYes {
    59  		if done, err := confirmInitOptions(out, newConfig); done {
    60  			return nil, err
    61  		}
    62  	}
    63  
    64  	if err := WriteData(out, c, newConfig, newManifests); err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	return newConfig, nil
    69  }
    70  
    71  // Returns true if transparent init should run on the given opts.Command, false otherwise
    72  func ValidCmd(opts config.SkaffoldOptions) bool {
    73  	valid := map[string]struct{}{
    74  		"debug": {},
    75  		"dev":   {},
    76  		"run":   {},
    77  	}
    78  	_, ok := valid[opts.Command]
    79  
    80  	return ok
    81  }