Skip to content

Commit

Permalink
implement compose flag
Browse files Browse the repository at this point in the history
  • Loading branch information
pPrecel committed Nov 19, 2023
1 parent c81b8ff commit 6cad620
Show file tree
Hide file tree
Showing 15 changed files with 453 additions and 9 deletions.
90 changes: 90 additions & 0 deletions cmd/compose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package cmd

import (
"fmt"
"path/filepath"
"time"

"github.com/pPrecel/PKUP/internal/logo"
"github.com/pPrecel/PKUP/pkg/compose"
"github.com/pPrecel/PKUP/pkg/period"
"github.com/pPrecel/PKUP/pkg/report"
"github.com/urfave/cli/v2"
)

func NewComposeCommand(opts *Options) *cli.Command {
since, until := period.GetCurrentPKUP()
actionsOpts := composeActionOpts{
Options: opts,
since: *cli.NewTimestamp(since),
until: *cli.NewTimestamp(until),
}

return &cli.Command{
Name: "compose",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
DefaultText: ".pkupcompose.yaml",
Destination: &actionsOpts.config,
Action: func(_ *cli.Context, path string) error {
path, err := filepath.Abs(path)
if err != nil {
return err
}

actionsOpts.config = path
return nil
},
},
&cli.TimestampFlag{
Name: "since",
Usage: "timestamp used to get commits and render report - foramt " + report.PeriodFormat,
Layout: report.PeriodFormat,
Timezone: time.Local,
Action: func(_ *cli.Context, time *time.Time) error {
actionsOpts.since.SetTimestamp(*time)
return nil
},
},
&cli.TimestampFlag{
Name: "until",
Usage: "timestamp used to get commits and render report - foramt " + report.PeriodFormat,
Layout: report.PeriodFormat,
Timezone: time.Local,
Action: func(_ *cli.Context, t *time.Time) error {
actionsOpts.until.SetTimestamp(t.Add(time.Hour*24 - time.Second))
return nil
},
},
&cli.BoolFlag{
Name: "ci",
Usage: "print output using standard log",
Category: loggingCategory,
Destination: &actionsOpts.ci,
},
},
Before: func(_ *cli.Context) error {
// print logo before any action
fmt.Printf("%s\n\n", logo.Build(opts.BuildVersion))

return nil
},
Action: func(ctx *cli.Context) error {
return composeCommandAction(ctx, &actionsOpts)
},
}
}

func composeCommandAction(ctx *cli.Context, opts *composeActionOpts) error {
cfg, err := compose.ReadConfig(opts.config)
if err != nil {
return fmt.Errorf("failed to read config from path '%s': %s", opts.config, err.Error())
}

return compose.New(ctx.Context, opts.Log).ForConfig(cfg, compose.ComposeOpts{
Since: *opts.since.Value(),
Until: *opts.until.Value(),
Ci: opts.ci,
})
}
4 changes: 1 addition & 3 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ func NewGenCommand(opts *Options) *cli.Command {
Flags: getGenFlags(actionsOpts),
Before: func(_ *cli.Context) error {
// print logo before any action
if !actionsOpts.ci {
fmt.Printf("%s\n\n", logo.Build(opts.BuildVersion))
}
fmt.Printf("%s\n\n", logo.Build(opts.BuildVersion))

// default
if err := actionsOpts.setDefaults(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/gen_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func getGenFlags(opts *genActionOpts) []cli.Flag {
&cli.StringSliceFlag{
Name: "org",
Usage: "<org> slice - use this flag to look for user activity in all organization repos",
Action: func(ctx *cli.Context, s []string) error {
Action: func(_ *cli.Context, s []string) error {
opts.orgs = s
return nil
},
Expand Down
9 changes: 9 additions & 0 deletions cmd/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ type Options struct {
Log *pterm.Logger
}

type composeActionOpts struct {
*Options

config string
since cli.Timestamp
until cli.Timestamp
ci bool
}

type genActionOpts struct {
*Options

Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ require (
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
)

require github.com/hashicorp/errwrap v1.0.0 // indirect

require (
atomicgo.dev/cursor v0.2.0 // indirect
atomicgo.dev/keyboard v0.2.9 // indirect
Expand All @@ -29,6 +31,7 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/hashicorp/go-multierror v1.1.1
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -45,5 +48,5 @@ require (
golang.org/x/text v0.12.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gopkg.in/yaml.v3 v3.0.1
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ github.com/gookit/color v1.4.2/go.mod h1:fqRyamkC1W8uxl+lxCQxOT09l/vYfZ+QeiX3rKQ
github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo=
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.10/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
Expand Down
2 changes: 1 addition & 1 deletion internal/view/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func selectChannelsForSpinners(workingSpinners map[string]*pterm.SpinnerPrinter,
} else {
text := buildTreeString(
fmt.Sprintf(
"found %d commits for repo '%s'",
"found %d commits for '%s'",
len(commitList.Commits), taskName),
commitsToStringList(commitList),
)
Expand Down
2 changes: 1 addition & 1 deletion internal/view/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func selectChannelsForLogger(log *pterm.Logger, taskName string, channels taskCh
fmt.Sprintf("skipping '%s' no user activity detected", taskName),
)
} else {
text := fmt.Sprintf("found %d commits for repo '%s'", len(commitList.Commits), taskName)
text := fmt.Sprintf("found %d commits for '%s'", len(commitList.Commits), taskName)
log.Info(text, log.Args("commits", commitsToStringList(commitList)))
}
}
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func main() {
},
Commands: []*cli.Command{
cmd.NewGenCommand(opts),
cmd.NewComposeCommand(opts),
cmd.NewVersionCommand(opts),
},
}
Expand Down
33 changes: 33 additions & 0 deletions pkg/compose/authors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package compose

import (
"fmt"

"github.com/pPrecel/PKUP/pkg/github"
)

func buildAuthors(remoteClients map[string]github.Client, user User) (map[string][]string, error) {
authorsMap := map[string][]string{}

// get signatures for opensource if not empty
if user.Username != "" {
signatures, err := remoteClients[""].GetUserSignatures(user.Username)
if err != nil {
return nil, fmt.Errorf("failed to list user signatures for opensource: %s", err.Error())
}

authorsMap[""] = signatures
}

// get signatures for every enterprise
for url, username := range user.EnterpriseUsernames {
signatures, err := remoteClients[url].GetUserSignatures(username)
if err != nil {
return nil, fmt.Errorf("failed to list user signatures for '%s': %s", url, err.Error())
}

authorsMap[url] = signatures
}

return authorsMap, nil
}
44 changes: 44 additions & 0 deletions pkg/compose/clients.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package compose

import (
"context"
"fmt"

"github.com/pPrecel/PKUP/pkg/github"
"github.com/pterm/pterm"
)

func buildClients(ctx context.Context, logger *pterm.Logger, config *Config, buildClient buildClientFunc) (map[string]github.Client, error) {
urlClients := map[string]github.Client{}

var err error
urlClients, err = appendRemoteClients(urlClients, ctx, logger, config.Orgs, buildClient)
if err != nil {
return nil, err
}

urlClients, err = appendRemoteClients(urlClients, ctx, logger, config.Repos, buildClient)

return urlClients, err
}

func appendRemoteClients(dest map[string]github.Client, ctx context.Context, logger *pterm.Logger, remotes []Remote, buildClient buildClientFunc) (map[string]github.Client, error) {
for i := range remotes {
if _, ok := dest[remotes[i].EnterpriseUrl]; !ok {
var err error
dest[remotes[i].EnterpriseUrl], err = buildClient(
ctx,
logger,
github.ClientOpts{
EnterpriseURL: remotes[i].EnterpriseUrl,
Token: remotes[i].Token,
},
)
if err != nil {
return nil, fmt.Errorf("failed to build client for '%s': %s", remotes[i].Name, err.Error())
}
}
}

return dest, nil
}
Loading

0 comments on commit 6cad620

Please sign in to comment.