Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enterprise woes and language filtering #25

Merged
merged 7 commits into from
Jun 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/sanity-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ jobs:
- name: Test
run: go test -v -coverprofile=coverage.out ./...

- name: Print coverage report
- name: Collect coverage report
id: gocoverage
run: |
go tool cover -func=coverage.out > coverage.txt
coverage=$(cat coverage.txt)
export coverage="$coverage"
echo "$coverage"
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,50 @@ and create a `repos.json` file:

![running piscator cast azemetre -x](./docs/cast-file.gif)

---

Running `piscator cast username --language=rust,lua` will output a JSON of
public repositories filtered by the languages you provide (comma separated) for
a user:

![running piscator cast azemetre -l rust,lua](./docs/cast-lang.gif)

<details>
<summary>example output of `piscator cast azemetre -l rust,lua`</summary>

```text
[
{
"name": "dotfiles",
"html_url": "https://github.com/azemetre/dotfiles",
"language": "Lua",
"fork": false,
"private": false,
"size": 2756
},
{
"name": "hipster.nvim",
"html_url": "https://github.com/azemetre/hipster.nvim",
"language": "Lua",
"fork": false,
"private": false,
"size": 1446
},
{
"name": "rust-solutions",
"html_url": "https://github.com/azemetre/rust-solutions",
"language": "Rust",
"fork": false,
"private": false,
"size": 47234
}
]
```

</details>

---

### [reel](#reels)

**Please note:** `piscator reel` can take the same flags as `piscator cast`, so
Expand All @@ -337,7 +381,7 @@ their repositories:

## [Todos](#todos)

- [ ] add ability to filter by language
- [x] add ability to filter by language
- [x] flesh out readme
- [x] create vhs tapes
- [x] automate release binaries
Expand Down
72 changes: 48 additions & 24 deletions cmd/piscator/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,40 @@ import (
)

var isSelfBool, isOrgBool, isForkedBool, makeFileBool bool
var githubToken string
var languageFilter, name, githubToken, username, password, enterprise string

func castRun(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("Please provide a GitHub username")
return
}

name := args[0]
tokenFileBool := viper.GetString("github_token") // get token from viper
isSelfBool, _ := cmd.PersistentFlags().GetBool("self")
isOrgBool, _ := cmd.PersistentFlags().GetBool("org")
isForkedBool, _ := cmd.PersistentFlags().GetBool("forked")
makeFileBool, _ := cmd.PersistentFlags().GetBool("makeFile")

sleeper := &piscator.RealSleeper{}

res, err := piscator.GetRepos(http.DefaultClient, sleeper, name, tokenFileBool, username, password, enterprise, isSelfBool, isOrgBool, isForkedBool, makeFileBool)

if err != nil {
fmt.Printf("Errors: %s", err)
return
}

if languageFilter != "" {
res, err = piscator.RepoByLanguage(res, languageFilter)
if err != nil {
fmt.Printf("Error filtering repositories by language: %s", err)
return
}
}

fmt.Println(res)
}

var castCmd = &cobra.Command{
Use: "cast",
Expand All @@ -22,28 +55,7 @@ repositories belonging to a user or organization, gathering a bountiful
collection of code treasures. Navigate with ease, discovering new horizons, and
charting your course towards software mastery.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("Please provide a GitHub username")
return
}

name := args[0]
tokenFileBool := viper.GetString("github_token") // get token from viper
isSelfBool, _ = cmd.PersistentFlags().GetBool("self")
isOrgBool, _ = cmd.PersistentFlags().GetBool("org")
isForkedBool, _ = cmd.PersistentFlags().GetBool("forked")
makeFileBool, _ = cmd.PersistentFlags().GetBool("makeFile")

sleeper := &piscator.RealSleeper{}

res, err := piscator.GetRepos(http.DefaultClient, sleeper, name, tokenFileBool, isSelfBool, isOrgBool, isForkedBool, makeFileBool)
if err != nil {
fmt.Printf("Errors: %s", err)
return
}
fmt.Println(res)
},
Run: castRun,
}

func init() {
Expand All @@ -53,10 +65,22 @@ func init() {
castCmd.PersistentFlags().BoolVarP(&isOrgBool, "org", "o", false, "Is an organization")
castCmd.PersistentFlags().BoolVarP(&isForkedBool, "forked", "x", false, "Include forked repositories")
castCmd.PersistentFlags().BoolVarP(&makeFileBool, "makeFile", "f", false, "Generate a repos.json file")

castCmd.PersistentFlags().StringVarP(&languageFilter, "language", "l", "", "Filter repositories by language(s)")

castCmd.PersistentFlags().StringVarP(&githubToken, "token", "t", "", "GitHub personal access token")
castCmd.PersistentFlags().StringVarP(&username, "username", "u", "", "GitHub username")
castCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "GitHub password")
castCmd.PersistentFlags().StringVarP(&enterprise, "enterprise", "e", "", "GitHub Enterprise URL")

// bind the token flag to a viper key
// bind the token flags to env keys
viper.BindPFlag("github_token", castCmd.PersistentFlags().Lookup("token"))
viper.BindPFlag("username", castCmd.PersistentFlags().Lookup("username"))
viper.BindPFlag("password", castCmd.PersistentFlags().Lookup("password"))

viper.BindEnv("github_token", "GITHUB_TOKEN")
viper.BindEnv("username", "GITHUB_USERNAME")
viper.BindEnv("password", "GITHUB_PASSWORD")

rootCmd.AddCommand(castCmd)
}
96 changes: 65 additions & 31 deletions cmd/piscator/reel.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,55 @@ import (

var isVerbose bool

func reelRun(cmd *cobra.Command, args []string) {
if isSelfBool {
// Use the GitHub username associated with the token
name = "" // Set name to empty string for self
} else if len(args) < 1 && githubToken == "" && viper.GetString("github_token") == "" {
fmt.Println("Please provide a GitHub username or specify a token")
return
} else if len(args) >= 1 {
name = args[0]
}

if name == "" {
fmt.Println("Please provide a GitHub username or org name")
return
}

tokenFileBool := viper.GetString("github_token") // get token from viper
isForkedBool, _ = cmd.PersistentFlags().GetBool("forked")
makeFileBool, _ = cmd.PersistentFlags().GetBool("makeFile")

sleeper := &piscator.RealSleeper{}

res, err := piscator.GetRepos(http.DefaultClient, sleeper, name, tokenFileBool, username, password, enterprise, isSelfBool, isOrgBool, isForkedBool, makeFileBool)
if err != nil {
fmt.Printf("Errors: %s", err)
return
}

if languageFilter != "" {
res, err = piscator.RepoByLanguage(res, languageFilter)
if err != nil {
fmt.Printf("Error filtering repositories by language: %s", err)
return
}
}

concurrentLimit := int8(10)
isVerbose, _ = cmd.PersistentFlags().GetBool("verbose")

err = piscator.CloneReposFromJson(piscator.RealCommandExecutor{}, res, name, concurrentLimit, isVerbose)

if err != nil {
fmt.Printf("Errors: %s", err)
return
}

fmt.Println("success friend :)")
}

var reelCmd = &cobra.Command{
Use: "reel",
Aliases: []string{"c"},
Expand All @@ -24,48 +73,33 @@ repositories, transforming them into valuable assets for your coding endeavors.
Unleash your fishing prowess, reel in those repositories, and embark on a coding
voyage like no other.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("Please provide a GitHub name")
return
}

name := args[0]
tokenFileBool := viper.GetString("github_token") // get token from viper
isSelfBool, _ = cmd.PersistentFlags().GetBool("self")
isOrgBool, _ = cmd.PersistentFlags().GetBool("org")
isForkedBool, _ = cmd.PersistentFlags().GetBool("forked")
makeFileBool, _ = cmd.PersistentFlags().GetBool("makeFile")

sleeper := &piscator.RealSleeper{}

res, err := piscator.GetRepos(http.DefaultClient, sleeper, name, tokenFileBool, isSelfBool, isOrgBool, isForkedBool, makeFileBool)
if err != nil {
fmt.Printf("Errors: %s", err)
return
}

concurrentLimit := int8(10)
isVerbose, _ = cmd.PersistentFlags().GetBool("verbose")

piscator.CloneReposFromJson(piscator.RealCommandExecutor{}, res, name, concurrentLimit, isVerbose)

fmt.Println("success friend :)")
},
Run: reelRun,
}

func init() {
viper.AutomaticEnv() // automatically use environment variables
viper.AutomaticEnv()

reelCmd.PersistentFlags().BoolVarP(&isSelfBool, "self", "s", false, "Your GitHub user, requires a personal access token")
reelCmd.PersistentFlags().BoolVarP(&isOrgBool, "org", "o", false, "Is an organization")
reelCmd.PersistentFlags().BoolVarP(&isForkedBool, "forked", "x", false, "Include forked repositories")
reelCmd.PersistentFlags().BoolVarP(&makeFileBool, "makeFile", "f", false, "Generate a repos.json file")
reelCmd.PersistentFlags().BoolVarP(&isVerbose, "verbose", "v", false, "logs detailed messaging to stdout")

reelCmd.PersistentFlags().StringVarP(&languageFilter, "language", "l", "", "Filter repositories by language(s)")

reelCmd.PersistentFlags().StringVarP(&githubToken, "token", "t", "", "GitHub personal access token")
reelCmd.PersistentFlags().StringVarP(&username, "username", "u", "", "GitHub username")
reelCmd.PersistentFlags().StringVarP(&password, "password", "p", "", "GitHub password")
reelCmd.PersistentFlags().StringVarP(&enterprise, "enterprise", "e", "", "GitHub Enterprise URL")

// bind the token flags to env keys
viper.BindPFlag("github_token", castCmd.PersistentFlags().Lookup("token"))
viper.BindPFlag("username", castCmd.PersistentFlags().Lookup("username"))
viper.BindPFlag("password", castCmd.PersistentFlags().Lookup("password"))

// bind the token flag to a viper key
viper.BindPFlag("github_token", reelCmd.PersistentFlags().Lookup("token"))
viper.BindEnv("github_token", "GITHUB_TOKEN")
viper.BindEnv("username", "GITHUB_USERNAME")
viper.BindEnv("password", "GITHUB_PASSWORD")

rootCmd.AddCommand(reelCmd)
}
Binary file added docs/cast-lang.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading