Hello group,
I’m making an attempt to filter a textual content by the key phrase with golang. Principally, what I’m executing:
bundle fundamental
import (
"fmt"
"os/exec"
)
func branches() {
out, err := exec.Command("git", "department", "-a", "--sort=-committerdate", "--column", "--format="%(committerdate)%09%(refname:quick)"").Output()
if err != nil {
// log.Deadly(err)
fmt.Println("Could not discover any branches within the repository")
}
str1 := string(out)
fmt.Println(str1)
}
func fundamental() {
fmt.Printf("1. CHECK ALL BRANCHES: ")
branches()
}
And getting:
go run fundamental.go
1. CHECK ALL BRANCHES: 'Mon Oct 3 12:20:53 2022 +0000 grasp'
'Mon Oct 3 12:20:53 2022 +0000 origin/HEAD'
'Mon Oct 3 12:20:53 2022 +0000 origin/grasp'
'Mon Oct 3 12:12:01 2022 +0000 origin/launch/v1'
'Wed Apr 27 06:26:22 2022 +0000 origin/launch/v2'
'Tue Feb 15 14:46:55 2022 +0000 origin/launch/v3'
'Mon Might 24 16:05:45 2021 +0300 origin/release-v1'
'Tue Oct 6 14:43:56 2020 +0300 origin/release-v1.0.0'
The aim is to get all strains with branches which are older than 2022, so 2021, 2020, 2019, and many others. (yr = key phrase, if that helps to succeed in the primary aim) and show these strains within the command line/terminal.
Possibly somebody might recommend tips on how to attain that?
Br,
I’ve discovered a strategy to remedy this difficulty utilizing ‘nested for loop’.
To begin with I’ve transformed ‘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 all for:
var strarray [6]string
strarray[0] = "2016"
strarray[1] = "2017"
strarray[2] = "2018"
strarray[3] = "2019"
strarray[4] = "2020"
strarray[5] = "2021"
and at last, nested for loop:
for _, v := vary s {
for _, phrase := vary strarray {
if phrase == v {
fmt.Println("Discovered some outdated branches: ", v)
}
}
}
It really works, however the OUTPUT just isn’t the one I anticipated:
go run fundamental.go
CHECK ALL BRANCHES:
Discovered some outdated branches: 2021
Discovered some outdated branches: 2020
I questioning is it potential to output the entire line with the “key phrase” discovered within the ‘git department’ output, like:
go run fundamental.go
CHECK ALL BRANCHES:
Discovered some outdated branches:
'Mon Might 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 discovered:
go run fundamental.go
CHECK ALL BRANCHES: OK!
Any ideas?
Thanks prematurely!
Cut up out
into strains as a substitute. Put your goal years in a map[string]bool
. Use an everyday expression to extract the yr from every line regexp bundle – regexp – Go Packages. Verify whether or not the map
incorporates the extracted yr as a key, and if that’s the case, print a message about discovering the out of date yr and set a boolean variable to true
. If the variable continues to be false
after the loop, print your success message.
1 Like
Hello @mje,
Thanks to your reply! I’ve tried to use your suggestions and will efficiently get outdated branches:
str1 := string(out)
temp := strings.Cut up(str1, `n`)
// Create a map with years
var mapper = map[string]bool{
"2016": true,
"2017": true,
"2018": true,
"2019": true,
"2020": true,
"2021": true,
}
// Verify whether or not the map incorporates the extracted yr as a key
for _, v := vary 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 := vary tst {
if _, exists := mapper[tz]; exists {
fmt.Printf("Discovered outdated department, yr: %s n", tz)
} else {
fmt.Printf("Handed")
}
}
for _, tz := vary tsts {
if _, exists := mapper[tz]; exists {
fmt.Printf("Discovered outdated department, yr: %s n", tz)
} else {
fmt.Printf("Handed")
}
}
}
Output:
CHECK ALL BRANCHES:
Discovered outdated department, yr: 2019
Discovered outdated department, yr: 2018
Discovered outdated department, yr: 2018
As an alternative of the output above is it potential to print the precise line the place the important thing(yr) was discovered? Like:
go run fundamental.go
CHECK ALL BRANCHES:
Discovered some outdated branches:
'Mon Might 24 16:05:45 2021 +0300 origin/release-v1'
'Tue Oct 6 14:43:56 2020 +0300 origin/release-v1.0.0'
The second factor I’m nervous about is that it doesn’t output the duty of the “else” assertion. Thus, in case nothing was discovered, “Handed” must be displayed, however it’s not:
go run fundamental.go
CHECK ALL BRANCHES: //"Handed" must be right here if nothing was discovered
Thanks prematurely!
Ought to be
allGood := true
for _, tz := vary tst {
if _, exists := mapper[tz]; exists {
fmt.Printf("Discovered outdated department, yr: %s n", tz)
allGood = false
}
}
if allGood {
fmt.Printf("Handed")
}
1 Like
Hello @mje,
Thanks to your reply with examples! That helped to unravel a problem with if/else assertion.
Do you will have any concepts tips on how to get the anticipated “branches output”?
go run fundamental.go
CHECK ALL BRANCHES:
Discovered some outdated branches:
‘Mon Might 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 incorporates?