git-project-manager/cmd/project_go.go

48 lines
1.1 KiB
Go

package cmd
import (
"fmt"
"os"
"os/exec"
"github.com/spf13/cobra"
)
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) {
project := fzfSearchProjectAliases(searchStringFromArgs(args))
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)
}