Skip to content

Commit

Permalink
Merge pull request #12 from k1LoW/only
Browse files Browse the repository at this point in the history
Add options `--name-only` `--repo-only`
  • Loading branch information
k1LoW authored Nov 1, 2021
2 parents 93e192f + ea10e52 commit 9b96509
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package cmd

import (
"context"
"errors"
"io"
"log"
"os"
Expand Down Expand Up @@ -82,7 +83,11 @@ var rootCmd = &cobra.Command{
fsys := ghfs.NewWithGitHubClient(g.Client(), opts.Owner, repo)
opts.Repo = repo
if err := scanner.Scan(ctx, fsys, os.Stdout, &opts); err != nil {
return err
if errors.Is(err, &scanner.RepoOnlyError{}) {
continue
} else {
return err
}
}
}
return nil
Expand Down Expand Up @@ -113,5 +118,7 @@ func init() {
rootCmd.Flags().StringVarP(&opts.Exclude, "exclude", "", "", "skip files and directories matching pattern")
rootCmd.Flags().BoolVarP(&opts.LineNumber, "line-number", "n", false, "show line numbers")
rootCmd.Flags().BoolVarP(&ignoreCase, "ignore-case", "i", false, "case insensitive matching")
rootCmd.Flags().BoolVarP(&opts.NameOnly, "name-only", "", false, "show only repogitory:filenames")
rootCmd.Flags().BoolVarP(&opts.RepoOnly, "repo-only", "", false, "show only repogitory")
rootCmd.Flags().StringSliceVarP(&patterns, "", "e", []string{}, "match pattern")
}
22 changes: 22 additions & 0 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ var (
delimiter = color.New(color.FgCyan).Sprint(":")
)

type RepoOnlyError struct{}

func (e *RepoOnlyError) Error() string {
return "already errored"
}

type Opts struct {
Patterns []*regexp.Regexp
Owner string
Repo string
Include string
Exclude string
LineNumber bool
NameOnly bool
RepoOnly bool
}

func Scan(ctx context.Context, fsys fs.FS, w io.Writer, opts *Opts) error {
Expand Down Expand Up @@ -86,6 +94,20 @@ func Scan(ctx context.Context, fsys fs.FS, w io.Writer, opts *Opts) error {
matches = f

if len(matches) > 0 {
if opts.RepoOnly {
if _, err := fmt.Fprintf(w, "%s/%s\n", opts.Owner, opts.Repo); err != nil {
return err
}
return new(RepoOnlyError)
}

if opts.NameOnly {
if _, err := fmt.Fprintf(w, "%s/%s%s%s\n", opts.Owner, opts.Repo, delimiter, path); err != nil {
return err
}
break
}

if opts.LineNumber {
if _, err := fmt.Fprintf(w, "%s/%s%s%s%s%d%s%s\n", opts.Owner, opts.Repo, delimiter, path, delimiter, n, delimiter, internal.PrintLine(line, matches, matchc)); err != nil {
return err
Expand Down

0 comments on commit 9b96509

Please sign in to comment.