git-project-manager/cmd/project_go.go

56 lines
1.4 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) {
remotes := viper.GetStringSlice(FlagRemote)
fzfOpts := &fzfProjectOpts{
Ctx: cmd.Context(),
Search: searchStringFromArgs(args),
MustHaveAlias: true,
Remotes: remotes,
}
project := fzfSearchProjectAliases(fzfOpts)
if project == nil {
plog.Fatal("No project selected, nowhere to go")
}
projectCache.GoTo(project)
project.SetRepo(projectCache.OpenProject(cmd.Context(), project))
plog.Debug("Project ready", plog.Args(
"path", projectCache.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(projectCache.GetProjectPath(project))
exec.Command("cd", projectCache.GetProjectPath(project)).Run()
}
func init() {
projectCmd.AddCommand(projectGoCmd)
}