commands

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CheckCommand = &cobra.Command{
	Use:     "check",
	Aliases: []string{"c"},
	Long:    "check command walks the specified paths recursively and checks if the specified files have the license header in the config file.",
	RunE: func(cmd *cobra.Command, args []string) error {
		hasErrors := false
		for _, h := range Config.Headers() {
			var result header.Result

			if len(args) > 0 {
				logger.Log.Debugln("Overriding paths with command line args.")
				h.Paths = args
			}

			if err := header.Check(h, &result); err != nil {
				return err
			}

			logger.Log.Infoln(result.String())

			writeSummaryQuietly(&result)

			if result.HasFailure() {
				if err := review.Header(&result, h); err != nil {
					logger.Log.Warnln("Failed to create review comments", err)
				}
				hasErrors = true
				logger.Log.Error(result.Error())
			}
		}
		if hasErrors {
			return fmt.Errorf("one or more files does not have a valid license header")
		}
		return nil
	},
}
View Source
var (
	Config config.Config
)
View Source
var Deps = &cobra.Command{
	Use:     "dependency",
	Aliases: []string{"d", "deps", "dep", "dependencies"},
	Short:   "Dependencies related commands; e.g. check, etc.",
	Long:    "deps command checks all dependencies of a module and their transitive dependencies.",
}
View Source
var DepsCheckCommand = &cobra.Command{
	Use:     "check",
	Aliases: []string{"c"},
	Long:    "resolves and check license compatibility in all dependencies of a module and their transitive dependencies",
	RunE: func(cmd *cobra.Command, args []string) error {
		var errors []error
		configDeps := Config.Dependencies()
		for _, header := range Config.Headers() {
			if err := deps.Check(header.License.SpdxID, configDeps, weakCompatible); err != nil {
				errors = append(errors, err)
			}
		}
		if len(errors) > 0 {
			for _, err := range errors {
				logger.Log.Error(err)
			}
			return fmt.Errorf("one or more errors occurred checking license compatibility")
		}
		return nil
	},
}
View Source
var DepsResolveCommand = &cobra.Command{
	Use:     "resolve",
	Aliases: []string{"r"},
	Long:    "resolves all dependencies of a module and their transitive dependencies",
	PreRunE: func(cmd *cobra.Command, args []string) error {
		if outDir != "" {
			absPath, err := filepath.Abs(outDir)
			if err != nil {
				return err
			}
			outDir = absPath
			if err := os.MkdirAll(outDir, 0o700); err != nil && !os.IsExist(err) {
				return err
			}
		}
		if summaryTplPath != "" {
			absPath, err := filepath.Abs(summaryTplPath)
			if err != nil {
				return err
			}
			summaryTplPath = absPath
			tpl, err := deps.ParseTemplate(os.DirFS(filepath.Dir(absPath)), filepath.Base(absPath))
			if err != nil {
				return err
			}
			summaryTpl = tpl
		}
		if licensePath != "" {
			absPath, err := filepath.Abs(licensePath)
			if err != nil {
				return err
			}
			licensePath = absPath
			if err := os.MkdirAll(filepath.Dir(outDir), 0o700); err != nil && !os.IsExist(err) {
				return err
			}

			if summaryTpl == nil {
				tpl, err := deps.ParseTemplate(assets.FS(), "default-license.tpl")
				if err != nil {
					return err
				}
				summaryTpl = tpl
			}
		}
		return nil
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		report := deps.Report{}

		configDeps := Config.Dependencies()
		if err := deps.Resolve(configDeps, &report); err != nil {
			return err
		}

		if summaryTpl != nil {
			if err := writeSummary(&report, licensePath); err != nil {
				return err
			}
		}

		if outDir != "" {
			for _, result := range report.Resolved {
				writeLicense(result)
			}
		}

		fmt.Println(report.String())

		if skipped := len(report.Skipped); skipped > 0 {
			pkgs := make([]string, skipped)
			for i, s := range report.Skipped {
				pkgs[i] = s.Dependency
			}
			return fmt.Errorf(
				"failed to identify the licenses of following packages (%d):\n%s",
				len(pkgs), strings.Join(pkgs, "\n"),
			)
		}

		return nil
	},
}
View Source
var FixCommand = &cobra.Command{
	Use:     "fix",
	Aliases: []string{"f"},
	Long:    "fix command walks the specified paths recursively and fix the license header if the specified files don't have the license header.",
	RunE: func(cmd *cobra.Command, args []string) error {
		var errors []string
		for _, h := range Config.Headers() {
			var result header.Result
			var files []string

			if len(args) > 0 {
				files = args
			} else if err := header.Check(h, &result); err != nil {
				return err
			} else {
				files = result.Failure
			}

			for _, file := range files {
				if err := header.Fix(file, h, &result); err != nil {
					errors = append(errors, err.Error())
				}
			}

			logger.Log.Infoln(result.String())
		}
		if len(errors) > 0 {
			return fmt.Errorf("%s", strings.Join(errors, "\n"))
		}
		return nil
	},
}
View Source
var Header = &cobra.Command{
	Use:     "header",
	Aliases: []string{"h"},
	Short:   "License header related commands; e.g. check, fix, etc.",
	Long:    "`header` command walks the specified paths recursively and checks if the specified files have the license header in the config file.",
}

Functions

func Execute

func Execute() error

Execute sets flags to the root command appropriately. This is called by main.main(). It only needs to happen once to the root.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL