#8 delete cobra, add parsing .netrc

This commit is contained in:
Gorbunov Kirill Andreevich
2024-03-31 14:49:40 +03:00
parent 8a85fce0a9
commit 55c0bdb49a
13 changed files with 221 additions and 92 deletions

View File

@@ -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]

View File

@@ -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()
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
},

View File

@@ -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
},
}

View File

@@ -9,11 +9,11 @@ type Config struct {
}
type Source struct {
TypeGit string `json:"type" yaml:"type"`
Token string `json:"token" yaml:"token"`
APIURL string `json:"apiurl" yaml:"apiurl"`
Repository string `json:"repository" yaml:"repository"`
Owner string `json:"owner" yaml:"owner"`
TypeGit string `json:"type" yaml:"type" env:"GIT_TYPE"`
Token string `json:"token" yaml:"token" env:"GIT_TOKEN"`
APIURL string `json:"apiurl" yaml:"apiurl" env:"GIT_API"`
Repository string `json:"repository" yaml:"repository" env:"GIT_REPO"`
Owner string `json:"owner" yaml:"owner" env:"GIT_OWNER"`
}
type UpdateOpt struct {
@@ -23,8 +23,14 @@ type UpdateOpt struct {
Cached bool `json:"cached" yaml:"cached" default:"true"`
}
type Cli struct {
Method string `flag:"name=method,desc='choice method(update, close, checkupdaue, list)',default='checkupdate'"`
Path string `flag:"name=path,desc='title of mod',default=''"`
}
func NewConfig() *Config {
return &Config{
Source: &Source{},
Source: &Source{},
UpdateOpt: &UpdateOpt{},
}
}

View File

@@ -59,6 +59,10 @@ type giteaPull struct {
ID int64 `json:"id"`
}
func (g *Gitea) Name() string {
return "gitea"
}
func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
logger.Debugf(ctx, fmt.Sprintf("RequestOpen start, mod title: %s", path))
@@ -256,7 +260,7 @@ func (g *Gitea) RequestOpen(ctx context.Context, branch string, path string, mod
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
fmt.Sprintf("%s/repos/%s/%s/pulls?token=%s", g.URL, g.Owner, g.Repository, g.Token),
fmt.Sprintf("https://%s/api/v1/repos/%s/%s/pulls?token=%s", g.URL, g.Owner, g.Repository, g.Token),
bytes.NewReader(buf),
)
if err != nil {
@@ -371,6 +375,9 @@ func (g *Gitea) RequestList(ctx context.Context, branch string) (map[string]stri
rMap := make(map[string]string)
for _, pull := range gPulls {
if !strings.HasPrefix(pull.Title, "Bump ") { //добавляем только реквесты бота по обновлению модулей
continue
}
path = strings.Split(pull.Title, " ")[1] //todo Работет только для дефолтного шаблона
rMap[path] = pull.Title
}
@@ -388,7 +395,7 @@ func getVersions(s string) string {
func DeleteBranch(ctx context.Context, url, owner, repo, branch, token string) (*http.Request, error) {
var buf []byte
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("%s/repos/%s/%s/branches/%s?token=%s", url, owner, repo, branch, token), bytes.NewReader(buf))
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, fmt.Sprintf("https://%s/api/v1/repos/%s/%s/branches/%s?token=%s", url, owner, repo, branch, token), bytes.NewReader(buf))
if err != nil {
return nil, err
}
@@ -404,7 +411,7 @@ func GetPulls(ctx context.Context, url, owner, repo, token string) ([]*giteaPull
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fmt.Sprintf("%s/repos/%s/%s/pulls?state=open&token=%s", url, owner, repo, token),
fmt.Sprintf("https://%s/api/v1//repos/%s/%s/pulls?state=open&token=%s", url, owner, repo, token),
nil)
if err != nil {
return nil, err

View File

@@ -2,6 +2,7 @@ package github
import (
"context"
"fmt"
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
"git.unistack.org/unistack-org/pkgdash/internal/modules"
@@ -17,15 +18,19 @@ func NewGithub(cfg configcli.Config) *Github {
}
}
func (g *Github) Name() string {
return "github"
}
func (g *Github) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Github) RequestClose(ctx context.Context, branch string, path string) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Github) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Github) RequestList(ctx context.Context, branch string) (map[string]string, error) {
return nil, nil
return nil, fmt.Errorf("implement me")
}

View File

@@ -2,6 +2,7 @@ package gitlab
import (
"context"
"fmt"
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
"git.unistack.org/unistack-org/pkgdash/internal/modules"
@@ -17,15 +18,19 @@ func NewGitlab(cfg configcli.Config) *Gitlab {
}
}
func (g *Gitlab) Name() string {
return "gitlab"
}
func (g *Gitlab) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gitlab) RequestClose(ctx context.Context, branch string, path string) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gitlab) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gitlab) RequestList(ctx context.Context, branch string) (map[string]string, error) {
return nil, nil
return nil, fmt.Errorf("implement me")
}

View File

@@ -2,6 +2,7 @@ package gogs
import (
"context"
"fmt"
"git.unistack.org/unistack-org/pkgdash/internal/configcli"
"git.unistack.org/unistack-org/pkgdash/internal/modules"
@@ -17,15 +18,19 @@ func NewGogs(cfg configcli.Config) *Gogs {
}
}
func (g *Gogs) Name() string {
return "gogs"
}
func (g *Gogs) RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gogs) RequestClose(ctx context.Context, branch string, path string) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gogs) RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error {
return nil
return fmt.Errorf("implement me")
}
func (g *Gogs) RequestList(ctx context.Context, branch string) (map[string]string, error) {
return nil, nil
return nil, fmt.Errorf("implement me")
}

View File

@@ -12,6 +12,7 @@ import (
)
type SourceControl interface {
Name() string
RequestOpen(ctx context.Context, branch string, path string, mod modules.Update) error
RequestClose(ctx context.Context, branch string, path string) error
RequestUpdate(ctx context.Context, branch string, path string, mod modules.Update) error