commands

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2022 License: Apache-2.0 Imports: 13 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 {
		var result header.Result

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

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

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

		writeSummaryQuietly(&result)

		if result.HasFailure() {
			if err := review.Header(&result, &Config); err != nil {
				logger.Log.Warnln("Failed to create review comments", err)
			}
			return result.Error()
		}

		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 {
		return deps.Check(Config.Header.License.SpdxID, &Config.Deps)
	},
}
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(summaryTplPath)
			if err != nil {
				return err
			}
			summaryTpl = tpl
		}
		return nil
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		report := deps.Report{}

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

		if summaryTpl != nil {
			if err := writeSummary(&report); 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 result header.Result
		var errors []string
		var files []string

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

		for _, file := range files {
			if err := header.Fix(file, &Config.Header, &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