github.com/keikoproj/manny@v0.0.0-20210726112440-8571e4c99ced/cmd/build.go (about)

     1  package cmd
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"path/filepath"
     7  
     8  	"github.com/go-git/go-git/v5"
     9  	"github.com/spf13/cobra"
    10  	"go.uber.org/zap"
    11  
    12  	"github.com/keikoproj/manny/configurator"
    13  	"github.com/keikoproj/manny/utils"
    14  )
    15  
    16  var (
    17  	buildCmd = &cobra.Command{
    18  		Use:     "build path/to/stacks",
    19  		Short:   "Builds a manny deployment",
    20  		Long:    "Builds a manny deployment",
    21  		Example: "manny build usw2",
    22  		RunE:    buildConfig,
    23  	}
    24  )
    25  
    26  func buildConfig(cmd *cobra.Command, args []string) error {
    27  	if len(args) == 0 {
    28  		return errors.New(ErrMissingArg)
    29  	}
    30  
    31  	path := args[0]
    32  
    33  	Logger.Debug("Git repository path", zap.String("repo-path", filepath.Join(path, git.GitDirName)))
    34  
    35  	gitURL, err := utils.GitRepoRemote(path)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	// create a new configurator with defaults
    41  	config := configurator.New(configurator.Config{
    42  		Path:   path,
    43  		Logger: Logger,
    44  		GitURL: gitURL,
    45  	})
    46  
    47  	deployments, err := config.CreateDeployments()
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	Logger.Debug("Deployments created", zap.Any("CloudResourceDeployments", len(deployments)))
    53  
    54  	if validate {
    55  		if err := deployments.Validate(); err != nil {
    56  			Logger.Error("Validation failed", zap.Error(err))
    57  			return err
    58  		}
    59  	}
    60  
    61  	// early return for dry run
    62  	if dryRun {
    63  		return nil
    64  	}
    65  
    66  	// render the manifest in a given format
    67  	bytes, err := deployments.Render(format)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	// output to location
    73  	if output != "stdout" {
    74  		// File location validation
    75  		ok, err := utils.ValidateAndWrite(output, bytes)
    76  		if !ok {
    77  			return err
    78  		}
    79  
    80  		// early return
    81  		return nil
    82  	}
    83  
    84  	// write to stdout
    85  	fmt.Printf("%s", bytes)
    86  
    87  	return nil
    88  }