git-project-manager/cmd/util_init.go
Ryan McGuire c78c41f567
All checks were successful
Build and Publish / release (push) Successful in 1m10s
Rename project to git-project-manager
2024-12-27 17:42:44 -05:00

144 lines
4.4 KiB
Go

// 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
package cmd
import (
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"golang.org/x/sys/unix"
"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"
)
func initProjectCache(cmd *cobra.Command, _ []string) {
var err error
plog.Debug("Running pre-run for cacheCmd")
conf.Cache.File = conf.ProjectPath + "/.cache.yaml"
gitRemotes := remotes.NewRemotes()
gitRemotes.AddRemotes(*getRemotes(cmd)...)
cacheOpts := &cache.CacheOpts{
ProjectsPath: conf.ProjectPath,
Path: conf.Cache.File,
TTL: conf.Cache.Ttl,
Logger: plog,
Remotes: gitRemotes,
Config: &conf,
}
if projectCache, err = cache.NewProjectCache(cacheOpts); err != nil {
plog.Error("Failed to prepare project cache", plog.Args("error", err))
os.Exit(1)
}
if err := projectCache.Read(); err != nil {
plog.Error("Cache load failed", plog.Args("error", err))
os.Exit(1)
}
plog.Debug("Remotes Loaded", plog.Args("remotes", cacheOpts.Remotes))
}
// Generically loads remotes from info.RemoteInfo in config.Remotes
func getRemotes(cmd *cobra.Command) *remotes.Remotes {
gitRemotes := new(remotes.Remotes)
*gitRemotes = make([]remote.Remote, 0)
for _, r := range conf.Remotes {
// Create a copy, set context
gitRemoteInfo := r
gitRemoteInfo.SetContext(cmd.Context())
var gitRemote remote.Remote
var err error
switch r.Type {
case "gitlab":
gitRemote, err = gitlabremote.NewGitlabRemote(&gitRemoteInfo)
case "gitea":
gitRemote, err = gitearemote.NewGiteaRemote(&gitRemoteInfo)
case "github":
gitRemote, err = githubremote.NewGithubRemote(&gitRemoteInfo)
}
if err != nil {
plog.Error("Failed to prepare remote", plog.Args(
"error", err,
"type", r.Type))
} else {
*gitRemotes = append(*gitRemotes, gitRemote)
}
}
return gitRemotes
}
func postProjectCache(_ *cobra.Command, _ []string) {
projectCache.Write()
}
func initProjectPath(_ *cobra.Command, _ []string) {
plog.Debug("Running persistent pre-run for rootCmd")
var err error
if conf.ProjectPath == "" {
conf.ProjectPath = config.DefaultConfig.ProjectPath
return
}
if conf.ProjectPath, err = resolvePath(conf.ProjectPath); err != nil {
plog.Error("Failed to determine project path", plog.Args("error", err))
os.Exit(1)
}
_, err = os.Stat(conf.ProjectPath)
if err != nil {
plog.Error("Failed to stat project path, trying to create", plog.Args("error", err))
if err = os.MkdirAll(conf.ProjectPath, 0o750); err != nil {
plog.Error("Failed to create project path", plog.Args("error", err))
os.Exit(1)
}
plog.Info("Project path created", plog.Args("path", conf.ProjectPath))
} else {
if err = unix.Access(conf.ProjectPath, unix.W_OK); err != nil {
plog.Error("Unable to write to project path", plog.Args(
"path", conf.ProjectPath,
"error", err))
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)
}
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
}