cmd

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "iccheck",
	Short: "Finds inconsistent changes in your git changes",
	Long: fmt.Sprintf(`ICCheck %v
Finds inconsistent changes in your git changes.`, cli.GetFormattedVersion()),
	SilenceUsage: true,
	RunE: func(cmd *cobra.Command, args []string) error {
		ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(timeoutSeconds))
		defer cancel()

		if logLevel == "" {
			logLevel = autoDetermineLogLevel()
		}
		switch logLevel {
		case "debug":
			slog.SetLogLoggerLevel(slog.LevelDebug)
		case "info":
			slog.SetLogLoggerLevel(slog.LevelInfo)
		case "warn":
			slog.SetLogLoggerLevel(slog.LevelWarn)
		case "error":
			slog.SetLogLoggerLevel(slog.LevelError)
		default:
			return errors.New("invalid log level")
		}

		ignore, err := domain.ReadIgnoreRules(repoDir, ignoreCLIOptions, disableDefaultIgnore)
		if err != nil {
			return errors.Wrapf(err, "reading ignore rules")
		}

		if repoDir == "" {
			repoDir, err = autoDetermineTopLevelGit()
			if err != nil {
				return errors.Wrapf(err, "detecting git repository")
			}
		}

		repo, err := git.PlainOpen(repoDir)
		if err != nil {
			return errors.Wrapf(err, "opening repository at %v", repoDir)
		}

		if fromRef == "" && toRef != "" {

			fromRef = toRef + "^"
		} else if fromRef != "" && toRef == "" {
			return errors.New("only one of --from was set, this is invalid - do not set for automatic discovery or set both")
		} else if fromRef == "" && toRef == "" {
			fromRef, toRef, err = autoDetermineRefs(repo)
			if err != nil {
				return errors.Wrapf(err, "determining refs")
			}
		}

		fromTree, err := resolveTree(repo, fromRef, false)
		if err != nil {
			return errors.Wrap(err, "resolving base tree")
		}
		toTree, err := resolveTree(repo, toRef, true)
		if err != nil {
			return errors.Wrap(err, "resolving target tree")
		}

		cloneSets, err := search.Search(ctx, algorithm, fromTree, toTree, ignore)
		if err != nil {
			return err
		}

		cloneSets = lo.Filter(cloneSets, func(cs *domain.CloneSet, _ int) bool { return len(cs.Missing) > 0 })

		missingChanges := lo.SumBy(cloneSets, func(set *domain.CloneSet) int { return len(set.Missing) })
		if missingChanges == 0 {
			slog.Info(fmt.Sprintf("No clones are missing consistent change."))
		} else {
			slog.Info(fmt.Sprintf("%d clone(s) are likely missing consistent change.", missingChanges))
		}

		printer := getPrinter()
		out := printer.PrintClones(cloneSets)
		fmt.Print(string(out))

		if len(cloneSets) > 0 && failCode != 0 {
			os.Exit(failCode)
		}
		return nil
	},
}

RootCmd represents the base command when called without any subcommands

Functions

func Execute

func Execute()

Execute adds all child commands to the root command and sets flags appropriately. This is called by main.main(). It only needs to happen once to the rootCmd.

Types

This section is empty.

Jump to

Keyboard shortcuts

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