git-project-manager/cmd/project_go.go
Ryan McGuire b944af140a Add multi-remote support for GitLab (#1)
Co-authored-by: Ryan D McGuire <ryand_mcguire@sweetwater.com>
Reviewed-on: 50W/git-project-manager#1
2024-01-14 15:33:15 +00:00

56 lines
1.3 KiB
Go

package cmd
import (
"fmt"
"os"
"os/exec"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var projectGoCmd = &cobra.Command{
Use: "go [fuzzy alias search]",
Short: "Go to a GitLab project",
Aliases: []string{"goto", "cd"},
Args: cobra.MaximumNArgs(1),
ArgAliases: []string{"project"},
ValidArgsFunction: validAliasesFunc,
Long: projGoCmdLong,
Run: projectGoCmdRun,
}
func projectGoCmdRun(cmd *cobra.Command, args []string) {
gitlabs := viper.GetStringSlice("project.gitlabs")
fzfOpts := &fzfProjectOpts{
Ctx: cmd.Context(),
Search: searchStringFromArgs(args),
MustHaveAlias: true,
Gitlabs: gitlabs,
}
project := fzfSearchProjectAliases(fzfOpts)
if project == nil {
plog.Fatal("No project selected, nowhere to go")
}
cache.GoTo(project)
project.SetRepo(cache.OpenProject(cmd.Context(), project))
plog.Debug("Project ready", plog.Args(
"path", cache.GetProjectPath(project),
"project", project,
))
fmt.Fprintln(os.Stderr, project.GetGitInfo())
// This should be read by any source command, for instance
// `cd "$(gitlab-project-manager projects cd somealias)"`
fmt.Println(cache.GetProjectPath(project))
exec.Command("cd", cache.GetProjectPath(project)).Run()
}
func init() {
projectCmd.AddCommand(projectGoCmd)
}