github.com/KusionStack/kpm@v0.8.4-0.20240326033734-dc72298a30e5/pkg/cmd/cmd_pkg.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 "path/filepath" 11 12 "github.com/urfave/cli/v2" 13 "kcl-lang.io/kpm/pkg/client" 14 pkg "kcl-lang.io/kpm/pkg/package" 15 "kcl-lang.io/kpm/pkg/reporter" 16 "kcl-lang.io/kpm/pkg/utils" 17 ) 18 19 // NewPkgCmd new a Command for `kpm pkg`. 20 func NewPkgCmd(kpmcli *client.KpmClient) *cli.Command { 21 return &cli.Command{ 22 Hidden: false, 23 Name: "pkg", 24 Usage: "package a kcl package into tar", 25 Flags: []cli.Flag{ 26 &cli.StringFlag{ 27 Name: "target", 28 Usage: "Packaged target path", 29 }, 30 // '--vendor' will trigger the vendor mode 31 // In the vendor mode, the package search path is the subdirectory 'vendor' in current package. 32 // In the non-vendor mode, the package search path is the $KCL_PKG_PATH. 33 &cli.BoolFlag{ 34 Name: FLAG_VENDOR, 35 Usage: "push in vendor mode", 36 }, 37 }, 38 Action: func(c *cli.Context) error { 39 tarPath := c.String("target") 40 41 if len(tarPath) == 0 { 42 return reporter.NewErrorEvent( 43 reporter.InvalidCmd, 44 fmt.Errorf("the directory where the tar is generated is required"), 45 ) 46 } 47 48 pwd, err := os.Getwd() 49 50 if err != nil { 51 return reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, failed to load working directory.") 52 } 53 54 kclPkg, err := pkg.LoadKclPkg(pwd) 55 56 if err != nil { 57 reporter.ExitWithReport("failed to load package in " + pwd + ".") 58 return err 59 } 60 61 // If the file path used to save the package tar file does not exist, create this file path. 62 if !utils.DirExists(tarPath) { 63 err := os.MkdirAll(tarPath, os.ModePerm) 64 if err != nil { 65 return reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, failed to create the target directory") 66 } 67 } 68 69 return kpmcli.Package(kclPkg, filepath.Join(tarPath, kclPkg.GetPkgTarName()), c.Bool(FLAG_VENDOR)) 70 }, 71 } 72 }