From 4fc2cb0a5f89f069684f4f01a4a7d8f459d98117 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Sun, 31 Mar 2024 14:49:40 +0300 Subject: [PATCH] #8 delete cobra, add parsing .netrc --- cmd/pkgdashcli/pkgdashcli.go | 7 ------- go.mod | 2 ++ go.sum | 2 ++ internal/cli/checkupdate.go | 3 ++- internal/cli/init.go | 30 +++++++++++++++++++++--------- internal/cli/list.go | 10 +++++++--- 6 files changed, 34 insertions(+), 20 deletions(-) delete mode 100644 cmd/pkgdashcli/pkgdashcli.go diff --git a/cmd/pkgdashcli/pkgdashcli.go b/cmd/pkgdashcli/pkgdashcli.go deleted file mode 100644 index e080b7c..0000000 --- a/cmd/pkgdashcli/pkgdashcli.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "git.unistack.org/unistack-org/pkgdash/internal/cli" - -func main() { - cli.Execute() -} diff --git a/go.mod b/go.mod index 6beb75c..a1e5fc9 100644 --- a/go.mod +++ b/go.mod @@ -35,8 +35,10 @@ require ( require ( github.com/google/go-cmp v0.6.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/jdx/go-netrc v1.0.0 // indirect github.com/silas/dag v0.0.0-20220518035006-a7e85ada93c5 // indirect github.com/spf13/pflag v1.0.5 // indirect + go.unistack.org/micro-config-flag/v4 v4.0.4 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240311132316-a219d84964c2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index f943bad..0678c75 100644 --- a/go.sum +++ b/go.sum @@ -686,6 +686,8 @@ github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dv github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= +github.com/jdx/go-netrc v1.0.0 h1:QbLMLyCZGj0NA8glAhxUpf1zDg6cxnWgMBbjq40W0gQ= +github.com/jdx/go-netrc v1.0.0/go.mod h1:Gh9eFQJnoTNIRHXl2j5bJXA1u84hQWJWgGh569zF3v8= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= diff --git a/internal/cli/checkupdate.go b/internal/cli/checkupdate.go index 1fe8e2a..8ae1d80 100644 --- a/internal/cli/checkupdate.go +++ b/internal/cli/checkupdate.go @@ -33,7 +33,8 @@ func NewCheckUpdateCommand() *cobra.Command { Short: "CheckUpdate collects a list of dependencies with the latest updates.", Long: `CheckUpdate collects a list of dependencies with the latest updates.`, RunE: func(cmd *cobra.Command, args []string) error { - fmt.Println("checkupdate called") + logger.Info(ctx, "CheckUpdate called") + path := "." if len(os.Args) > 1 { path = os.Args[1] diff --git a/internal/cli/init.go b/internal/cli/init.go index 0a6af9d..2b9fc6d 100644 --- a/internal/cli/init.go +++ b/internal/cli/init.go @@ -6,10 +6,12 @@ package cli import ( "context" "fmt" + "os" "path/filepath" "git.unistack.org/unistack-org/pkgdash/internal/configcli" "git.unistack.org/unistack-org/pkgdash/internal/source" + "git.unistack.org/unistack-org/pkgdash/internal/source/github" "github.com/spf13/cobra" yamlcodec "go.unistack.org/micro-codec-yaml/v4" envconfig "go.unistack.org/micro-config-env/v4" @@ -23,7 +25,7 @@ import ( // initCmd represents the init command var initCmd = NewInitCommand() -var gitsource = source.SourceControl(nil) +var gitsource source.SourceControl = (*github.Github)(nil) var cfg = configcli.NewConfig() @@ -63,16 +65,18 @@ func NewInitCommand() *cobra.Command { 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 { - fmt.Println("init called") + logger.Info(ctx, "Init called") var err error + lCfg := configcli.NewConfig() + if err = config.Load(ctx, []config.Config{ config.NewConfig( - config.Struct(cfg), + config.Struct(lCfg), ), envconfig.NewConfig( - config.Struct(cfg), + config.Struct(lCfg), ), }, config.LoadOverride(true), @@ -82,12 +86,16 @@ func NewInitCommand() *cobra.Command { for _, configDir := range configDirs { for _, configFile := range configFiles { - logger.Info(ctx, fmt.Sprintf("path: %s", filepath.Join(configDir, configFile))) + path := filepath.Join(configDir, configFile) + if _, err = os.Stat(path); os.IsNotExist(err) { + continue + } + c := fileconfig.NewConfig( config.AllowFail(false), - config.Struct(cfg), + config.Struct(lCfg), options.Codec(yamlcodec.NewCodec()), - fileconfig.Path(".gitea/pkgdashcli.yaml"), + fileconfig.Path(path), ) err = c.Init(options.Context(ctx)) if err != nil { @@ -101,7 +109,7 @@ func NewInitCommand() *cobra.Command { } } - logger.Info(ctx, fmt.Sprintf("Load config... %s", cfg.Source.Repository)) + logger.Info(ctx, fmt.Sprintf("Load config... \n %s", cfg)) if cfg.PullRequestBody == "" { cfg.PullRequestBody = DefaultPullRequestBody @@ -111,7 +119,11 @@ func NewInitCommand() *cobra.Command { cfg.PullRequestTitle = DefaultPullRequestTitle } - gitsource = source.NewSourceControl(*cfg) + gitsource = source.NewSourceControl(*lCfg) + + logger.Info(ctx, fmt.Sprintf("Git: %s", gitsource.Name())) + + cfg = lCfg return nil }, diff --git a/internal/cli/list.go b/internal/cli/list.go index a440079..b8c6536 100644 --- a/internal/cli/list.go +++ b/internal/cli/list.go @@ -35,8 +35,6 @@ func init() { func NewListCommand() *cobra.Command { ctx := context.Background() - logger.Info(ctx, "RequestList start") - cmd := &cobra.Command{ Use: "list", Short: "Update allows you to start the process of creating a PR in the repository", @@ -45,6 +43,9 @@ Update allows you to start the process of creating a PR in the repository. Use the -a flag to update all dependencies or -m flag a specific module. `, RunE: func(cmd *cobra.Command, args []string) error { + + logger.Info(ctx, "RequestList start") + if gitsource == nil { return errSourceNil } @@ -54,8 +55,9 @@ Use the -a flag to update all dependencies or -m flag a specific module. } prList = make(map[string]map[string]string) - + logger.Info(ctx, fmt.Sprintf("Load config... \n %s", cfg)) for _, branch := range cfg.Branches { + logger.Info(ctx, fmt.Sprintf("Start getting pr for %s", branch)) rMap, err := gitsource.RequestList(ctx, branch) if err != nil { return err @@ -65,6 +67,8 @@ Use the -a flag to update all dependencies or -m flag a specific module. logger.Info(ctx, fmt.Sprintf("for %s:\n%s", branch, rMap)) } + logger.Info(ctx, "Successful getting pull requests") + return nil }, }