github.com/facebookincubator/ttpforge@v1.0.13-0.20240405153150-5ae801628835/cmd/run.go (about)

     1  /*
     2  Copyright © 2023-present, Meta Platforms, Inc. and affiliates
     3  Permission is hereby granted, free of charge, to any person obtaining a copy
     4  of this software and associated documentation files (the "Software"), to deal
     5  in the Software without restriction, including without limitation the rights
     6  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  copies of the Software, and to permit persons to whom the Software is
     8  furnished to do so, subject to the following conditions:
     9  The above copyright notice and this permission notice shall be included in
    10  all copies or substantial portions of the Software.
    11  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    12  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    13  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    14  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    15  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    16  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    17  THE SOFTWARE.
    18  */
    19  
    20  package cmd
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"github.com/facebookincubator/ttpforge/pkg/blocks"
    26  	"github.com/facebookincubator/ttpforge/pkg/logging"
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  func buildRunCommand(cfg *Config) *cobra.Command {
    31  	var argsList []string
    32  	var ttpCfg blocks.TTPExecutionConfig
    33  	runCmd := &cobra.Command{
    34  		Use:   "run [repo_name//path/to/ttp]",
    35  		Short: "Run the TTP found in the specified YAML file.",
    36  		Args:  cobra.ExactArgs(1),
    37  		RunE: func(cmd *cobra.Command, args []string) error {
    38  			// don't want confusing usage display for errors past this point
    39  			cmd.SilenceUsage = true
    40  
    41  			// capture output for tests if needed
    42  			if cfg.testCfg != nil {
    43  				ttpCfg.Stdout, ttpCfg.Stderr = cfg.testCfg.Stdout, cfg.testCfg.Stderr
    44  			}
    45  
    46  			// find the TTP file
    47  			ttpRef := args[0]
    48  			foundRepo, ttpAbsPath, err := cfg.repoCollection.ResolveTTPRef(ttpRef)
    49  			if err != nil {
    50  				return fmt.Errorf("failed to resolve TTP reference %v: %v", ttpRef, err)
    51  			}
    52  
    53  			// load TTP and process argument values
    54  			// based on the TTPs argument value specifications
    55  			ttpCfg.Repo = foundRepo
    56  
    57  			ttp, execCtx, err := blocks.LoadTTP(ttpAbsPath, foundRepo.GetFs(), &ttpCfg, argsList)
    58  			if err != nil {
    59  				return fmt.Errorf("could not load TTP at %v:\n\t%v", ttpAbsPath, err)
    60  			}
    61  
    62  			if ttpCfg.DryRun {
    63  				logging.L().Info("Dry-Run Requested - Returning Early")
    64  				return nil
    65  			}
    66  
    67  			runErr := ttp.Execute(*execCtx)
    68  			// Run clean up always
    69  			cleanupErr := ttp.RunCleanup(*execCtx)
    70  
    71  			if cleanupErr != nil {
    72  				logging.L().Warnf("Failed to run cleanup: %v", cleanupErr)
    73  			}
    74  
    75  			if runErr != nil {
    76  				return fmt.Errorf("failed to run TTP at %v: %v", ttpAbsPath, err)
    77  			}
    78  			return nil
    79  		},
    80  	}
    81  	runCmd.PersistentFlags().BoolVar(&ttpCfg.DryRun, "dry-run", false, "Parse arguments and validate TTP Contents, but do not actually run the TTP")
    82  	runCmd.PersistentFlags().BoolVar(&ttpCfg.NoCleanup, "no-cleanup", false, "Disable cleanup (useful for debugging and daisy-chaining TTPs)")
    83  	runCmd.PersistentFlags().UintVar(&ttpCfg.CleanupDelaySeconds, "cleanup-delay-seconds", 0, "Wait this long after TTP execution before starting cleanup")
    84  	runCmd.Flags().StringArrayVarP(&argsList, "arg", "a", []string{}, "variable input mapping for args to be used in place of inputs defined in each ttp file")
    85  
    86  	return runCmd
    87  }