2024-12-27 22:42:44 +00:00
|
|
|
// This file contains init methods that may be used by
|
|
|
|
// multiple sub-commands. For instance, the cach and projects
|
|
|
|
// sub-commands both depend on a cache and may both call the initProjectCache
|
|
|
|
// func from their PersistentPreRun commands
|
2023-12-05 21:56:47 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-12-27 22:42:44 +00:00
|
|
|
"fmt"
|
2023-12-05 21:56:47 +00:00
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2024-12-27 22:42:44 +00:00
|
|
|
"github.com/pterm/pterm"
|
2023-12-05 21:56:47 +00:00
|
|
|
"github.com/spf13/cobra"
|
2024-12-27 22:42:44 +00:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2024-12-19 19:55:49 +00:00
|
|
|
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/cache"
|
|
|
|
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/config"
|
|
|
|
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes"
|
|
|
|
gitearemote "gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/gitea"
|
|
|
|
githubremote "gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/github"
|
|
|
|
gitlabremote "gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/gitlab"
|
|
|
|
"gitea.libretechconsulting.com/rmcguire/git-project-manager/internal/remotes/remote"
|
2023-12-05 21:56:47 +00:00
|
|
|
)
|
|
|
|
|
2024-12-27 22:42:44 +00:00
|
|
|
func initProjectCache(cmd *cobra.Command, _ []string) {
|
2024-01-16 17:48:42 +00:00
|
|
|
var err error
|
2023-12-08 21:52:26 +00:00
|
|
|
plog.Debug("Running pre-run for cacheCmd")
|
|
|
|
conf.Cache.File = conf.ProjectPath + "/.cache.yaml"
|
2023-12-05 21:56:47 +00:00
|
|
|
|
2024-01-16 17:48:42 +00:00
|
|
|
gitRemotes := remotes.NewRemotes()
|
2024-01-17 19:10:26 +00:00
|
|
|
gitRemotes.AddRemotes(*getRemotes(cmd)...)
|
2024-01-16 18:23:38 +00:00
|
|
|
|
2024-01-15 21:02:15 +00:00
|
|
|
cacheOpts := &cache.CacheOpts{
|
2023-12-09 04:13:17 +00:00
|
|
|
ProjectsPath: conf.ProjectPath,
|
|
|
|
Path: conf.Cache.File,
|
|
|
|
TTL: conf.Cache.Ttl,
|
|
|
|
Logger: plog,
|
2024-01-16 17:48:42 +00:00
|
|
|
Remotes: gitRemotes,
|
2023-12-10 04:19:19 +00:00
|
|
|
Config: &conf,
|
2023-12-05 21:56:47 +00:00
|
|
|
}
|
2024-01-15 21:02:15 +00:00
|
|
|
if projectCache, err = cache.NewProjectCache(cacheOpts); err != nil {
|
2023-12-07 17:08:56 +00:00
|
|
|
plog.Error("Failed to prepare project cache", plog.Args("error", err))
|
2023-12-05 21:56:47 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-01-15 21:02:15 +00:00
|
|
|
if err := projectCache.Read(); err != nil {
|
2023-12-07 17:08:56 +00:00
|
|
|
plog.Error("Cache load failed", plog.Args("error", err))
|
2023-12-05 21:56:47 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-01-14 15:33:15 +00:00
|
|
|
|
2024-01-16 17:48:42 +00:00
|
|
|
plog.Debug("Remotes Loaded", plog.Args("remotes", cacheOpts.Remotes))
|
|
|
|
}
|
|
|
|
|
2024-01-17 15:06:20 +00:00
|
|
|
// Generically loads remotes from info.RemoteInfo in config.Remotes
|
2024-01-17 19:10:26 +00:00
|
|
|
func getRemotes(cmd *cobra.Command) *remotes.Remotes {
|
|
|
|
gitRemotes := new(remotes.Remotes)
|
|
|
|
*gitRemotes = make([]remote.Remote, 0)
|
2024-01-17 15:06:20 +00:00
|
|
|
for _, r := range conf.Remotes {
|
2024-01-17 19:10:26 +00:00
|
|
|
// Create a copy, set context
|
|
|
|
gitRemoteInfo := r
|
2024-01-17 21:56:41 +00:00
|
|
|
gitRemoteInfo.SetContext(cmd.Context())
|
2024-01-17 15:06:20 +00:00
|
|
|
var gitRemote remote.Remote
|
|
|
|
var err error
|
|
|
|
switch r.Type {
|
|
|
|
case "gitlab":
|
2024-01-17 19:10:26 +00:00
|
|
|
gitRemote, err = gitlabremote.NewGitlabRemote(&gitRemoteInfo)
|
2024-01-17 15:06:20 +00:00
|
|
|
case "gitea":
|
2024-01-17 19:10:26 +00:00
|
|
|
gitRemote, err = gitearemote.NewGiteaRemote(&gitRemoteInfo)
|
2024-01-18 16:09:54 +00:00
|
|
|
case "github":
|
|
|
|
gitRemote, err = githubremote.NewGithubRemote(&gitRemoteInfo)
|
2024-01-16 19:26:56 +00:00
|
|
|
}
|
2024-01-16 18:23:38 +00:00
|
|
|
if err != nil {
|
2024-01-17 15:06:20 +00:00
|
|
|
plog.Error("Failed to prepare remote", plog.Args(
|
|
|
|
"error", err,
|
|
|
|
"type", r.Type))
|
2024-01-16 18:23:38 +00:00
|
|
|
} else {
|
2024-01-17 19:10:26 +00:00
|
|
|
*gitRemotes = append(*gitRemotes, gitRemote)
|
2024-01-16 18:23:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return gitRemotes
|
|
|
|
}
|
|
|
|
|
2024-12-27 22:42:44 +00:00
|
|
|
func postProjectCache(_ *cobra.Command, _ []string) {
|
2024-01-15 21:02:15 +00:00
|
|
|
projectCache.Write()
|
2023-12-05 21:56:47 +00:00
|
|
|
}
|
|
|
|
|
2024-12-27 22:42:44 +00:00
|
|
|
func initProjectPath(_ *cobra.Command, _ []string) {
|
2023-12-07 17:08:56 +00:00
|
|
|
plog.Debug("Running persistent pre-run for rootCmd")
|
2023-12-05 21:56:47 +00:00
|
|
|
var err error
|
2023-12-11 15:39:48 +00:00
|
|
|
if conf.ProjectPath == "" {
|
|
|
|
conf.ProjectPath = config.DefaultConfig.ProjectPath
|
2023-12-19 17:16:45 +00:00
|
|
|
return
|
2023-12-11 15:39:48 +00:00
|
|
|
}
|
2023-12-05 21:56:47 +00:00
|
|
|
if conf.ProjectPath, err = resolvePath(conf.ProjectPath); err != nil {
|
2023-12-07 17:08:56 +00:00
|
|
|
plog.Error("Failed to determine project path", plog.Args("error", err))
|
2023-12-05 21:56:47 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
_, err = os.Stat(conf.ProjectPath)
|
|
|
|
if err != nil {
|
2023-12-07 17:08:56 +00:00
|
|
|
plog.Error("Failed to stat project path, trying to create", plog.Args("error", err))
|
2024-12-27 22:42:44 +00:00
|
|
|
if err = os.MkdirAll(conf.ProjectPath, 0o750); err != nil {
|
2023-12-07 17:08:56 +00:00
|
|
|
plog.Error("Failed to create project path", plog.Args("error", err))
|
2023-12-05 21:56:47 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-12-07 17:08:56 +00:00
|
|
|
plog.Info("Project path created", plog.Args("path", conf.ProjectPath))
|
2023-12-05 21:56:47 +00:00
|
|
|
} else {
|
|
|
|
if err = unix.Access(conf.ProjectPath, unix.W_OK); err != nil {
|
2023-12-07 17:08:56 +00:00
|
|
|
plog.Error("Unable to write to project path", plog.Args(
|
2023-12-05 21:56:47 +00:00
|
|
|
"path", conf.ProjectPath,
|
2023-12-07 17:08:56 +00:00
|
|
|
"error", err))
|
2023-12-05 21:56:47 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resolvePath(path string) (string, error) {
|
|
|
|
if strings.HasPrefix(path, "~/") {
|
|
|
|
usr, _ := user.Current()
|
|
|
|
path = filepath.Join(usr.HomeDir, path[2:])
|
|
|
|
}
|
|
|
|
return filepath.Abs(path)
|
|
|
|
}
|
2024-12-27 22:42:44 +00:00
|
|
|
|
|
|
|
func getConfigName(configPath string) string {
|
|
|
|
// Check existing config
|
|
|
|
for _, ext := range []string{"yml", "yaml"} {
|
|
|
|
configFile := fmt.Sprintf("%s/%s.%s", configPath, configName, ext)
|
|
|
|
legacyConfigFile := fmt.Sprintf("%s/%s.%s", configPath, legacyConfigName, ext)
|
|
|
|
|
|
|
|
if _, err := os.Stat(configFile); err == nil {
|
|
|
|
return configName
|
|
|
|
} else if _, err := os.Stat(legacyConfigFile); err == nil {
|
|
|
|
pterm.DefaultLogger.WithWriter(os.Stderr).
|
|
|
|
Warn(fmt.Sprintf("using legacy config path, suggest using %s/%s.yaml", configPath, configName))
|
|
|
|
return legacyConfigName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing found, do what we want
|
|
|
|
return configName
|
|
|
|
}
|