Skip to content

Commit

Permalink
feat: add push command (#2)
Browse files Browse the repository at this point in the history
* fix: don't parse flags in init

This is bad practice which results in spaghetti code down the line.
Moving flag parsing to the actual command execution is cleaner.

See: spf13/cobra#553

* feat: add push command

This new command lets users perform arbitrary actions on mass to a set
of repositories and push the changes upstream.
  • Loading branch information
antonag32 authored Nov 10, 2023
1 parent 77092b8 commit e3e37b0
Show file tree
Hide file tree
Showing 8 changed files with 452 additions and 38 deletions.
11 changes: 11 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- unused
- revive
- gocritic
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
# gul
Command line based utilities for Gitlab

## Available tools

### file: search for repos containing a given file

### file-text: search for repos with given text in a file

### push: batch pushes to repositories after executing a given workflow
This tool is based on the concept of workflows. Each workflow consists of directory containing the following three files:

* targets.txt: list of repositories in format `namespace/project@branch` to perform the work on
* job: this executable will be run on each cloned repository, changes performed by it will be committed and pushed
* message.txt: commit message to be used

To use the tool you first need to configure the following with `gul config`:

1. Set a git user
2. Set a git email
3. Set a git ssh.domain

## Notes
Code here is really radioactive, I tried to integrate some linting into it to keep from going
too spaghetti, but there are no unit tests, just real life testing,
28 changes: 10 additions & 18 deletions cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,21 @@ type ProjectFile struct {
project *gitlab.Project
}

var verbose int
var search *string

var fileCmd = &cobra.Command{
Use: "file name",
Short: "Search for a file across repositories",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("1 argument is required but %d were provided", len(args))
}
return nil
},
Run: fileExecute,
Use: "file name",
Short: "Search for a file across repositories",
Args: cobra.ExactArgs(1),
Run: fileExecute,
DisableFlagParsing: true,
}

func init() {
fileCmd.Flags().CountVarP(&verbose, "verbose", "v", "Make the operation more talkative")
search = fileCmd.Flags().String("search", "", "Search criteria")
}
func fileExecute(cmd *cobra.Command, args []string) {
var verbose = cmd.Flags().CountP("verbose", "v", "Make the operation more talkative")
var search = cmd.Flags().String("search", "", "Search criteria")
cobra.CheckErr(cmd.Flags().Parse(args))

func fileExecute(_ *cobra.Command, args []string) {
ch := make(chan ProjectFile)
go fileSearch(args[0], search, nil, verbose, ch)
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)
}
Expand Down
27 changes: 11 additions & 16 deletions cmd/fileText.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,29 @@ import (
)

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,
Use: "file-text name text",
Short: "Search for text inside a file across repositories",
Args: cobra.ExactArgs(2),
Run: fileTextExecute,
DisableFlagParsing: true,
}

func init() {
fileTextCmd.Flags().CountVarP(&verbose, "verbose", "v", "Make the operation more talkative")
search = fileTextCmd.Flags().String("search", "", "Search criteria")
}
func fileTextExecute(cmd *cobra.Command, args []string) {
var verbose = cmd.Flags().CountP("verbose", "v", "Make the operation more talkative")
var search = cmd.Flags().String("search", "", "Search criteria")
cobra.CheckErr(cmd.Flags().Parse(args))

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)
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[:])
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)
Expand Down
Loading

0 comments on commit e3e37b0

Please sign in to comment.