git-project-manager/cmd/project_run.go

55 lines
1.3 KiB
Go

package cmd
import (
"fmt"
"os"
"os/exec"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var projectRunCmd = &cobra.Command{
Use: "run",
Short: "Run the project (e.g. go run .)",
Aliases: []string{"exec", "r"},
Long: projRunCmdLong,
Run: projectRunCmdRun,
}
func projectRunCmdRun(cmd *cobra.Command, args []string) {
remotes := viper.GetStringSlice(FlagRemote)
fzfOpts := &fzfProjectOpts{
Ctx: cmd.Context(),
Search: searchStringFromArgs(args),
Remotes: remotes,
}
project := fzfCwdOrSearchProjectAliases(fzfOpts)
if project == nil {
plog.Fatal("No project selected, nothing to open")
}
lang := project.GetLanguage()
if lang == nil {
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() {
projectCmd.AddCommand(projectRunCmd)
}