github.com/evanlouie/fabrikate@v0.17.4/cmd/install.go (about)

     1  package cmd
     2  
     3  import (
     4  	"errors"
     5  	"os/exec"
     6  
     7  	"github.com/evanlouie/fabrikate/core"
     8  	"github.com/evanlouie/fabrikate/generators"
     9  	"github.com/evanlouie/fabrikate/logger"
    10  	"github.com/kyokomi/emoji"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  // Install implements the 'install' command.  It installs the component at the given path and all of
    15  // its subcomponents by iterating the component subtree.
    16  func Install(path string) (err error) {
    17  	// Make sure host system contains all utils needed by Fabrikate
    18  	requiredSystemTools := []string{"git", "helm", "sh", "curl"}
    19  	for _, tool := range requiredSystemTools {
    20  		path, err := exec.LookPath(tool)
    21  		if err != nil {
    22  			return err
    23  		}
    24  		logger.Info(emoji.Sprintf(":mag: Using %s: %s", tool, path))
    25  	}
    26  
    27  	logger.Info(emoji.Sprintf(":point_right: Initializing Helm"))
    28  	if output, err := exec.Command("helm", "init", "--client-only").CombinedOutput(); err != nil {
    29  		logger.Error(emoji.Sprintf(":no_entry_sign: %s: %s", err, output))
    30  		return err
    31  	}
    32  
    33  	rootInit := func(startingPath string, environments []string, c core.Component) (component core.Component, err error) {
    34  		return c.InstallRoot(startingPath, environments)
    35  	}
    36  
    37  	results := core.WalkComponentTree(path, []string{}, func(path string, component *core.Component) (err error) {
    38  		logger.Info(emoji.Sprintf(":point_right: Starting install for component: %s", component.Name))
    39  
    40  		var generator core.Generator
    41  
    42  		switch component.ComponentType {
    43  		case "helm":
    44  			generator = &generators.HelmGenerator{}
    45  		case "static":
    46  			generator = &generators.StaticGenerator{}
    47  		}
    48  
    49  		// Load access tokens and add them to the global token list. Do not overwrite if already present
    50  		if accessTokens, err := component.GetAccessTokens(); err != nil {
    51  			return err
    52  		} else if len(accessTokens) > 0 {
    53  			for repo, token := range accessTokens {
    54  				if _, exists := core.GitAccessTokens.Get(repo); !exists {
    55  					core.GitAccessTokens.Set(repo, token)
    56  				}
    57  			}
    58  		}
    59  
    60  		if err := component.Install(path, generator); err != nil {
    61  			return err
    62  		}
    63  
    64  		logger.Info(emoji.Sprintf(":point_left: Finished install for component: %s", component.Name))
    65  
    66  		return err
    67  	}, rootInit)
    68  
    69  	components, err := core.SynchronizeWalkResult(results)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	for _, component := range components {
    75  		logger.Info(emoji.Sprintf(":white_check_mark: Installed successfully: %s", component.Name))
    76  	}
    77  	logger.Info(emoji.Sprintf(":raised_hands: Finished install"))
    78  
    79  	return err
    80  }
    81  
    82  var installCmd = &cobra.Command{
    83  	Use:   "install",
    84  	Short: "Installs all of the remote components specified in the current deployment tree locally",
    85  	Long: `Installs all of the remote components specified in the current deployment tree locally, iterating the 
    86  component subtree from the current directory to do so.  Required to be executed before generate (if needed), such
    87  that Fabrikate has all of the dependencies locally to use to generate the resource manifests.`,
    88  	RunE: func(cmd *cobra.Command, args []string) (err error) {
    89  		PrintVersion()
    90  
    91  		path := "./"
    92  
    93  		if len(args) == 1 {
    94  			path = args[0]
    95  		}
    96  
    97  		if len(args) > 1 {
    98  			return errors.New("install takes zero or one arguments: the path to the root of the definition tree (defaults to current directory)")
    99  		}
   100  
   101  		return Install(path)
   102  	},
   103  }
   104  
   105  func init() {
   106  	rootCmd.AddCommand(installCmd)
   107  }