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

54 lines
1.5 KiB
Go
Raw Normal View History

2024-12-30 20:50:31 +00:00
package project
2023-12-09 04:13:17 +00:00
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"
2024-12-30 20:50:31 +00:00
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/util"
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"},
2024-12-30 20:50:31 +00:00
ValidArgsFunction: util.ValidAliasesFunc,
Long: util.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-12-30 20:50:31 +00:00
remotes := viper.GetStringSlice(util.FlagRemote)
fzfOpts := &util.FzfProjectOpts{
Ctx: cmd.Context(),
2024-12-30 20:50:31 +00:00
Search: utils.SearchStringFromArgs(args),
MustHaveAlias: true,
Remotes: remotes,
}
2024-12-30 20:50:31 +00:00
project := utils.FzfSearchProjectAliases(fzfOpts)
2023-12-10 04:45:30 +00:00
if project == nil {
2024-12-30 20:50:31 +00:00
utils.Logger().Fatal("No project selected, nowhere to go")
2023-12-10 04:45:30 +00:00
}
2024-12-30 20:50:31 +00:00
utils.Cache().GoTo(project)
project.SetRepo(utils.Cache().OpenProject(cmd.Context(), project))
2023-12-10 04:19:19 +00:00
2024-12-30 20:50:31 +00:00
utils.Logger().Debug("Project ready", utils.Logger().Args(
"path", utils.Cache().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-12-30 20:50:31 +00:00
fmt.Println(utils.Cache().GetProjectPath(project))
exec.Command("cd", utils.Cache().GetProjectPath(project)).Run()
2023-12-09 04:13:17 +00:00
}