git-project-manager/cmd/util_init.go

123 lines
3.5 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"
2023-12-11 15:39:48 +00:00
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/config"
2023-12-07 17:08:56 +00:00
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/gitlab"
2023-12-05 21:56:47 +00:00
"gitlab.sweetwater.com/it/devops/tools/gitlab-project-manager/internal/projects"
"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) {
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
// Backwards-compatible support for singular instance
opts := make([]*gitlab.ClientOpts, 0)
if conf.GitlabHost != "" {
opts = append(opts, &gitlab.ClientOpts{
Ctx: cmd.Context(),
Host: conf.GitlabHost, // deprecated, switch to gitlabs
Token: conf.GitlabToken, // deprecated, switch to gitlabs
Name: conf.GitlabHost, // not originally supported, use the new gitlabs field
})
}
// If defined, load additional instances
for _, g := range conf.Gitlabs {
opts = append(opts, &gitlab.ClientOpts{
Ctx: cmd.Context(),
Name: g.Name,
Host: g.Host,
Token: g.Token,
})
}
// We need at least one GitLab
if len(opts) < 1 {
plog.Error("At least one GitLab must be configured. Add to .gitlabs in your config file")
os.Exit(1)
}
// Load all gitlab configs into clients
var gitlabs *gitlab.Clients
var err error
gitlabs, err = gitlab.NewGitlabClients(opts)
2023-12-07 17:08:56 +00:00
if err != nil {
plog.Error("Failed to create GitLab clients", plog.Args("error", err))
2023-12-07 17:08:56 +00:00
os.Exit(1)
}
2023-12-05 21:56:47 +00:00
cacheOpts := &projects.CacheOpts{
2023-12-09 04:13:17 +00:00
ProjectsPath: conf.ProjectPath,
Path: conf.Cache.File,
TTL: conf.Cache.Ttl,
Logger: plog,
Gitlabs: gitlabs,
2023-12-10 04:19:19 +00:00
Config: &conf,
2023-12-05 21:56:47 +00:00
}
if cache, err = projects.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)
}
2023-12-08 21:52:26 +00:00
if err := cache.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)
}
plog.Debug("Gitlab Clients", plog.Args("gitlabs", cacheOpts.Gitlabs))
2023-12-05 21:56:47 +00:00
}
func postProjectCache(cmd *cobra.Command, args []string) {
cache.Write()
}
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)
}