kcl-lang.io/kpm@v0.8.7-0.20240520061008-9fc4c5efc8c7/pkg/cmd/cmd_metadata.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/kpm/pkg/client"
    13  	"kcl-lang.io/kpm/pkg/env"
    14  	"kcl-lang.io/kpm/pkg/reporter"
    15  )
    16  
    17  // NewMetadataCmd new a Command for `kpm metadata`.
    18  func NewMetadataCmd(kpmcli *client.KpmClient) *cli.Command {
    19  	return &cli.Command{
    20  		Hidden: false,
    21  		Name:   "metadata",
    22  		Usage:  "output the resolved dependencies of a package",
    23  		Flags: []cli.Flag{
    24  			// '--vendor' will trigger the vendor mode
    25  			// In the vendor mode, the package search path is the subdirectory 'vendor' in current package.
    26  			// In the non-vendor mode, the package search path is the $KCL_PKG_PATH.
    27  			&cli.BoolFlag{
    28  				Name:  FLAG_VENDOR,
    29  				Usage: "get metadata in vendor mode",
    30  			},
    31  			// '--update' will trigger the auto-update mode
    32  			// In the auto-update mode, `kpm metadata` will automatically check the local package, update and download the package.
    33  			// In the non-auto-update mode, `kpm metadata`` will only return the metadata of the existing packages.
    34  			&cli.BoolFlag{
    35  				Name:  FLAG_UPDATE,
    36  				Usage: "check the local package and update and download the local package.",
    37  			},
    38  		},
    39  		Action: func(c *cli.Context) error {
    40  			// acquire the lock of the package cache.
    41  			err := kpmcli.AcquirePackageCacheLock()
    42  			if err != nil {
    43  				return err
    44  			}
    45  
    46  			defer func() {
    47  				// release the lock of the package cache after the function returns.
    48  				releaseErr := kpmcli.ReleasePackageCacheLock()
    49  				if releaseErr != nil && err == nil {
    50  					err = releaseErr
    51  				}
    52  			}()
    53  
    54  			pwd, err := os.Getwd()
    55  			if err != nil {
    56  				return reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it")
    57  			}
    58  
    59  			kclPkg, err := kpmcli.LoadPkgFromPath(pwd)
    60  			if err != nil {
    61  				return err
    62  			}
    63  
    64  			globalPkgPath, err := env.GetAbsPkgPath()
    65  			if err != nil {
    66  				return err
    67  			}
    68  
    69  			kclPkg.SetVendorMode(c.Bool(FLAG_VENDOR))
    70  
    71  			err = kclPkg.ValidateKpmHome(globalPkgPath)
    72  			if err != (*reporter.KpmEvent)(nil) {
    73  				return err
    74  			}
    75  
    76  			autoUpdate := c.Bool(FLAG_UPDATE)
    77  
    78  			jsonStr, err := kpmcli.ResolveDepsMetadataInJsonStr(kclPkg, autoUpdate)
    79  			if err != nil {
    80  				return err
    81  			}
    82  
    83  			fmt.Println(jsonStr)
    84  
    85  			return nil
    86  		},
    87  	}
    88  }