Implement golang project run

This commit is contained in:
Ryan McGuire 2023-12-29 13:35:56 -05:00
parent e1545d114b
commit 03e992bf6b
3 changed files with 58 additions and 12 deletions

View File

@ -1,6 +1,10 @@
package cmd package cmd
import ( import (
"fmt"
"os"
"os/exec"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -18,11 +22,24 @@ func projectRunCmdRun(cmd *cobra.Command, args []string) {
plog.Fatal("No project selected, nothing to open") plog.Fatal("No project selected, nothing to open")
} }
plog.Info("Running Projet", plog.Args("lang", project.Language)) lang := project.GetLanguage()
if project.Language == nil { if lang == nil {
plog.Fatal("GitLab isn't sure what language this project is... can't run.") plog.Fatal("GitLab isn't sure what language this project is... can't run.")
} }
plog.Debug(fmt.Sprintf("Project is written in %s, %.2f%% coverage", lang.Name, lang.Percentage))
switch lang.Name {
case "Go":
cmdArgs := []string{"run", "."} // Run from cwd
cmdArgs = append(cmdArgs, args[1:]...) // Support flags from shell command
cmd := exec.CommandContext(cmd.Context(), "go", cmdArgs...) // Honor parent context
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}
} }
func init() { func init() {

View File

@ -29,3 +29,5 @@ popen () {
gitlab-project-manager project open $1 gitlab-project-manager project open $1
reset 2>&1 > /dev/null reset 2>&1 > /dev/null
} }
alias prun="gitlab-project-manager project run"

View File

@ -31,10 +31,17 @@ type Project struct {
AvatarURL string AvatarURL string
LastActivityAt time.Time LastActivityAt time.Time
Readme string Readme string
Language *string Languages *ProjectLanguages
gitRepo *git.Repository gitRepo *git.Repository
} }
type ProjectLanguages []*ProjectLanguage
type ProjectLanguage struct {
Name string
Percentage float32
}
type User struct { type User struct {
ID int ID int
Username string Username string
@ -61,6 +68,23 @@ func (p *Project) String() string {
return fmt.Sprintf("%s (%s)", p.Path, p.PathWithNamespace) return fmt.Sprintf("%s (%s)", p.Path, p.PathWithNamespace)
} }
func (p *Project) GetLanguage() *ProjectLanguage {
if p.Languages == nil {
return nil
}
var lang *ProjectLanguage
var maxPcnt float32
for _, p := range *p.Languages {
if p.Percentage > maxPcnt {
lang = p
}
maxPcnt = p.Percentage
}
return lang
}
func (p *Project) SanitizedPath() string { func (p *Project) SanitizedPath() string {
return strings.Trim(p.PathWithNamespace, " '\"%<>|`") return strings.Trim(p.PathWithNamespace, " '\"%<>|`")
} }
@ -197,7 +221,7 @@ func (c *Client) handleProjects(projects []*gitlab.Project) []*Project {
AvatarURL: project.AvatarURL, AvatarURL: project.AvatarURL,
LastActivityAt: *project.LastActivityAt, LastActivityAt: *project.LastActivityAt,
Readme: project.ReadmeURL, Readme: project.ReadmeURL,
Language: c.GetProjectLanguage(project), Languages: c.GetProjectLanguages(project),
} }
pList = append(pList, p) pList = append(pList, p)
} }
@ -206,23 +230,26 @@ func (c *Client) handleProjects(projects []*gitlab.Project) []*Project {
// A nil return indicates an API error or GitLab doesn't know what // A nil return indicates an API error or GitLab doesn't know what
// language the project uses. // language the project uses.
func (c *Client) GetProjectLanguage(project *gitlab.Project) *string { func (c *Client) GetProjectLanguages(project *gitlab.Project) *ProjectLanguages {
l, _, e := c.gitlab.Projects.GetProjectLanguages(project.ID, gitlab.WithContext(c.Ctx)) l, _, e := c.gitlab.Projects.GetProjectLanguages(project.ID, gitlab.WithContext(c.Ctx))
if e != nil { if e != nil {
pterm.Error.Printfln("Failed requesting project languages: %s", e.Error()) pterm.Error.Printfln("Failed requesting project languages: %s", e.Error())
return nil return nil
} }
var mainLang *string var pLangs ProjectLanguages
var mostUsed float32 pLangs = make([]*ProjectLanguage, len(*l))
for name, p := range *l {
if p > mostUsed { var i int
mostUsed = p for name, pcnt := range *l {
mainLang = &name pLangs[i] = &ProjectLanguage{
Name: name,
Percentage: pcnt,
} }
i++
} }
return mainLang return &pLangs
} }
func NewGitlabClient(ctx context.Context, host, token string) (*Client, error) { func NewGitlabClient(ctx context.Context, host, token string) (*Client, error) {