Skip to content

Commit

Permalink
Merge pull request #424 from rsteube/added-glab
Browse files Browse the repository at this point in the history
added glab
  • Loading branch information
rsteube authored Jul 6, 2021
2 parents eb1c3d9 + 3d85b6d commit 92eca68
Show file tree
Hide file tree
Showing 95 changed files with 2,586 additions and 0 deletions.
35 changes: 35 additions & 0 deletions completers/glab_completer/cmd/action/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package action

import (
"io/ioutil"
"os"

"github.com/rsteube/carapace"
"gopkg.in/yaml.v3"
)

func loadAliases() (aliases map[string]string, err error) {
var dir string
if dir, err = os.UserConfigDir(); err == nil {
var content []byte
if content, err = ioutil.ReadFile(dir + "/glab-cli/aliases.yml"); err == nil {
err = yaml.Unmarshal(content, &aliases)
}
}
return
}

func ActionAliases() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
aliases, err := loadAliases()
if err != nil {
return carapace.ActionMessage(err.Error())
}

vals := make([]string, 0)
for alias, desc := range aliases {
vals = append(vals, alias, desc)
}
return carapace.ActionValuesDescribed(vals...)
})
}
50 changes: 50 additions & 0 deletions completers/glab_completer/cmd/action/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package action

import (
"encoding/json"
"fmt"
"net/url"
"strings"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

func actionApi(cmd *cobra.Command, query string, v interface{}, transform func() carapace.Action) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if flag := cmd.Flag("repo"); flag != nil && flag.Changed {
project := strings.Join(strings.Split(flag.Value.String(), "/")[1:], "/")
query = strings.Replace(query, ":fullpath", url.PathEscape(project), 1)
}

args := []string{"api"}
if flag := cmd.Flag("repo"); flag != nil && flag.Changed {
host := strings.Split(flag.Value.String(), "/")[0]
args = append(args, "--hostname", host)
}
args = append(args, query)
return carapace.ActionExecCommand("glab", args...)(func(output []byte) carapace.Action {
if err := json.Unmarshal(output, &v); err != nil {
return carapace.ActionMessage("failed to unmarshall response: " + err.Error())
}
return transform()
})
})
}

func actionGraphql(cmd *cobra.Command, query string, v interface{}, transform func() carapace.Action) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
args := []string{"api", "graphql"}
if flag := cmd.Flag("repo"); flag != nil && flag.Changed {
host := strings.Split(flag.Value.String(), "/")[0]
args = append(args, "--hostname", host)
}
args = append(args, "-f", fmt.Sprintf("query=%v", query))
return carapace.ActionExecCommand("glab", args...)(func(output []byte) carapace.Action {
if err := json.Unmarshal(output, &v); err != nil {
return carapace.ActionMessage("failed to unmarshall response: " + err.Error())
}
return transform()
})
})
}
30 changes: 30 additions & 0 deletions completers/glab_completer/cmd/action/branch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package action

import (
"fmt"
"net/url"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

type branch struct {
Name string
Commit struct {
Title string
}
}

func ActionBranches(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
var branches []branch
query := fmt.Sprintf("/projects/:fullpath/repository/branches?search=^%v", url.QueryEscape(c.CallbackValue))
return actionApi(cmd, query, &branches, func() carapace.Action {
vals := make([]string, 0, len(branches)*2)
for _, branch := range branches {
vals = append(vals, branch.Name, branch.Commit.Title)
}
return carapace.ActionValuesDescribed(vals...)
})
})
}
73 changes: 73 additions & 0 deletions completers/glab_completer/cmd/action/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package action

import (
"io/ioutil"
"os"

"github.com/rsteube/carapace"
"gopkg.in/yaml.v3"
)

func ActionConfigHosts() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
if hosts, err := hosts(); err != nil {
return carapace.ActionMessage(err.Error())
} else {
return carapace.ActionValues(hosts...)
}
})
}

type glabConfig struct {
Hosts map[string]interface{}
}

func hosts() ([]string, error) {
config, err := loadConfig()
if err != nil {
return nil, err
}

hosts := make([]string, 0)
for host := range config.Hosts {
hosts = append(hosts, host)
}
return hosts, nil
}

func loadConfig() (config *glabConfig, err error) {
var dir string
if dir, err = os.UserConfigDir(); err == nil {
var content []byte
if content, err = ioutil.ReadFile(dir + "/glab-cli/config.yml"); err == nil {
err = yaml.Unmarshal(content, &config)
}
}
return
}

func ActionConfigKeys() carapace.Action {
return carapace.ActionValuesDescribed(
"token", "Your gitlab access token, defaults to environment variables",
"gitlab_uri", "if unset, defaults to https://gitlab.com",
"browser", "if unset, defaults to environment variables",
"editor", "if unset, defaults to environment variables.",
"visual", "alternative for editor. if unset, defaults to environment variables.",
"glamour_style", "Your desired markdown renderer style.",
)
}

func ActionConfigValues(key string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
actions := map[string]carapace.Action{
"token": carapace.ActionValues(),
"gitlab_uri": carapace.ActionValues(),
"browser": carapace.ActionFiles(),
"editor": carapace.ActionFiles(),
"visual": carapace.ActionFiles(),
"glamour_style": carapace.ActionValues("dark", "light", "notty"),
}

return actions[key]
})
}
24 changes: 24 additions & 0 deletions completers/glab_completer/cmd/action/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package action

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

type environment struct {
Name string
ExternalUrl string `json:"external_url"`
}

func ActionEnvironments(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
var queryResult []environment
return actionApi(cmd, "projects/:fullpath/environments", &queryResult, func() carapace.Action {
vals := make([]string, 0, len(queryResult)*2)
for _, env := range queryResult {
vals = append(vals, env.Name, env.ExternalUrl)
}
return carapace.ActionValuesDescribed(vals...)
})
})
}
48 changes: 48 additions & 0 deletions completers/glab_completer/cmd/action/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package action

import (
"fmt"
"net/url"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

type group struct {
Path string
Description string
}

func ActionGroups(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
query := fmt.Sprintf(`groups?all_available=true&search=%v`, url.QueryEscape(c.CallbackValue))

var queryResult []group
return actionApi(cmd, query, queryResult, func() carapace.Action {
vals := make([]string, 0, len(queryResult)*2)
for _, group := range queryResult {
vals = append(vals, group.Path, group.Description)
}
return carapace.ActionValuesDescribed(vals...)
})
})
}

func ActionSubgroups(cmd *cobra.Command, groupID string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
query := fmt.Sprintf(`groups/%v/subgroups?all_available=true&search=%v`, url.PathEscape(groupID), url.QueryEscape(c.CallbackValue))

var queryResult []group
return actionApi(cmd, query, &queryResult, func() carapace.Action {
// TODO allow failure
// if strings.Contains(err.Error(), "404 Group Not Found") {
// return carapace.ActionValues() // fail silently for repo completion
// }
vals := make([]string, 0, len(queryResult)*2)
for _, group := range queryResult {
vals = append(vals, group.Path, group.Description)
}
return carapace.ActionValuesDescribed(vals...)
})
})
}
33 changes: 33 additions & 0 deletions completers/glab_completer/cmd/action/issue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package action

import (
"fmt"
"net/url"
"strconv"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

type issue struct {
Iid int
Title string
}

func ActionIssues(cmd *cobra.Command, state string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
stateQuery := ""
if state != "" {
stateQuery = "&state=" + url.QueryEscape(state)
}

var queryResult []issue
return actionApi(cmd, fmt.Sprintf("projects/:fullpath/issues?order_by=updated_at&per_page=100%v", stateQuery), &queryResult, func() carapace.Action {
vals := make([]string, 0, len(queryResult)*2)
for _, issue := range queryResult {
vals = append(vals, strconv.Itoa(issue.Iid), issue.Title)
}
return carapace.ActionValuesDescribed(vals...)
})
})
}
27 changes: 27 additions & 0 deletions completers/glab_completer/cmd/action/label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package action

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

type label struct {
Name string
Description string
}

func ActionLabels(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
//repo := "" // TODO from config or var

var queryResult []label
return actionApi(cmd, "projects/:fullpath/labels", &queryResult, func() carapace.Action {
vals := make([]string, 0, len(queryResult)*2)
for _, label := range queryResult {
vals = append(vals, label.Name, label.Description)
}
return carapace.ActionValuesDescribed(vals...)
//}).Cache(1*time.Hour, cache.String(repo.FullName()))
})
})
}
41 changes: 41 additions & 0 deletions completers/glab_completer/cmd/action/mergeRequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package action

import (
"fmt"
"net/url"
"strconv"

"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

type mergeRequest struct {
Iid int
Title string
}

func ActionMergeRequests(cmd *cobra.Command, state string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
stateQuery := ""
if state != "" {
stateQuery = "&state=" + url.QueryEscape(state)
}

var queryResult []mergeRequest
return actionApi(cmd, fmt.Sprintf("projects/:fullpath/merge_requests?order_by=updated_at&per_page=100%v", stateQuery), queryResult, func() carapace.Action {
vals := make([]string, 0, len(queryResult)*2)
for _, mr := range queryResult {
vals = append(vals, strconv.Itoa(mr.Iid), mr.Title)
}
return carapace.ActionValuesDescribed(vals...)
})
})
}

func ActionMergeRequestsAndBranches(cmd *cobra.Command, state string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
branches := ActionBranches(cmd).Invoke(c)
mergeRequests := ActionMergeRequests(cmd, state).Invoke(c)
return branches.Merge(mergeRequests).Filter(c.Args).ToA()
})
}
24 changes: 24 additions & 0 deletions completers/glab_completer/cmd/action/milestone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package action

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

type milestone struct {
Title string
Description string
}

func ActionMilestones(cmd *cobra.Command) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
var queryResult []milestone
return actionApi(cmd, "projects/:fullpath/milestones", &queryResult, func() carapace.Action {
vals := make([]string, 0, len(queryResult)*2)
for _, milestone := range queryResult {
vals = append(vals, milestone.Title, milestone.Description)
}
return carapace.ActionValuesDescribed(vals...)
})
})
}
Loading

0 comments on commit 92eca68

Please sign in to comment.