2024-03-23 18:52:13 +03:00
|
|
|
/*
|
|
|
|
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
|
|
|
|
*/
|
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-03-31 14:49:40 +03:00
|
|
|
"os"
|
2024-03-23 18:52:13 +03:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
|
|
|
|
"git.unistack.org/unistack-org/pkgdash/internal/source"
|
2024-03-31 14:49:40 +03:00
|
|
|
"git.unistack.org/unistack-org/pkgdash/internal/source/github"
|
2024-03-23 18:52:13 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
yamlcodec "go.unistack.org/micro-codec-yaml/v4"
|
|
|
|
envconfig "go.unistack.org/micro-config-env/v4"
|
|
|
|
fileconfig "go.unistack.org/micro-config-file/v4"
|
|
|
|
"go.unistack.org/micro/v4/config"
|
|
|
|
"go.unistack.org/micro/v4/logger"
|
|
|
|
"go.unistack.org/micro/v4/logger/slog"
|
|
|
|
"go.unistack.org/micro/v4/options"
|
|
|
|
)
|
|
|
|
|
|
|
|
// initCmd represents the init command
|
|
|
|
var initCmd = NewInitCommand()
|
|
|
|
|
2024-03-31 14:49:40 +03:00
|
|
|
var gitsource source.SourceControl = (*github.Github)(nil)
|
2024-03-23 18:52:13 +03:00
|
|
|
|
|
|
|
var cfg = configcli.NewConfig()
|
|
|
|
|
|
|
|
var (
|
|
|
|
DefaultPullRequestTitle = `Bump {{.Name}} from {{.VersionOld}} to {{.VersionNew}}`
|
|
|
|
DefaultPullRequestBody = `Bumps {{.Name}} from {{.VersionOld}} to {{.VersionNew}}`
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
configFiles = []string{
|
|
|
|
"dependabot.yml",
|
|
|
|
"pkgdashcli.yml",
|
|
|
|
"pkgdashcli.yaml",
|
|
|
|
}
|
|
|
|
configDirs = []string{
|
|
|
|
".gitea",
|
|
|
|
".github",
|
|
|
|
".gitlab",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(initCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInitCommand() *cobra.Command {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
logger.DefaultLogger = slog.NewLogger()
|
|
|
|
|
|
|
|
if err := logger.DefaultLogger.Init(logger.WithCallerSkipCount(3), logger.WithLevel(logger.DebugLevel)); err != nil {
|
|
|
|
logger.Error(ctx, fmt.Sprintf("logger init error: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "init",
|
|
|
|
Short: "Init fills the config with data from the configuration file.",
|
|
|
|
Long: `Init fills the config with data from the configuration file.`,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2024-03-31 14:49:40 +03:00
|
|
|
logger.Info(ctx, "Init called")
|
2024-03-23 18:52:13 +03:00
|
|
|
var err error
|
|
|
|
|
2024-03-31 14:49:40 +03:00
|
|
|
lCfg := configcli.NewConfig()
|
|
|
|
|
2024-03-23 18:52:13 +03:00
|
|
|
if err = config.Load(ctx,
|
|
|
|
[]config.Config{
|
|
|
|
config.NewConfig(
|
2024-03-31 14:49:40 +03:00
|
|
|
config.Struct(lCfg),
|
2024-03-23 18:52:13 +03:00
|
|
|
),
|
|
|
|
envconfig.NewConfig(
|
2024-03-31 14:49:40 +03:00
|
|
|
config.Struct(lCfg),
|
2024-03-23 18:52:13 +03:00
|
|
|
),
|
|
|
|
},
|
|
|
|
config.LoadOverride(true),
|
|
|
|
); err != nil {
|
|
|
|
logger.Fatal(ctx, fmt.Sprintf("failed to load config: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, configDir := range configDirs {
|
|
|
|
for _, configFile := range configFiles {
|
2024-03-31 14:49:40 +03:00
|
|
|
path := filepath.Join(configDir, configFile)
|
|
|
|
if _, err = os.Stat(path); os.IsNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-03-23 18:52:13 +03:00
|
|
|
c := fileconfig.NewConfig(
|
|
|
|
config.AllowFail(false),
|
2024-03-31 14:49:40 +03:00
|
|
|
config.Struct(lCfg),
|
2024-03-23 18:52:13 +03:00
|
|
|
options.Codec(yamlcodec.NewCodec()),
|
2024-03-31 14:49:40 +03:00
|
|
|
fileconfig.Path(path),
|
2024-03-23 18:52:13 +03:00
|
|
|
)
|
|
|
|
err = c.Init(options.Context(ctx))
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(ctx, fmt.Sprintf("failed to init config: %v", err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = c.Load(ctx, config.LoadOverride(true)); err != nil {
|
|
|
|
logger.Error(ctx, fmt.Sprintf("failed to load config: %v", err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-31 14:49:40 +03:00
|
|
|
logger.Info(ctx, fmt.Sprintf("Load config... \n %s", cfg))
|
2024-03-23 18:52:13 +03:00
|
|
|
|
|
|
|
if cfg.PullRequestBody == "" {
|
|
|
|
cfg.PullRequestBody = DefaultPullRequestBody
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.PullRequestTitle == "" {
|
|
|
|
cfg.PullRequestTitle = DefaultPullRequestTitle
|
|
|
|
}
|
|
|
|
|
2024-03-31 14:49:40 +03:00
|
|
|
gitsource = source.NewSourceControl(*lCfg)
|
|
|
|
|
|
|
|
logger.Info(ctx, fmt.Sprintf("Git: %s", gitsource.Name()))
|
|
|
|
|
|
|
|
cfg = lCfg
|
2024-03-23 18:52:13 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|