git-project-manager/cmd/util_init.go

161 lines
4.7 KiB
Go
Raw Normal View History

2023-12-05 21:56:47 +00:00
package cmd
import (
"os"
"os/user"
"path/filepath"
"strings"
"github.com/spf13/cobra"
2024-01-15 21:02:15 +00:00
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/cache"
2023-12-11 15:39:48 +00:00
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
2024-01-15 19:57:15 +00:00
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes"
2024-01-16 18:23:38 +00:00
gitearemote "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/gitea"
2024-01-16 17:48:42 +00:00
gitlabremote "gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/gitlab"
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/info"
2024-01-16 17:48:42 +00:00
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/remotes/remote"
2023-12-05 21:56:47 +00:00
"golang.org/x/sys/unix"
)
// 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
func initProjectCache(cmd *cobra.Command, args []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-16 18:23:38 +00:00
// Load GitLabs
2024-01-16 17:48:42 +00:00
gitRemotes.AddRemotes(getGitLabRemotes(cmd)...)
2023-12-07 17:08:56 +00:00
2024-01-16 18:23:38 +00:00
// Load Giteas
gitRemotes.AddRemotes(getGiteaRemotes(cmd)...)
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-16 17:48:42 +00:00
plog.Debug("Remotes Loaded", plog.Args("remotes", cacheOpts.Remotes))
}
2024-01-16 18:23:38 +00:00
// Loads all configured giteas
func getGiteaRemotes(cmd *cobra.Command) []remote.Remote {
gitRemotes := make([]remote.Remote, 0)
for _, gitea := range conf.Giteas {
2024-01-16 19:26:56 +00:00
if gitea.CloneProto == "" {
gitea.CloneProto = info.DefaultCloneProto
2024-01-16 19:26:56 +00:00
}
giteaRemote, err := gitearemote.NewGiteaRemote(&info.RemoteInfo{
2024-01-16 18:23:38 +00:00
Ctx: cmd.Context(),
Host: gitea.Host,
Name: gitea.Name,
Token: gitea.Token,
Type: "gitea",
2024-01-16 18:23:38 +00:00
CloneProto: gitea.CloneProto,
})
if err != nil {
plog.Error("Failed to prepare Gitea remote", plog.Args("error", err))
} else {
gitRemotes = append(gitRemotes, giteaRemote)
}
}
return gitRemotes
}
// Loads all configured gitlabs, including as defined by legacy
// top-level keys
2024-01-16 17:48:42 +00:00
func getGitLabRemotes(cmd *cobra.Command) []remote.Remote {
gitRemotes := make([]remote.Remote, 0)
// Support legacy keys
if conf.GitlabHost != "" && conf.GitlabToken != "" {
conf.Gitlabs = append(conf.Gitlabs, config.GitlabConfig{
Host: conf.GitlabHost,
Name: conf.GitlabHost,
Token: conf.GitlabToken,
CloneProto: info.CloneProtoSSH,
2024-01-16 17:48:42 +00:00
})
}
// Load Gitlabs
for _, gl := range conf.Gitlabs {
2024-01-16 19:26:56 +00:00
if gl.CloneProto == "" {
gl.CloneProto = info.DefaultCloneProto
2024-01-16 19:26:56 +00:00
}
gitlabRemote, err := gitlabremote.NewGitlabRemote(&info.RemoteInfo{
2024-01-16 17:48:42 +00:00
Ctx: cmd.Context(),
Host: gl.Host,
Name: gl.Name,
Token: gl.Token,
Type: "gitlab",
2024-01-16 17:48:42 +00:00
CloneProto: gl.CloneProto,
})
if err != nil {
plog.Error("Failed to prepare GitLab remote", plog.Args("error", err))
} else {
gitRemotes = append(gitRemotes, gitlabRemote)
}
}
return gitRemotes
2023-12-05 21:56:47 +00:00
}
func postProjectCache(cmd *cobra.Command, args []string) {
2024-01-15 21:02:15 +00:00
projectCache.Write()
2023-12-05 21:56:47 +00:00
}
func initProjectPath(cmd *cobra.Command, args []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
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))
if err := os.MkdirAll(conf.ProjectPath, 0750); 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)
}