git-project-manager/cmd/project/project_go.go

54 lines
1.5 KiB
Go

package project
import (
"fmt"
"os"
"os/exec"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/util"
)
var projectGoCmd = &cobra.Command{
Use: "go [fuzzy alias search]",
Short: "Go to a Git project",
Aliases: []string{"goto", "cd"},
Args: cobra.MaximumNArgs(1),
ArgAliases: []string{"project"},
ValidArgsFunction: util.ValidAliasesFunc,
Long: util.ProjGoCmdLong,
Run: projectGoCmdRun,
}
func projectGoCmdRun(cmd *cobra.Command, args []string) {
remotes := viper.GetStringSlice(util.FlagRemote)
fzfOpts := &util.FzfProjectOpts{
Ctx: cmd.Context(),
Search: utils.SearchStringFromArgs(args),
MustHaveAlias: true,
Remotes: remotes,
}
project := utils.FzfSearchProjectAliases(fzfOpts)
if project == nil {
utils.Logger().Fatal("No project selected, nowhere to go")
}
utils.Cache().GoTo(project)
project.SetRepo(utils.Cache().OpenProject(cmd.Context(), project))
utils.Logger().Debug("Project ready", utils.Logger().Args(
"path", utils.Cache().GetProjectPath(project),
"project", project,
))
fmt.Fprintln(os.Stderr, project.GetGitInfo())
// This should be read by any source command, for instance
// `cd "$(git-project-manager projects cd somealias)"`
fmt.Println(utils.Cache().GetProjectPath(project))
exec.Command("cd", utils.Cache().GetProjectPath(project)).Run()
}