github.com/KusionStack/kpm@v0.8.4-0.20240326033734-dc72298a30e5/pkg/cmd/cmd_run.go (about)

     1  // Copyright 2023 The KCL Authors. All rights reserved.
     2  // Deprecated: The entire contents of this file will be deprecated. 
     3  // Please use the kcl cli - https://github.com/kcl-lang/cli.
     4  
     5  package cmd
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  
    11  	"github.com/urfave/cli/v2"
    12  	"kcl-lang.io/kcl-go/pkg/kcl"
    13  	"kcl-lang.io/kpm/pkg/api"
    14  	"kcl-lang.io/kpm/pkg/client"
    15  	"kcl-lang.io/kpm/pkg/git"
    16  	"kcl-lang.io/kpm/pkg/opt"
    17  	"kcl-lang.io/kpm/pkg/reporter"
    18  	"kcl-lang.io/kpm/pkg/runner"
    19  )
    20  
    21  // NewRunCmd new a Command for `kpm run`.
    22  func NewRunCmd(kpmcli *client.KpmClient) *cli.Command {
    23  	return &cli.Command{
    24  		Hidden: false,
    25  		Name:   "run",
    26  		Usage:  "compile kcl package.",
    27  		Flags: []cli.Flag{
    28  			// The entry kcl file.
    29  			&cli.StringSliceFlag{
    30  				Name:  FLAG_INPUT,
    31  				Usage: "a kcl file as the compile entry file",
    32  			},
    33  			&cli.StringFlag{
    34  				Name:  FLAG_TAG,
    35  				Usage: "the tag for oci artifact",
    36  			},
    37  			// '--vendor' will trigger the vendor mode
    38  			// In the vendor mode, the package search path is the subdirectory 'vendor' in current package.
    39  			// In the non-vendor mode, the package search path is the $KCL_PKG_PATH.
    40  			&cli.BoolFlag{
    41  				Name:  FLAG_VENDOR,
    42  				Usage: "run in vendor mode",
    43  			},
    44  			// --no_sum_check
    45  			&cli.BoolFlag{
    46  				Name:  FLAG_NO_SUM_CHECK,
    47  				Usage: "do not check the checksum of the package and update kcl.mod.lock",
    48  			},
    49  
    50  			// KCL arg: --setting, -Y
    51  			&cli.StringSliceFlag{
    52  				Name:    FLAG_SETTING,
    53  				Aliases: []string{"Y"},
    54  				Usage:   "specify the input setting file",
    55  			},
    56  
    57  			// KCL arg: --argument, -D
    58  			&cli.StringSliceFlag{
    59  				Name:    FLAG_ARGUMENT,
    60  				Aliases: []string{"D"},
    61  				Usage:   "specify the top-level argument",
    62  			},
    63  
    64  			// KCL arg: --overrides, -O
    65  			&cli.StringSliceFlag{
    66  				Name:    FLAG_OVERRIDES,
    67  				Aliases: []string{"O"},
    68  				Usage:   "specify the configuration override path and value",
    69  			},
    70  
    71  			// KCL arg: --disable_none, -n
    72  			&cli.BoolFlag{
    73  				Name:    FLAG_DISABLE_NONE,
    74  				Aliases: []string{"n"},
    75  				Usage:   "disable dumping None values",
    76  			},
    77  
    78  			// KCL arg: --sort_keys -k
    79  			&cli.BoolFlag{
    80  				Name:    FLAG_SORT_KEYS,
    81  				Aliases: []string{"k"},
    82  				Usage:   "sort result keys",
    83  			},
    84  		},
    85  		Action: func(c *cli.Context) error {
    86  			return KpmRun(c, kpmcli)
    87  		},
    88  	}
    89  }
    90  
    91  func KpmRun(c *cli.Context, kpmcli *client.KpmClient) error {
    92  	// acquire the lock of the package cache.
    93  	err := kpmcli.AcquirePackageCacheLock()
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	kpmcli.SetNoSumCheck(c.Bool(FLAG_NO_SUM_CHECK))
    99  
   100  	defer func() {
   101  		// release the lock of the package cache after the function returns.
   102  		releaseErr := kpmcli.ReleasePackageCacheLock()
   103  		if releaseErr != nil && err == nil {
   104  			err = releaseErr
   105  		}
   106  	}()
   107  
   108  	kclOpts := CompileOptionFromCli(c)
   109  	runEntry, errEvent := runner.FindRunEntryFrom(c.Args().Slice())
   110  	if errEvent != nil {
   111  		return errEvent
   112  	}
   113  
   114  	// 'kpm run' compile the current package under '$pwd'.
   115  	if runEntry.IsEmpty() {
   116  		pwd, err := os.Getwd()
   117  		kclOpts.SetPkgPath(pwd)
   118  
   119  		if err != nil {
   120  			return reporter.NewErrorEvent(
   121  				reporter.Bug, err, "internal bugs, please contact us to fix it.",
   122  			)
   123  		}
   124  		compileResult, err := kpmcli.CompileWithOpts(kclOpts)
   125  		if err != nil {
   126  			return err
   127  		}
   128  		fmt.Println(compileResult.GetRawYamlResult())
   129  	} else {
   130  		var compileResult *kcl.KCLResultList
   131  		var err error
   132  		// 'kpm run' compile the package from the local file system.
   133  		if runEntry.IsLocalFile() || runEntry.IsLocalFileWithKclMod() {
   134  			kclOpts.SetPkgPath(runEntry.PackageSource())
   135  			kclOpts.ExtendEntries(runEntry.EntryFiles())
   136  			if runEntry.IsLocalFile() {
   137  				// If there is only kcl file without kcl package,
   138  				compileResult, err = api.RunWithOpt(kclOpts)
   139  			} else {
   140  				// Else compile the kcl pacakge.
   141  				compileResult, err = kpmcli.CompileWithOpts(kclOpts)
   142  			}
   143  		} else if runEntry.IsTar() {
   144  			// 'kpm run' compile the package from the kcl package tar.
   145  			compileResult, err = kpmcli.CompileTarPkg(runEntry.PackageSource(), kclOpts)
   146  		} else if runEntry.IsGit() {
   147  			gitOpts := git.NewCloneOptions(runEntry.PackageSource(), "", c.String(FLAG_TAG), "", "", nil)
   148  			// 'kpm run' compile the package from the git url
   149  			compileResult, err = kpmcli.CompileGitPkg(gitOpts, kclOpts)
   150  		} else {
   151  			// 'kpm run' compile the package from the OCI reference or url.
   152  			compileResult, err = kpmcli.CompileOciPkg(runEntry.PackageSource(), c.String(FLAG_TAG), kclOpts)
   153  		}
   154  
   155  		if err != nil {
   156  			return err
   157  		}
   158  		fmt.Println(compileResult.GetRawYamlResult())
   159  	}
   160  	return nil
   161  }
   162  
   163  // CompileOptionFromCli will parse the kcl options from the cli context.
   164  func CompileOptionFromCli(c *cli.Context) *opt.CompileOptions {
   165  	opts := opt.DefaultCompileOptions()
   166  
   167  	// --input
   168  	opts.ExtendEntries(c.StringSlice(FLAG_INPUT))
   169  
   170  	// --vendor
   171  	opts.SetVendor(c.Bool(FLAG_VENDOR))
   172  
   173  	// --setting, -Y
   174  	settingsOpt := c.StringSlice(FLAG_SETTING)
   175  	if len(settingsOpt) != 0 {
   176  		for _, sPath := range settingsOpt {
   177  			opts.Merge(kcl.WithSettings(sPath))
   178  		}
   179  		opts.SetHasSettingsYaml(true)
   180  	}
   181  
   182  	// --argument, -D
   183  	opts.Merge(kcl.WithOptions(c.StringSlice(FLAG_ARGUMENT)...))
   184  
   185  	// --overrides, -O
   186  	opts.Merge(kcl.WithOverrides(c.StringSlice(FLAG_OVERRIDES)...))
   187  
   188  	// --disable_none, -n
   189  	opts.Merge(kcl.WithDisableNone(c.Bool(FLAG_DISABLE_NONE)))
   190  
   191  	// --sort_keys, -k
   192  	opts.Merge(kcl.WithSortKeys(c.Bool(FLAG_SORT_KEYS)))
   193  
   194  	return opts
   195  }