Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
using cobra, month option, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed May 5, 2017
1 parent fee262f commit 8b9057c
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 132 deletions.
36 changes: 24 additions & 12 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions Gopkg.toml
@@ -1,4 +1,56 @@

## Gopkg.toml example (these lines may be deleted)

## "required" lists a set of packages (not projects) that must be included in
## Gopkg.lock. This list is merged with the set of packages imported by the current
## project. Use it when your project needs a package it doesn't explicitly import -
## including "main" packages.
# required = ["github.com/user/thing/cmd/thing"]

## "ignored" lists a set of packages (not projects) that are ignored when
## dep statically analyzes source code. Ignored packages can be in this project,
## or in a dependency.
# ignored = ["github.com/user/project/badpkg"]

## Dependencies define constraints on dependent projects. They are respected by
## dep whether coming from the Gopkg.toml of the current project or a dependency.
# [[dependencies]]
## Required: the root import path of the project being constrained.
# name = "github.com/user/project"
#
## Recommended: the version constraint to enforce for the project.
## Only one of "branch", "version" or "revision" can be specified.
# version = "1.0.0"
# branch = "master"
# revision = "abc123"
#
## Optional: an alternate location (URL or import path) for the project's source.
# source = "https://github.com/myfork/package.git"

## Overrides have the same structure as [[dependencies]], but supercede all
## [[dependencies]] declarations from all projects. Only the current project's
## [[overrides]] are applied.
##
## Overrides are a sledgehammer. Use them only as a last resort.
# [[overrides]]
## Required: the root import path of the project being constrained.
# name = "github.com/user/project"
#
## Optional: specifying a version constraint override will cause all other
## constraints on this project to be ignored; only the overriden constraint
## need be satisfied.
## Again, only one of "branch", "version" or "revision" can be specified.
# version = "1.0.0"
# branch = "master"
# revision = "abc123"
#
## Optional: specifying an alternate source location as an override will
## enforce that the alternate location is used for that project, regardless of
## what source location any dependent projects specify.
# source = "https://github.com/myfork/package.git"



[[dependencies]]
branch = "master"
name = "github.com/caarlos0/spin"
Expand Down
98 changes: 0 additions & 98 deletions cmd/karmahub/main.go

This file was deleted.

135 changes: 135 additions & 0 deletions cmd/root.go
@@ -0,0 +1,135 @@
package cmd

import (
"context"
"fmt"
"os"
"time"

"github.com/caarlos0/karmahub/karma"
"github.com/caarlos0/spin"
"github.com/google/go-github/github"
"github.com/spf13/cobra"
"golang.org/x/oauth2"
)

var token string
var user string
var filter string
var months int

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "karmahub",
Short: "get your history of reviews/comments and pull requests/issues opened",
Long: `Compares the amount of issues and pull requests you created with the
amount of comments and code reviews you did.
The idea is to use it at your daily job organization, so you can get an idea
of how much are you actually contributing to the code review practices.`,
RunE: func(cmd *cobra.Command, args []string) error {
if token == "" {
return fmt.Errorf("missing github token")
}
var spin = spin.New("%s Gathering data...").Start()

var ctx = context.Background()
var client = github.NewClient(oauth2.NewClient(
ctx,
oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}),
))
var fn = karma.GitHubSearch(ctx, client)
if user == "" {
me, _, err := client.Users.Get(ctx, "")
if err != nil {
spin.Stop()
return err
}
user = *me.Login
}
prs, err := karma.Authors(fn, user, filter, months)
if err != nil {
spin.Stop()
return err
}
crs, err := karma.Reviews(fn, user, filter, months)
if err != nil {
spin.Stop()
return err
}
spin.Stop()

// header
fmt.Printf("\033[1;36mAction ")
for i := 0; i < months; i++ {
fmt.Printf(
"\t%v",
time.Now().AddDate(0, i*-1, 0).UTC().Format("Jan"),
)
}
fmt.Printf("\033[0m\n")

// authored
fmt.Printf("Authored ")
for i := 0; i < months; i++ {
fmt.Printf("\t%v", prs[i])
}
fmt.Printf("\n")

// reviewed
fmt.Printf("Reviewed ")
for i := 0; i < months; i++ {
fmt.Printf("\t%v", crs[i])
}
fmt.Printf("\n")

// karma
fmt.Printf("Karma ")
for i := 0; i < months; i++ {
fmt.Printf("\t%.1f", float32(crs[i])/float32(prs[i]))
}
fmt.Printf("\n")
return nil
},
}

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute(version string) {
Version = version
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}

func init() {
RootCmd.PersistentFlags().StringVarP(
&token,
"token",
"t",
os.Getenv("GITHUB_TOKEN"),
"Your GitHub token",
)
RootCmd.PersistentFlags().StringVarP(
&user,
"user",
"u",
"",
"User to collect data from",
)
RootCmd.PersistentFlags().IntVarP(
&months,
"months",
"m",
3,
"Number of months to search",
)
RootCmd.PersistentFlags().StringVarP(
&filter,
"filter",
"f",
"",
"Additional filters, github syntax. E.g.: is:pr will gather data for pull requests only",
)
}
22 changes: 22 additions & 0 deletions cmd/version.go
@@ -0,0 +1,22 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// Version of karma
var Version = "dev"

var versionCmd = &cobra.Command{
Use: "version",
Short: "shows karmahub version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("karmahub version", Version)
},
}

func init() {
RootCmd.AddCommand(versionCmd)
}
1 change: 0 additions & 1 deletion goreleaser.yml
@@ -1,5 +1,4 @@
build:
main: ./cmd/karmahub/main.go
goos:
- windows
- darwin
Expand Down

0 comments on commit 8b9057c

Please sign in to comment.