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.
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'
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.
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
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’