git-project-manager/cmd/project_go.go

56 lines
1.4 KiB
Go
Raw Normal View History

2023-12-09 04:13:17 +00:00
package cmd
import (
2023-12-10 04:19:19 +00:00
"fmt"
"os"
"os/exec"
2023-12-09 04:13:17 +00:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
2023-12-09 04:13:17 +00:00
)
var projectGoCmd = &cobra.Command{
Use: "go [fuzzy alias search]",
2024-12-27 22:42:44 +00:00
Short: "Go to a Git project",
2023-12-10 04:19:19 +00:00
Aliases: []string{"goto", "cd"},
2023-12-09 04:13:17 +00:00
Args: cobra.MaximumNArgs(1),
ArgAliases: []string{"project"},
ValidArgsFunction: validAliasesFunc,
Long: projGoCmdLong,
2023-12-10 04:19:19 +00:00
Run: projectGoCmdRun,
2023-12-09 04:13:17 +00:00
}
func projectGoCmdRun(cmd *cobra.Command, args []string) {
2024-10-01 18:29:14 +00:00
remotes := viper.GetStringSlice(FlagRemote)
fzfOpts := &fzfProjectOpts{
Ctx: cmd.Context(),
Search: searchStringFromArgs(args),
MustHaveAlias: true,
Remotes: remotes,
}
project := fzfSearchProjectAliases(fzfOpts)
2023-12-10 04:45:30 +00:00
if project == nil {
plog.Fatal("No project selected, nowhere to go")
}
2024-01-15 21:02:15 +00:00
projectCache.GoTo(project)
project.SetRepo(projectCache.OpenProject(cmd.Context(), project))
2023-12-10 04:19:19 +00:00
plog.Debug("Project ready", plog.Args(
2024-01-15 21:02:15 +00:00
"path", projectCache.GetProjectPath(project),
2023-12-10 04:19:19 +00:00
"project", project,
))
fmt.Fprintln(os.Stderr, project.GetGitInfo())
// This should be read by any source command, for instance
2024-12-27 22:42:44 +00:00
// `cd "$(git-project-manager projects cd somealias)"`
2024-01-15 21:02:15 +00:00
fmt.Println(projectCache.GetProjectPath(project))
exec.Command("cd", projectCache.GetProjectPath(project)).Run()
2023-12-09 04:13:17 +00:00
}
2023-12-10 04:19:19 +00:00
func init() {
projectCmd.AddCommand(projectGoCmd)
2023-12-09 04:13:17 +00:00
}