Giter VIP home page Giter VIP logo

gh-grep's Introduction

gh-grep

:octocat: Print lines matching a pattern in repositories using GitHub API

Usage

$ gh grep func.*schema.Schema --include=**/*.go --owner k1LoW --repo tbls
k1LoW/tbls:cmd/doc.go:func withDot(s *schema.Schema, c *config.Config, force bool) (e error) {
k1LoW/tbls:cmd/doc.go:func outputErExists(s *schema.Schema, path string) bool {
k1LoW/tbls:config/config.go:func (c *Config) ModifySchema(s *schema.Schema) error {
k1LoW/tbls:config/config.go:func (c *Config) MergeAdditionalData(s *schema.Schema) error {
k1LoW/tbls:config/config.go:func (c *Config) FilterTables(s *schema.Schema) error {
k1LoW/tbls:config/config.go:func (c *Config) mergeDictFromSchema(s *schema.Schema) {
k1LoW/tbls:config/config.go:func excludeTableFromSchema(name string, s *schema.Schema) error {
[...]

( Do grep the codes (**/*.go) of k1LoW/tbls with the pattern func.*schema.Schema )

$ gh grep --help
Print lines matching a pattern in repositories using GitHub API

Usage:
  gh-grep [PATTERN] [flags]

Flags:
  -e, -- strings         match pattern
      --branch string    branch name
  -c, --count            show the number of matches instead of matching lines
      --exclude string   skip files and directories matching pattern
  -h, --help             help for gh-grep
  -i, --ignore-case      case insensitive matching
      --include string   search only files that match pattern (default "**/*")
  -n, --line-number      show line numbers
      --name-only        show only repository:filenames
  -o, --only-matching    show only matching parts of a line
      --owner string     repository owner or org
      --repo strings     repository name
      --repo-only        show only repositories
      --tag string       tag name
      --url              show URL
  -v, --version          version for gh-grep

⚠️ Notice ⚠️

gh-grep is very slow because it does all its scanning through the GitHub API.

It is recommended to specify the --include option to get the results in a realistic time.

Examples

List base Docker images used in the Dockerfile of the project root

$ gh grep ^FROM --include=Dockerfile --owner k1LoW
k1LoW/centve:Dockerfile:FROM centos:7
k1LoW/docker-alpine-pandoc-ja:Dockerfile:FROM frolvlad/alpine-glibc
k1LoW/docker-sshd:Dockerfile:FROM docker.io/alpine:3.9
k1LoW/gh-grep:Dockerfile:FROM debian:buster-slim
k1LoW/ghdag:Dockerfile:FROM debian:buster-slim
k1LoW/ghdag-action:Dockerfile:FROM ghcr.io/k1low/ghdag:v0.16.0
k1LoW/ghput:Dockerfile:FROM alpine:3.13
k1LoW/ghput-release-action:Dockerfile:FROM ghcr.io/k1low/ghput:v0.12.0
k1LoW/github-script-ruby:Dockerfile:FROM ghcr.io/k1low/github-script-ruby-base:v1.1.0
[...]

List Actions you are using

$ gh grep uses: --include=.github/workflows/* --owner k1LoW | sed -e 's/.*uses:\s*//g' | sort | uniq -c
   9 ./
   1 EndBug/add-and-commit@v7
   2 actions/checkout@master
  10 actions/checkout@v1
  50 actions/checkout@v2
  18 actions/setup-go@v1
  21 actions/setup-go@v2
   4 aquasecurity/trivy-action@master
[...]

Open the matched lines in a browser.

$ gh grep 'ioutil\.' --include=**/*.go --owner k1LoW --repo ghput --url
https://github.com/k1LoW/ghput/blob/main/gh/gh.go#L300
https://github.com/k1LoW/ghput/blob/main/gh/gh.go#L313
$ gh grep 'ioutil\.' --include=**/*.go --owner k1LoW --repo ghput --url | xargs open

*open command only works on macOS.

Install

gh-grep can be installed as a standalone command or as a GitHub CLI extension

Install as a GitHub CLI extension

$ gh extension install k1LoW/gh-grep

Install as a standalone command

Run gh-grep instead of gh grep.

deb:

$ export GH_GREP_VERSION=X.X.X
$ curl -o gh-grep.deb -L https://github.com/k1LoW/gh-grep/releases/download/v$GH_GREP_VERSION/gh-grep_$GH_GREP_VERSION-1_amd64.deb
$ dpkg -i gh-grep.deb

RPM:

$ export GH_GREP_VERSION=X.X.X
$ yum install https://github.com/k1LoW/gh-grep/releases/download/v$GH_GREP_VERSION/gh-grep_$GH_GREP_VERSION-1_amd64.rpm

apk:

$ export GH_GREP_VERSION=X.X.X
$ curl -o gh-grep.apk -L https://github.com/k1LoW/gh-grep/releases/download/v$GH_GREP_VERSION/gh-grep_$GH_GREP_VERSION-1_amd64.apk
$ apk add gh-grep.apk

homebrew tap:

$ brew install k1LoW/tap/gh-grep

macports:

$ sudo port install gh-grep

Maintainer: @herbygillot

manually:

Download binary from releases page

go get:

$ go get github.com/k1LoW/gh-grep

docker:

$ docker pull ghcr.io/k1low/gh-grep:latest

gh-grep's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar herbygillot avatar k1low avatar mattn avatar pyama86 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

gh-grep's Issues

It doesn't work without authentication credentials for github.com.

gh-grep will encounter an error if only authentication credentials for GitHub Enterprise Server are available. This is a problem when using GitHub Actions with GitHub Enterprise Server.

I'll describe how to reproduce the issue below.

# authorized my-github.example.com

$ gh auth status
<my-github.example.com>
  ✓ Logged in to <my-github.example.com> as <my-user> (GH_ENTERPRISE_TOKEN)
  ✓ Git operations for <my-github.example.com> configured to use https protocol.
  ✓ Token: ghp_************************************
  ✓ Token scopes: admin:enterprise, admin:gpg_key, admin:org, admin:org_hook, admin:pre_receive_hook, admin:public_key, admin:repo_hook, admin:ssh_signing_key, audit_log, delete:packages, delete_repo, gist, notifications, project, repo, user, workflow, write:discussion, write:packages


# run gh grep

$ gh grep --owner me test
non-200 OK status code: 401 Unauthorized body: "{\"message\":\"This endpoint requires you to be authenticated.\",\"documentation_url\":\"https://docs.github.com/graphql/guides/forming-calls-with-graphql#authenticating-with-graphql\"}"
no assets match the file pattern


# other sub commands are avaiable

$ gh repo view 'myuser/myrepo' # => ok
$ gh api graphql -f query="query { viewer { email } }" # => ok

After sign-in github.com, gh grep will work for GitHub Enterprise Server.

When GH_HOST is set, it becomes unusable.

ref: #29

if GH_HOST is set to anything other than github.com, gh grep cannot be executed. Because it cannot retrieve the tag, as it is now set to retrieve the tag using gh-cli.

Specifically, the tag becomes an empty string, and the string for the exe becomes like gh-grep__linux_amd64. It can be reproduced with the following command.

GH_HOST=<your-github.example.com> gh grep help

Upgrade dependency "github.com/k1LoW/go-github-client"

Background

Repo github.com/k1LoW/gh-grep depends on github.com/k1LoW/go-github-client/[email protected].

https://github.com/k1LoW/gh-grep/blob/main/go.mod#L11

However, comparing version v39.2.2 of github.com/k1LoW/go-github-client from proxy.golang.org and github, there are inconsistencies.

commit time of the copy on github.com

"committer": {
      "name": "k1LoW",
      "email": "[email protected]",
      "date": "2022-01-31T12:31:21Z"
    }

commit time of the copy on proxy.golang.org

{"Version":"v39.2.2","Time":"2021-11-08T07:14:18Z"}

So the checksum from the code in github does not match the checksum saved in sum.golang.org. The v39.2.2 tag of github.com/k1LoW/go-github-client might have been retagged after a minor edition on github. I guess you use proxy.golang.org to get dependencies, but that also shows that your project is depending on the copy of github.com/k1LoW/[email protected] before its edition. Depending upon such inconsistent tag version may also result in some unexpected errors as well as build errors due to different proxy settings.

For example, when someone who does not use proxy.golang.org, say GOPROXY=direct, attempts to get github.com/k1LoW/go-github-client/[email protected], the following error occurs.

go: downloading github.com/k1LoW/go-github-client/v39 v39.2.2
go: github.com/k1LoW/go-github-client/v39@v39.2.2: verifying module: checksum mismatch
        downloaded: h1:MykkzFe7dTBOWOTPfiw1gvbcRbBD0SVN1KtYWFPyftg=
        sum.golang.org: h1:BML9eBIvMnlM+u2awm8hugeaT//dd8ilR3sM5i/FzG8=

SECURITY ERROR
This download does NOT match the one reported by the checksum server.
The bits may have been replaced on the origin server, or an attacker may
have intercepted the download attempt.

For more information, see 'go help module-auth'.

So, this is a reminder in the hope that you can get rid of this problematic version of project github.com/k1LoW/go-github-client.

Solution

1. Bump the version of dependency github.com/k1LoW/go-github-client

I would recommend bumping the version of github.com/k1LoW/go-github-client to a new release to ensure dependency copy in proxy.golang.org and github in sync.

References

"Add support for secure token storage"

Token storage change in latest release of gh

This is a message from the GitHub CLI team, maintainers of gh, writing to inform you that the most recent release of gh contains changes which may affect your extension. The latest release introduces the feature of storing authentication tokens in the system keyring (encrypted storage) instead of in a plain text file.
The keyrings that are supported are:

  • Keychain on macOS

  • GNOME Keyring on Linux (Secret Service dbus interface)

  • Wincred on Windows

This has huge security benefits for the users of our tool and was one of our oldest outstanding issues. Unfortunately this change has the potential to break extensions that rely on utilizing the users authentication token to work.

In order to have continued compatibility with gh there are some actions you, as an extension author, need to take. These actions will depend on the implementation of your extension.

Extensions built in Go using go-gh:

  1. Upgrade your go-gh version to v1.2.1, the latest version.

  2. Verify that in your extension retrieval of the user authentication token is done using the auth.TokenForHost function.

    • If you were previously accessing the authentication token using any other method it will no longer work.
    • Automatic resolution of the authentication token when using the API clients will continue to work without changes.

All other extensions:

  1. Verify that in your extension retrieval of the user authentication token is done by shelling out to the gh auth token command.

    • If you were previously accessing the authentication token using the gh config get command, reading the configuration file directly, or any other methods it will no longer work.

As of right now storing the authentication token in the system keyring is an opt-in feature, but in the near future it will be required and at that point if the changes above are not made then your extension will be broken for all users. If you have any questions/concerns about this change please feel free to open a discussion in the gh repo.

Thanks,
The GitHub CLI Team

Error: env GITHUB_TOKEN is not set

In my environment gh grep throws an error "Error: env GITHUB_TOKEN is not set".
Other built-in commands from gh (like gh repo) work without any error.

$ gh version
gh version 2.2.0 (2021-10-25)

$ gh auth status

github.com
  ✓ Logged in to github.com as lacolaco (***)
  ✓ Git operations for github.com configured to use https protocol.
  ✓ Token: *******************

I've just executed gh extension command for installation. Are there any additional steps?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.