Filter a text by the keyword

Hi community,

I’m trying to filter a text by the keyword with golang. Basically, what I’m executing:

package main

import (
	"fmt"
	"os/exec"
)

func branches() {
	out, err := exec.Command("git", "branch", "-a", "--sort=-committerdate", "--column", "--format='%(committerdate)%09%(refname:short)'").Output()

	if err != nil {
		// log.Fatal(err)
		fmt.Println("Couldn't find any branches in the repository")
	}

	str1 := string(out)

	fmt.Println(str1)
}

func main() {
	fmt.Printf("1. CHECK ALL BRANCHES: ")
	branches()
}

And getting:

go run main.go
1. CHECK ALL BRANCHES: 'Mon Oct 3 12:20:53 2022 +0000	master'
'Mon Oct 3 12:20:53 2022 +0000	origin/HEAD'
'Mon Oct 3 12:20:53 2022 +0000	origin/master'
'Mon Oct 3 12:12:01 2022 +0000	origin/release/v1'
'Wed Apr 27 06:26:22 2022 +0000	origin/release/v2'
'Tue Feb 15 14:46:55 2022 +0000	origin/release/v3'
'Mon May 24 16:05:45 2021 +0300	origin/release-v1'
'Tue Oct 6 14:43:56 2020 +0300	origin/release-v1.0.0'

The goal is to get all lines with branches that are older than 2022, so 2021, 2020, 2019, etc. (year = keyword, if that helps to reach the main goal) and display those lines in the command line/terminal.

Maybe someone could suggest how to reach that? :slight_smile:

Br,

I have found a way to solve this issue using ‘nested for loop’.

First of all I have converted ‘out’ variable to string, then separated it into fields:

str1 := string(out)

s := strings.Fields(str1)

Second step was to create a string array with dates I’m interested in:

	var strarray [6]string
	strarray[0] = "2016"
	strarray[1] = "2017"
	strarray[2] = "2018"
	strarray[3] = "2019"
	strarray[4] = "2020"
	strarray[5] = "2021"

and finally, nested for loop:

for _, v := range s {
		for _, word := range strarray {
			if word == v {
				fmt.Println("Found some outdated branches: ", v)
			}
		}
	}

It works, but the OUTPUT is not the one I expected:

go run main.go
CHECK ALL BRANCHES:
Found some outdated branches:  2021
Found some outdated branches:  2020

I wondering is it possible to output the whole line with the “keyword” found in the ‘git branch’ output, like:

go run main.go
CHECK ALL BRANCHES:
Found some outdated branches: 
'Mon May 24 16:05:45 2021 +0300	origin/release-v1'
'Tue Oct 6 14:43:56 2020 +0300	origin/release-v1.0.0'

And if nothing was found:

go run main.go
CHECK ALL BRANCHES: OK!

Any suggestions?

Thanks in advance!

Split out into lines instead. Put your target years in a map[string]bool. Use a regular expression to extract the year from each line regexp package - regexp - Go Packages. Check whether the map contains the extracted year as a key, and if so, print a message about finding the obsolete year and set a boolean variable to true. If the variable is still false after the loop, print your success message.

1 Like

Hi @mje,

Thank you for your reply! I have tried to apply your recommendations and could successfully get outdated branches:

    str1 := string(out)

	temp := strings.Split(str1, `\n`)

	// Create a map with years
	var mapper = map[string]bool{
		"2016": true,
		"2017": true,
		"2018": true,
		"2019": true,
		"2020": true,
		"2021": true,
	}

	// Check whether the map contains the extracted year as a key
	for _, v := range temp {

		var notvalidID = regexp.MustCompile(`202([0-1])`)
		var notvalidID2 = regexp.MustCompile(`201([0-9])`)

		tsts := notvalidID2.FindAllString(v, -1)
		tst := notvalidID.FindAllString(v, -1)

		for _, tz := range tst {
			if _, exists := mapper[tz]; exists {

				fmt.Printf("Found outdated branch, year: %s \n", tz)
			} else {
				fmt.Printf("Passed")
			}
		}

		for _, tz := range tsts {
			if _, exists := mapper[tz]; exists {

				fmt.Printf("Found outdated branch, year: %s \n", tz)
			} else {
				fmt.Printf("Passed")
			}
		}
	}

Output:

CHECK ALL BRANCHES:
Found outdated branch, year: 2019
Found outdated branch, year: 2018
Found outdated branch, year: 2018

Instead of the output above is it possible to print the exact line where the key(year) was found? Like:

go run main.go
CHECK ALL BRANCHES:
Found some outdated branches: 
'Mon May 24 16:05:45 2021 +0300	origin/release-v1'
'Tue Oct 6 14:43:56 2020 +0300	origin/release-v1.0.0'

The second thing I’m worried about is that it doesn’t output the task of the “else” statement. Thus, in case nothing was found, “Passed” should be displayed, but it is not:

go run main.go
CHECK ALL BRANCHES:  //"Passed" should be here if nothing was found

Thanks in advance!

Should be

                allGood := true
		for _, tz := range tst {
			if _, exists := mapper[tz]; exists {

				fmt.Printf("Found outdated branch, year: %s \n", tz)
                                allGood = false
			} 
		}
                if allGood {

				fmt.Printf("Passed")

		}

1 Like

Hi @mje,

Thank you for your reply with examples! That helped to solve an issue with if/else statement.

Do you have any ideas how to get the expected “branches output”?

go run main.go
CHECK ALL BRANCHES:
Found some outdated branches:
‘Mon May 24 16:05:45 2021 +0300 origin/release-v1’
‘Tue Oct 6 14:43:56 2020 +0300 origin/release-v1.0.0’

Is that not what v contains?

Hi @HowardParks,

Yes, that’s right. But if I specify v here fmt.Printf("Found outdated branch, year: %s \n", v) it will show an output:

3. CHECK ALL BRANCHES:
Found outdated branch: 'Mon Oct 3 12:20:53 2022 +0000	master'
'Mon Oct 3 12:20:53 2022 +0000	origin/HEAD'
'Mon Oct 3 12:20:53 2022 +0000	origin/master'
'Mon Oct 3 12:12:01 2022 +0000	origin/release/v1.4'
'Fri Sep 30 12:00:51 2022 +0000	origin/development'
'Wed Apr 27 06:26:22 2022 +0000	origin/release/v1.3'
'Tue Feb 15 14:46:55 2022 +0000	origin/release/v1.2'
'Mon May 24 16:05:45 2021 +0300	origin/release-v1.1'
'Tue Oct 6 14:43:56 2020 +0300	origin/release-v1.0.0'

Found outdated branch: 'Mon Oct 3 12:20:53 2022 +0000	master'
'Mon Oct 3 12:20:53 2022 +0000	origin/HEAD'
'Mon Oct 3 12:20:53 2022 +0000	origin/master'
'Mon Oct 3 12:12:01 2022 +0000	origin/release/v1.4'
'Fri Sep 30 12:00:51 2022 +0000	origin/development'
'Wed Apr 27 06:26:22 2022 +0000	origin/release/v1.3'
'Tue Feb 15 14:46:55 2022 +0000	origin/release/v1.2'
'Mon May 24 16:05:45 2021 +0300	origin/release-v1.1'
'Tue Oct 6 14:43:56 2020 +0300	origin/release-v1.0.0'