Skip to content

Commit

Permalink
Merge pull request #195 from zong-zhe/add-kpm-quiet
Browse files Browse the repository at this point in the history
feat: add '--quiet' to control the kpm log message
  • Loading branch information
Peefy authored Oct 7, 2023
2 parents 26ede83 + 0348557 commit 900a16d
Show file tree
Hide file tree
Showing 38 changed files with 430 additions and 128 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ jobs:
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.50.1
timeout: 5m
12 changes: 12 additions & 0 deletions kpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ func main() {
cmd.NewPushCmd(kpmcli),
cmd.NewPullCmd(kpmcli),
}
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: cmd.FLAG_QUIET,
Usage: "push in vendor mode",
},
}
app.Before = func(c *cli.Context) error {
if c.Bool(cmd.FLAG_QUIET) {
kpmcli.SetLogWriter(nil)
}
return nil
}
err = app.Run(os.Args)
if err != nil {
reporter.Fatal(err)
Expand Down
1 change: 0 additions & 1 deletion main.k

This file was deleted.

133 changes: 79 additions & 54 deletions pkg/api/kpm_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,62 +105,11 @@ func getAbsInputPath(pkgPath string, inputPath string) (string, error) {

// RunPkgWithOpt will compile the kcl package with the compile options.
func RunPkgWithOpt(opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
pkgPath, err := filepath.Abs(opts.PkgPath())
if err != nil {
return nil, errors.InternalBug
}

kclPkg, err := pkg.LoadKclPkg(pkgPath)
if err != nil {
return nil, fmt.Errorf("kpm: failed to load package, please check the package path '%s' is valid", pkgPath)
}

kclPkg.SetVendorMode(opts.IsVendor())

globalPkgPath, err := env.GetAbsPkgPath()
if err != nil {
return nil, err
}

err = kclPkg.ValidateKpmHome(globalPkgPath)
if err != (*reporter.KpmEvent)(nil) {
return nil, err
}

if len(opts.Entries()) > 0 {
// add entry from '--input'
for _, entry := range opts.Entries() {
if filepath.IsAbs(entry) {
opts.Merge(kcl.WithKFilenames(entry))
} else {
opts.Merge(kcl.WithKFilenames(filepath.Join(opts.PkgPath(), entry)))
}
}
// add entry from 'kcl.mod'
} else if len(kclPkg.GetEntryKclFilesFromModFile()) > 0 {
opts.Merge(*kclPkg.GetKclOpts())
} else if !opts.HasSettingsYaml() {
// no entry
opts.Merge(kcl.WithKFilenames(opts.PkgPath()))
}
opts.Merge(kcl.WithWorkDir(opts.PkgPath()))

// Calculate the absolute path of entry file described by '--input'.
compiler := runner.NewCompilerWithOpts(opts)

kpmcli, err := client.NewKpmClient()
if err != nil {
return nil, err
}

// Call the kcl compiler.
compileResult, err := kpmcli.Compile(kclPkg, compiler)

if err != nil {
return nil, reporter.NewErrorEvent(reporter.CompileFailed, err, "failed to compile the kcl package")
}

return compileResult, nil
return run(kpmcli, opts)
}

// RunCurrentPkg will compile the current kcl package.
Expand Down Expand Up @@ -191,12 +140,16 @@ func RunTarPkg(tarPath string, opts *opt.CompileOptions) (*kcl.KCLResultList, er
}

opts.SetPkgPath(destDir)
kpmcli, err := client.NewKpmClient()
if err != nil {
return nil, err
}
// The directory after extracting the tar package is taken as the root directory of the package,
// and kclvm is called to compile the kcl program under the 'destDir'.
// e.g.
// if the tar path is 'xxx/xxx/xxx/test.tar',
// the 'xxx/xxx/xxx/test' will be taken as the root path of the kcl package to compile.
return RunPkgWithOpt(opts)
return run(kpmcli, opts)
}

// RunOciPkg will compile the kcl package from an OCI reference.
Expand Down Expand Up @@ -235,5 +188,77 @@ func RunOciPkg(ociRef, version string, opts *opt.CompileOptions) (*kcl.KCLResult
return nil, errors.FailedPull
}

return RunTarPkg(matches[0], opts)
// 4. Untar the tar file.
absTarPath, err := utils.AbsTarPath(matches[0])
if err != nil {
return nil, err
}
// Extract the tar package to a directory with the same name.
// e.g.
// 'xxx/xxx/xxx/test.tar' will be extracted to the directory 'xxx/xxx/xxx/test'.
destDir := strings.TrimSuffix(absTarPath, filepath.Ext(absTarPath))
err = utils.UnTarDir(absTarPath, destDir)
if err != nil {
return nil, err
}

opts.SetPkgPath(destDir)
return run(kpmcli, opts)
}

// 'run' will compile the kcl package from the compile options by kpm client.
func run(kpmcli *client.KpmClient, opts *opt.CompileOptions) (*kcl.KCLResultList, error) {
pkgPath, err := filepath.Abs(opts.PkgPath())
if err != nil {
return nil, errors.InternalBug
}

kclPkg, err := pkg.LoadKclPkg(pkgPath)
if err != nil {
return nil, fmt.Errorf("kpm: failed to load package, please check the package path '%s' is valid", pkgPath)
}

kclPkg.SetVendorMode(opts.IsVendor())

globalPkgPath, err := env.GetAbsPkgPath()
if err != nil {
return nil, err
}

err = kclPkg.ValidateKpmHome(globalPkgPath)
if err != (*reporter.KpmEvent)(nil) {
return nil, err
}

if len(opts.Entries()) > 0 {
// add entry from '--input'
for _, entry := range opts.Entries() {
if filepath.IsAbs(entry) {
opts.Merge(kcl.WithKFilenames(entry))
} else {
opts.Merge(kcl.WithKFilenames(filepath.Join(opts.PkgPath(), entry)))
}
}
// add entry from 'kcl.mod'
} else if len(kclPkg.GetEntryKclFilesFromModFile()) > 0 {
opts.Merge(*kclPkg.GetKclOpts())
} else if !opts.HasSettingsYaml() {
// no entry
opts.Merge(kcl.WithKFilenames(opts.PkgPath()))
}
opts.Merge(kcl.WithWorkDir(opts.PkgPath()))

// Calculate the absolute path of entry file described by '--input'.
compiler := runner.NewCompilerWithOpts(opts)

kpmcli.SetLogWriter(opts.LogWriter())

// Call the kcl compiler.
compileResult, err := kpmcli.Compile(kclPkg, compiler)

if err != nil {
return nil, reporter.NewErrorEvent(reporter.CompileFailed, err, "failed to compile the kcl package")
}

return compileResult, nil
}
55 changes: 33 additions & 22 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ func NewKpmClient() (*KpmClient, error) {
}, nil
}

func (c *KpmClient) SetLogWriter(writer io.Writer) {
c.logWriter = writer
}

func (c *KpmClient) GetLogWriter() io.Writer {
return c.logWriter
}

// SetHomePath will set the home path of kpm.
func (c *KpmClient) SetHomePath(homePath string) {
c.homePath = homePath
Expand Down Expand Up @@ -339,26 +347,41 @@ func (c *KpmClient) CompileOciPkg(ociSource, version string, opts *opt.CompileOp
return c.CompileTarPkg(matches[0], opts)
}

// InitEmptyPkg will initialize an empty kcl package.
func (c *KpmClient) InitEmptyPkg(kclPkg *pkg.KclPkg) error {
// createIfNotExist will create a file if it does not exist.
func (c *KpmClient) createIfNotExist(filepath string, storeFunc func() error) error {
reporter.ReportMsgTo(fmt.Sprintf("kpm: creating new :%s", filepath), c.GetLogWriter())
err := utils.CreateFileIfNotExist(
kclPkg.ModFile.GetModFilePath(),
kclPkg.ModFile.StoreModFile,
filepath,
storeFunc,
)
if err != nil {
if errEvent, ok := err.(*reporter.KpmEvent); ok {
if errEvent.Type() != reporter.FileExists {
return err
} else {
reporter.ReportMsgTo(fmt.Sprintf("kpm: '%s' already exists", filepath), c.GetLogWriter())
}
} else {
return err
}
}

return nil
}

// InitEmptyPkg will initialize an empty kcl package.
func (c *KpmClient) InitEmptyPkg(kclPkg *pkg.KclPkg) error {
err := c.createIfNotExist(kclPkg.ModFile.GetModFilePath(), kclPkg.ModFile.StoreModFile)
if err != nil {
return err
}

err = utils.CreateFileIfNotExist(
kclPkg.ModFile.GetModLockFilePath(),
kclPkg.LockDepsVersion,
)
err = c.createIfNotExist(kclPkg.ModFile.GetModLockFilePath(), kclPkg.LockDepsVersion)
if err != nil {
return err
}

// create the default kcl program.
err = createDefaultPkgIn(kclPkg.HomePath)
err = c.createIfNotExist(filepath.Join(kclPkg.ModFile.HomePath, constants.DEFAULT_KCL_FILE_NAME), kclPkg.CreateDefauleMain)
if err != nil {
return err
}
Expand Down Expand Up @@ -991,18 +1014,6 @@ func (c *KpmClient) pullTarFromOci(localPath string, ociOpts *opt.OciOptions) er
return nil
}

// createDefaultPkgIn will create the default kcl program in the local path.
func createDefaultPkgIn(localPath string) error {
mainProgPath := filepath.Join(localPath, constants.DEFAULT_KCL_FILE_NAME)
if !utils.DirExists(mainProgPath) {
err := os.WriteFile(mainProgPath, []byte(constants.DEFAULT_KCL_FILE_CONTENT), 0644)
if err != nil {
return err
}
}
return nil
}

// check sum for a Dependency.
func check(dep pkg.Dependency, newDepPath string) bool {
if dep.Sum == "" {
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/cmd_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ const FLAG_DISABLE_NONE = "disable_none"
const FLAG_ARGUMENT = "argument"
const FLAG_OVERRIDES = "overrides"
const FLAG_SORT_KEYS = "sort_keys"
const FLAG_QUIET = "quiet"
10 changes: 5 additions & 5 deletions pkg/cmd/cmd_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cmd

import (
"fmt"
"os"
"path/filepath"

Expand Down Expand Up @@ -68,13 +69,12 @@ func NewInitCmd(kpmcli *client.KpmClient) *cli.Command {
}

err = kpmcli.InitEmptyPkg(&kclPkg)
if err == nil {
reporter.Report("kpm: package '", pkgName, "' init finished")
} else {
reporter.ExitWithReport(err)
if err != nil {
return err
}

return err
reporter.ReportMsgTo(fmt.Sprintf("kpm: package '%s' init finished", pkgName), kpmcli.GetLogWriter())
return nil
},
}
}
11 changes: 8 additions & 3 deletions pkg/cmd/cmd_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package cmd

import (
"fmt"

"github.com/urfave/cli/v2"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/reporter"
Expand Down Expand Up @@ -31,8 +33,11 @@ func NewLoginCmd(kpmcli *client.KpmClient) *cli.Command {
},
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
reporter.Report("kpm: registry must be specified.")
reporter.ExitWithReport("kpm: run 'kpm registry help' for more information.")
return reporter.NewErrorEvent(
reporter.InvalidCmd,
fmt.Errorf("registry must be specified"),
"run 'kpm login help' for more information",
)
}
registry := c.Args().First()

Expand All @@ -45,7 +50,7 @@ func NewLoginCmd(kpmcli *client.KpmClient) *cli.Command {
if err != nil {
return err
}

reporter.ReportMsgTo("Login Succeeded", kpmcli.GetLogWriter())
return nil
},
}
Expand Down
11 changes: 8 additions & 3 deletions pkg/cmd/cmd_logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package cmd

import (
"fmt"

"github.com/urfave/cli/v2"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/reporter"
Expand All @@ -16,14 +18,17 @@ func NewLogoutCmd(kpmcli *client.KpmClient) *cli.Command {
Usage: "logout from a registry",
Action: func(c *cli.Context) error {
if c.NArg() == 0 {
reporter.Report("kpm: registry must be specified.")
reporter.ExitWithReport("kpm: run 'kpm registry help' for more information.")
return reporter.NewErrorEvent(
reporter.InvalidCmd,
fmt.Errorf("registry must be specified"),
"run 'kpm logout help' for more information",
)
}
err := kpmcli.LogoutOci(c.Args().First())
if err != nil {
return err
}

reporter.ReportMsgTo("kpm: Logout Succeeded", kpmcli.GetLogWriter())
return nil
},
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/cmd/cmd_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cmd

import (
"fmt"
"os"
"path/filepath"

Expand Down Expand Up @@ -37,8 +38,11 @@ func NewPkgCmd(kpmcli *client.KpmClient) *cli.Command {
tarPath := c.String("target")

if len(tarPath) == 0 {
reporter.Report("kpm: The directory where the tar is generated is required.")
reporter.ExitWithReport("kpm: run 'kpm pkg help' for more information.")
return reporter.NewErrorEvent(
reporter.InvalidCmd,
fmt.Errorf("the directory where the tar is generated is required"),
"run 'kpm pkg help' for more information",
)
}

pwd, err := os.Getwd()
Expand Down
Loading

0 comments on commit 900a16d

Please sign in to comment.