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"
|
2024-01-14 15:33:15 +00:00
|
|
|
"github.com/spf13/viper"
|
2023-12-09 04:13:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var projectGoCmd = &cobra.Command{
|
|
|
|
Use: "go [fuzzy alias search]",
|
|
|
|
Short: "Go to a GitLab 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)
|
2024-01-14 15:33:15 +00:00
|
|
|
fzfOpts := &fzfProjectOpts{
|
|
|
|
Ctx: cmd.Context(),
|
|
|
|
Search: searchStringFromArgs(args),
|
|
|
|
MustHaveAlias: true,
|
2024-01-17 15:06:20 +00:00
|
|
|
Remotes: remotes,
|
2024-01-14 15:33:15 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
// `cd "$(gitlab-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
|
|
|
}
|