-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from NiclasvanEyk/feature/search-command
`search` subcommand
- Loading branch information
Showing
12 changed files
with
5,192 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,5 @@ | |
- Something This is unexpected | ||
- Something really good | ||
- foo bar foo | ||
- Something | ||
- a `monospaced` item |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
clog "github.com/niclasvaneyk/keepac/internal/changelog" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// searchCmd represents the grep command | ||
var searchCmd = &cobra.Command{ | ||
Use: "search [query]", | ||
Aliases: []string{"grep"}, | ||
Short: "Searches for strings in the nearest changelog and prints matches within their context", | ||
Long: ``, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
changelog, _, err := clog.ResolveChangelog() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
query := args[0] | ||
result := clog.Search(changelog, query) | ||
|
||
if result == "" { | ||
fmt.Println("Nothing matched your query!") | ||
} | ||
|
||
return clog.Show(result + "\n") | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(searchCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package changelog | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
func Search(changelog *Changelog, query string) string { | ||
output := make([]string, 0) | ||
|
||
nextRelease := changelog.Releases.Next | ||
if nextRelease != nil { | ||
includedRelease := false | ||
for _, section := range nextRelease.Sections { | ||
includedSection := false | ||
for _, item := range section.Items { | ||
text := changelog.ContentWithin(&item.Bounds) | ||
if strings.Contains(text, query) { | ||
if !includedRelease { | ||
includedRelease = true | ||
output = append(output, "## "+changelog.ContentWithin(&nextRelease.HeadlineBounds)) | ||
} | ||
|
||
if !includedSection { | ||
includedSection = true | ||
output = append(output, "### "+ChangeTypeLabel(section.Type)) | ||
} | ||
|
||
output = append(output, "- "+text) | ||
} | ||
} | ||
} | ||
} | ||
|
||
for _, release := range changelog.Releases.Past { | ||
includedRelease := false | ||
for _, section := range release.Sections { | ||
includedSection := false | ||
for _, item := range section.Items { | ||
text := changelog.ContentWithin(&item.Bounds) | ||
if strings.Contains(text, query) { | ||
if !includedRelease { | ||
includedRelease = true | ||
output = append(output, "## "+changelog.ContentWithin(&release.HeadlineBounds)) | ||
output = append(output, "") | ||
} | ||
|
||
if !includedSection { | ||
includedSection = true | ||
output = append(output, "### "+ChangeTypeLabel(section.Type)) | ||
output = append(output, "") | ||
} | ||
output = append(output, "- "+text) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return strings.Join(output, "\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package changelog | ||
|
||
import "testing" | ||
|
||
func TestItCanSearchForItems(t *testing.T) { | ||
changelog := Parse([]byte(` | ||
# Changelog | ||
## [Unreleased] | ||
### Added | ||
- Something cool | ||
- Another thing | ||
### Removed | ||
- Support for Go > 1.0 | ||
- Support for Windows | ||
`)) | ||
|
||
actual := Search(&changelog, "Windows") | ||
expected := ` | ||
# Changelog | ||
## [Unreleased] | ||
### Removed | ||
- Support for Windows | ||
` | ||
|
||
if actual != expected { | ||
t.Errorf("Expected does not match actual:\n\n%s", actual) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.