Skip to content

Commit

Permalink
feat: search for text through files
Browse files Browse the repository at this point in the history
Building up on the previous feature, it is now possible to search for
text inside a given file.
  • Loading branch information
antonag32 committed Oct 23, 2023
1 parent b63b3c1 commit 5e4cc34
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
42 changes: 32 additions & 10 deletions cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import (
"strings"
)

type ProjectFile struct {
file *gitlab.File
project *gitlab.Project
}

var verbose int
var search *string

var fileCmd = &cobra.Command{
Use: "file name",
Expand All @@ -24,11 +30,19 @@ var fileCmd = &cobra.Command{

func init() {
fileCmd.Flags().CountVarP(&verbose, "verbose", "v", "Make the operation more talkative")
search = fileCmd.Flags().String("search", "", "Search criteria")
}

func fileExecute(_ *cobra.Command, args []string) {
ch := make(chan ProjectFile)
go fileSearch(args[0], search, nil, verbose, ch)
for pj := range ch {
fmt.Printf("✅ Found %s in %s\n", pj.file.FileName, pj.project.NameWithNamespace)
}
}

func fileSearch(fileName string, search *string, branches []string, verbose int, channel chan ProjectFile) {
client := internal.SafeNewClient()
fileName := args[0]
if verbose > 0 {
fmt.Printf("URL Encoded filename is %s\n", gitlab.PathEscape(fileName))
}
Expand All @@ -38,6 +52,7 @@ func fileExecute(_ *cobra.Command, args []string) {
projects, resp, err := client.Projects.ListProjects(&gitlab.ListProjectsOptions{
WithProgrammingLanguage: gitlab.String("Python"),
ListOptions: pagOpts,
Search: search,
})
cobra.CheckErr(err)

Expand All @@ -53,23 +68,30 @@ func fileExecute(_ *cobra.Command, args []string) {
continue
}

if verbose > 1 {
fmt.Printf("Looking in %s\n", proj.NameWithNamespace)
if branches == nil {
branches = []string{proj.DefaultBranch}
}
if searchFile(client, proj, fileName) {
fmt.Printf("✅ Found %s in %s\n", fileName, proj.NameWithNamespace)

for _, branch := range branches {
if verbose > 1 {
fmt.Printf("Looking in %s@%s\n", proj.NameWithNamespace, branch)
}
file, _, _ := client.RepositoryFiles.GetFile(
proj.ID, fileName,
&gitlab.GetFileOptions{Ref: gitlab.String(branch)},
)
if file != nil {
channel <- ProjectFile{file, proj}
}
}

}

if resp.NextPage == 0 {
break
}
pagOpts.Page = resp.NextPage
}
}

func searchFile(client *gitlab.Client, project *gitlab.Project, fileName string) bool {
file, _, _ := client.RepositoryFiles.GetFile(project.ID, fileName, &gitlab.GetFileOptions{Ref: gitlab.String(project.DefaultBranch)})

return file != nil
close(channel)
}
44 changes: 44 additions & 0 deletions cmd/fileText.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"encoding/base64"
"fmt"
"github.com/spf13/cobra"
"strings"
)

var fileTextCmd = &cobra.Command{
Use: "file-text name text",
Short: "Search for text inside a file across repositories",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return fmt.Errorf("2 arguments are required but %d were provided", len(args))
}
return nil
},
Run: fileTextExecute,
}

func init() {
fileTextCmd.Flags().CountVarP(&verbose, "verbose", "v", "Make the operation more talkative")
search = fileTextCmd.Flags().String("search", "", "Search criteria")
}

func fileTextExecute(_ *cobra.Command, args []string) {
ch := make(chan ProjectFile)
fileName := args[0]
searchedText := args[1]

go fileSearch(fileName, search, []string{"12.0", "13.0", "14.0", "15.0", "16.0"}, verbose, ch)
for pj := range ch {
content, err := base64.StdEncoding.DecodeString(pj.file.Content)
if err != nil {
continue
}
contentStr := string(content[:])

if strings.Contains(contentStr, searchedText) {
fmt.Printf("✅ Found %s in file %s in %s @%s\n", searchedText, pj.file.FileName, pj.project.NameWithNamespace, pj.file.Ref)
}
}
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func init() {
initConfig()
rootCmd.AddCommand(configCmd)
rootCmd.AddCommand(fileCmd)
rootCmd.AddCommand(fileTextCmd)
}

func Execute() {
Expand Down

0 comments on commit 5e4cc34

Please sign in to comment.