From ea75ce78975c538bb7ea737a4f083c319f65e1d2 Mon Sep 17 00:00:00 2001 From: Bryce Thuilot Date: Sun, 17 Nov 2024 17:29:25 -0500 Subject: [PATCH] fix: remove find command Signed-off-by: Bryce Thuilot --- cmd/find.go | 60 -------------------------------------------- cmd/root.go | 38 +++++----------------------- cmd/scan.go | 27 ++++++++++++++++++++ main.go | 18 ------------- pkg/scanning/exec.go | 4 +-- 5 files changed, 35 insertions(+), 112 deletions(-) delete mode 100644 cmd/find.go diff --git a/cmd/find.go b/cmd/find.go deleted file mode 100644 index 81f0599..0000000 --- a/cmd/find.go +++ /dev/null @@ -1,60 +0,0 @@ -package cmd - -import ( - "fmt" - "github.com/bthuilot/git-lost-and-found/pkg/git" - "github.com/sirupsen/logrus" - "github.com/spf13/cobra" -) - -func init() { - rootCmd.AddCommand(findCmd) -} - -var findCmd = &cobra.Command{ - Use: "find", - Short: "Find all dangling commits in a repository", - Long: `Finds all dangling commits in a git repository and creates refs for them. -The refs are created in the format 'refs/dangling/'. -If -k is not set, the refs will be removed after the scanner command is executed. -`, - RunE: func(cmd *cobra.Command, args []string) (err error) { - r, dir, cleanup, err := getGitRepository() - if err != nil { - return err - } - defer cleanup() - - logrus.WithField("repository_directory", dir).Info("Scanning repository") - - danglingObjs, err := git.FindDanglingObjects(r, dir) - if err != nil { - return err - } - - // TODO: additional support scanning for blobs - var createdRefs []string - logrus.Infof("Found %d dangling commits", len(danglingObjs.Commits)) - for _, c := range danglingObjs.Commits { - logrus.Debugf("Dangling commit: %s", c.Hash.String()) - ref := fmt.Sprintf("refs/dangling/%s", c.Hash.String()) - if err = git.MakeRef(r, ref, c); err != nil { - logrus.Warnf("Failed to create ref for dangling commit: %s", c.Hash.String()) - continue - } - createdRefs = append(createdRefs, ref) - } - - logrus.WithField("created_refs_amt", len(createdRefs)).Info("created refs for dangling commits") - if !keepRefs { - defer func() { - logrus.WithField("created_refs_amt", len(createdRefs)).Debug("removing created refs") - if err = git.RemoveReferences(r, createdRefs); err != nil { - logrus.Errorf("Failed to remove created refs: %s", err) - } - }() - } - - return nil - }, -} diff --git a/cmd/root.go b/cmd/root.go index 74b6a9f..3108f4b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -10,22 +10,12 @@ import ( "github.com/spf13/cobra" ) -var ( - /* Flags */ - // repoURL is the URL of the git repository to scan - repoURL string - // repoPath is the path to the git repository to scan - repoPath string - // bare is a flag to clone or import the repository as a bare repository - bare bool - // keepRefs is a flag to keep refs created for dangling commits - keepRefs bool - // cleanup is a flag to remove the cloned repo after scanning - // NOTE: only valid when --repo-url is set - cleanup bool - // logLevel is the log level for the application - logLevel string -) +func Execute() { + if err := rootCmd.Execute(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} var rootCmd = &cobra.Command{ Use: "git-lost-and-found", @@ -45,22 +35,6 @@ This allows for scanners that use 'git log' to search blob data to not miss any func init() { rootCmd.SetErrPrefix("ERROR: ") - scanCmd.PersistentFlags().BoolVarP(&bare, "bare", "b", true, "clone or import the repository as a bare repository") - rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "info", "log level (debug, info, warn, error, fatal, panic)") - rootCmd.PersistentFlags().StringVarP(&repoURL, "repo-url", "r", "", "URL of the git repository to scan") - rootCmd.PersistentFlags().StringVarP(&repoPath, "repo-path", "p", "", "Path to the git repository to scan") - rootCmd.PersistentFlags().BoolVarP(&keepRefs, "keep-refs", "k", false, "Keep refs created for dangling commits") - rootCmd.PersistentFlags().BoolVarP(&cleanup, "cleanup", "c", false, "Remove the cloned repository after scanning") - _ = rootCmd.MarkPersistentFlagFilename("repo-path") - rootCmd.MarkFlagsMutuallyExclusive("repo-url", "repo-path") - rootCmd.MarkFlagsOneRequired("repo-url", "repo-path") -} - -func Execute() { - if err := rootCmd.Execute(); err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - } } func getGitRepository() (*gogit.Repository, string, func(), error) { diff --git a/cmd/scan.go b/cmd/scan.go index 892723a..cd39e72 100644 --- a/cmd/scan.go +++ b/cmd/scan.go @@ -8,7 +8,34 @@ import ( "github.com/spf13/cobra" ) +var ( + /* Flags */ + // repoURL is the URL of the git repository to scan + repoURL string + // repoPath is the path to the git repository to scan + repoPath string + // bare is a flag to clone or import the repository as a bare repository + bare bool + // keepRefs is a flag to keep refs created for dangling commits + keepRefs bool + // cleanup is a flag to remove the cloned repo after scanning + // NOTE: only valid when --repo-url is set + cleanup bool + // logLevel is the log level for the application + logLevel string +) + func init() { + scanCmd.Flags().BoolVarP(&bare, "bare", "b", true, "clone or import the repository as a bare repository") + scanCmd.Flags().StringVarP(&logLevel, "log-level", "l", "info", "log level (debug, info, warn, error, fatal, panic)") + scanCmd.Flags().StringVarP(&repoURL, "repo-url", "r", "", "URL of the git repository to scan") + scanCmd.Flags().StringVarP(&repoPath, "repo-path", "p", "", "Path to the git repository to scan") + scanCmd.Flags().BoolVarP(&keepRefs, "keep-refs", "k", false, "Keep refs created for dangling commits") + scanCmd.Flags().BoolVarP(&cleanup, "cleanup", "c", false, "Remove the cloned repository after scanning") + _ = scanCmd.MarkFlagFilename("repo-path") + scanCmd.MarkFlagsMutuallyExclusive("repo-url", "repo-path") + scanCmd.MarkFlagsOneRequired("repo-url", "repo-path") + rootCmd.AddCommand(scanCmd) } diff --git a/main.go b/main.go index 7194ea9..570f03b 100644 --- a/main.go +++ b/main.go @@ -1,27 +1,9 @@ package main import ( - "os" - "github.com/bthuilot/git-lost-and-found/cmd" - "github.com/sirupsen/logrus" ) -func init() { - lvl, ok := os.LookupEnv("LOG_LEVEL") - // LOG_LEVEL not set, let's default to debug - if !ok { - lvl = "debug" - } - // parse string, this is built-in feature of logrus - ll, err := logrus.ParseLevel(lvl) - if err != nil { - ll = logrus.DebugLevel - } - // set global log level - logrus.SetLevel(ll) -} - func main() { cmd.Execute() } diff --git a/pkg/scanning/exec.go b/pkg/scanning/exec.go index 1151972..b591ecc 100644 --- a/pkg/scanning/exec.go +++ b/pkg/scanning/exec.go @@ -1,7 +1,6 @@ package scanning import ( - "errors" "github.com/sirupsen/logrus" "os" "os/exec" @@ -10,7 +9,8 @@ import ( func ExecScanner(dir string, cmdArgs []string) error { if len(cmdArgs) == 0 { - return errors.New("no scanner command provided") + logrus.Warnf("no scanner command provided, exiting") + return nil } for i, arg := range cmdArgs {