github.com/KusionStack/kpm@v0.8.4-0.20240326033734-dc72298a30e5/pkg/cmd/cmd_update.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  	"os"
     9  
    10  	"github.com/urfave/cli/v2"
    11  	"kcl-lang.io/kpm/pkg/client"
    12  	"kcl-lang.io/kpm/pkg/env"
    13  	"kcl-lang.io/kpm/pkg/reporter"
    14  )
    15  
    16  // NewUpdateCmd new a Command for `kpm update`.
    17  func NewUpdateCmd(kpmcli *client.KpmClient) *cli.Command {
    18  	return &cli.Command{
    19  		Hidden: false,
    20  		Name:   "update",
    21  		Usage:  "Update dependencies listed in kcl.mod.lock based on kcl.mod",
    22  		Flags: []cli.Flag{
    23  			&cli.BoolFlag{
    24  				Name:  FLAG_NO_SUM_CHECK,
    25  				Usage: "do not check the checksum of the package and update kcl.mod.lock",
    26  			},
    27  		},
    28  		Action: func(c *cli.Context) error {
    29  			return KpmUpdate(c, kpmcli)
    30  		},
    31  	}
    32  }
    33  
    34  func KpmUpdate(c *cli.Context, kpmcli *client.KpmClient) error {
    35  	kpmcli.SetNoSumCheck(c.Bool(FLAG_NO_SUM_CHECK))
    36  
    37  	// acquire the lock of the package cache.
    38  	err := kpmcli.AcquirePackageCacheLock()
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	defer func() {
    44  		// release the lock of the package cache after the function returns.
    45  		releaseErr := kpmcli.ReleasePackageCacheLock()
    46  		if releaseErr != nil && err == nil {
    47  			err = releaseErr
    48  		}
    49  	}()
    50  
    51  	input_paths := c.Args().Slice()
    52  
    53  	pkg_paths := []string{}
    54  	if len(input_paths) == 0 {
    55  		pwd, err := os.Getwd()
    56  		if err != nil {
    57  			return reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it")
    58  		}
    59  		pkg_paths = append(pkg_paths, pwd)
    60  	} else {
    61  		pkg_paths = input_paths
    62  	}
    63  
    64  	for _, pkg_path := range pkg_paths {
    65  		kclPkg, err := kpmcli.LoadPkgFromPath(pkg_path)
    66  		if err != nil {
    67  			return err
    68  		}
    69  
    70  		globalPkgPath, err := env.GetAbsPkgPath()
    71  		if err != nil {
    72  			return err
    73  		}
    74  
    75  		err = kclPkg.ValidateKpmHome(globalPkgPath)
    76  		if err != (*reporter.KpmEvent)(nil) {
    77  			return err
    78  		}
    79  
    80  		err = kpmcli.UpdateDeps(kclPkg)
    81  		if err != nil {
    82  			return err
    83  		}
    84  	}
    85  	return nil
    86  }