Skip to content

Commit

Permalink
fix(keybinds): replace struct with map for template inputs (#353)
Browse files Browse the repository at this point in the history
Unfortuneatly it seems the Template options are a little lacking, and
I don't believe can be persuaded to accept zero values or even nil
pointers as 'missing' values.

The struct in question, `PRCommandTemplateInput`, appears to have no use
elsewhere however, so was easily replaced with a map, which I hope
serves your needs for the near future at least.
  • Loading branch information
Omnikron13 committed Apr 18, 2024
1 parent e189492 commit bf2338e
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions ui/modelUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@ type IssueCommandTemplateInput struct {
HeadRefName string
}

type PRCommandTemplateInput struct {
RepoName string
RepoPath string
PrNumber int
HeadRefName string
BaseRefName string
}

func (m *Model) executeKeybinding(key string) tea.Cmd {
currRowData := m.getCurrRowData()

Expand Down Expand Up @@ -108,15 +100,18 @@ func (m *Model) executeKeybinding(key string) tea.Cmd {
}

func (m *Model) runCustomPRCommand(commandTemplate string, prData *data.PullRequestData) tea.Cmd {
repoName := prData.GetRepoNameWithOwner()
repoPath, _ := common.GetRepoLocalPath(repoName, m.ctx.Config.RepoPaths)
// A generic map is a pretty easy & flexible way to populate a template if there's no pressing need
// for sructured data, existing structs, etc. Especially if holes in the data are expected.
input := map[string]any {
"RepoName": prData.GetRepoNameWithOwner(),
"PrNumber": prData.Number,
"HeadRefName": prData.HeadRefName,
"BaseRefName": prData.BaseRefName,
}

input := PRCommandTemplateInput{
RepoName: repoName,
RepoPath: repoPath,
PrNumber: prData.Number,
HeadRefName: prData.HeadRefName,
BaseRefName: prData.BaseRefName,
// Append in the local RepoPath only if it can be found
if repoPath, ok := common.GetRepoLocalPath(input["RepoName"].(string), m.ctx.Config.RepoPaths); ok {
input["RepoPath"] = repoPath
}

cmd, err := template.New("keybinding_command").Parse(commandTemplate)
Expand Down

0 comments on commit bf2338e

Please sign in to comment.