Skip to content

Commit

Permalink
Use GITHUB_TOKEN env var
Browse files Browse the repository at this point in the history
Signed-off-by: Hunter Gatewood <[email protected]>
  • Loading branch information
hcgatewood committed Jun 9, 2021
1 parent ade678a commit f02548a
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 41 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Hunter Gatewood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
# ghissue
<h1 align="center">
ghissue
<br>
<a href="https://github.com/hcgatewood/ghissue"><img src="https://raw.githubusercontent.com/magma/magma/master/assets/undraw_uploading_go67.png" width="550"></a>
</h1>

Bulk-upload GitHub Issues as easy as
<h3 align="center">Bulk-upload GitHub Issues</h3>

<p align="center">
<a href="https://github.com/hcgatewood/ghissue/blob/master/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License"></a>
<a href="https://github.com/hcgatewood/ghissue/releases"><img src="https://img.shields.io/github/release/hcgatewood/ghissue" alt="GitHub Release"></a>
<a href="https://github.com/hcgatewood/ghissue/commits/master"><img src="https://img.shields.io/github/last-commit/hcgatewood/ghissue" alt="GitHub last commit"></a>
<a href="https://github.com/hcgatewood/ghissue/blob/master/lib/create_test.go"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg" alt="PR's Welcome"></a>
</p>

Bulk-upload as easy as

```bash
ghissue --token gh.token create issues.txt
ghissue create issues.txt
```

## Example

Prerequisite: create a [GitHub personal access token](https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token) and save it to `./gh.token`
Prerequisite: create a [GitHub personal access token](https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token) and save its contents to the `GITHUB_TOKEN` environment variable.

We'll create two issues: a self-assigned feature request and a bug report

Expand All @@ -30,7 +43,7 @@ Next, bulk-create the Issues. This will also open your browser to view the newly

```bash

$ ghissue --token gh.token create issues.txt --open
$ ghissue create issues.txt --open

14,15
```
Expand Down
Binary file added assets/undraw_uploading_go67.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 1 addition & 25 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package cmd

import (
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"hcgatewood/ghissue/lib"
Expand Down Expand Up @@ -55,19 +51,14 @@ func init() {
}

func runCreate(cmd *cobra.Command, args []string) {
token, err := read(rootTokenFilepath)
if err != nil {
exit(cmd, err)
}
token = strings.TrimSpace(token)
input, err := read(args[0])
if err != nil {
exit(cmd, err)
}
input = lib.TrimInput(input)

cfg := &lib.Config{
Token: token,
Token: rootToken,
DryRun: globalConfig.DryRun,
Info: globalConfig.Info,
Open: globalConfig.Open,
Expand All @@ -78,18 +69,3 @@ func runCreate(cmd *cobra.Command, args []string) {
exit(cmd, err)
}
}

func read(filepath string) (string, error) {
bytes, err := ioutil.ReadFile(filepath)
if err != nil {
return "", errors.Errorf("could not read from %s: %+v", filepath, err)
}
return string(bytes), nil
}

func exit(cmd *cobra.Command, err error) {
_ = cmd.Usage()
fmt.Println("")
fmt.Println("Error:", err)
os.Exit(1)
}
43 changes: 35 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,33 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/pkg/errors"
"github.com/spf13/cobra"
)

var (
// Global flag vars
rootTokenFilepath string
rootToken string
)

var (
tokenEnvName = "GITHUB_TOKEN"
)

var RootCmd = &cobra.Command{
Use: "ghissue",
Example: "ghissue --token ~/.creds/gh.token create issues.txt",
Short: "Bulk-upload GitHub Issues",
Use: "ghissue",
Example: "GITHUB_TOKEN='...' ghissue create ./issues.txt",
Short: "Bulk-upload GitHub Issues",
PersistentPreRun: globalPre,
}

func init() {
RootCmd.PersistentFlags().StringVar(&rootTokenFilepath, "token", "", "Filepath of GitHub personal access token (required)")
err := RootCmd.MarkPersistentFlagRequired("token")
if err != nil {
panic(err)
}
RootCmd.PersistentFlags().StringVar(&rootToken, "token", os.Getenv(tokenEnvName), "GitHub personal access token")
}

func Execute() {
Expand All @@ -32,3 +37,25 @@ func Execute() {
os.Exit(1)
}
}

func globalPre(cmd *cobra.Command, args []string) {
if rootToken == "" {
exit(cmd, errors.New("Must provide a GitHub personal access token. Try setting GITHUB_TOKEN environment variable."))
}
rootToken = strings.TrimSpace(rootToken)
}

func exit(cmd *cobra.Command, err error) {
_ = cmd.Usage()
fmt.Println("")
fmt.Println("Error:", err)
os.Exit(1)
}

func read(filepath string) (string, error) {
bytes, err := ioutil.ReadFile(filepath)
if err != nil {
return "", errors.Errorf("could not read from %s: %+v", filepath, err)
}
return string(bytes), nil
}
4 changes: 2 additions & 2 deletions gen/ghissue.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ Bulk-upload GitHub Issues
### Examples

```
ghissue --token ~/.creds/gh.token create issues.txt
GITHUB_TOKEN='...' ghissue create ./issues.txt
```

### Options

```
-h, --help help for ghissue
--token string Filepath of GitHub personal access token (required)
--token string GitHub personal access token
```

### SEE ALSO
Expand Down
2 changes: 1 addition & 1 deletion gen/ghissue_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ ghissue create issues.txt [flags]
### Options inherited from parent commands

```
--token string Filepath of GitHub personal access token (required)
--token string GitHub personal access token
```

### SEE ALSO
Expand Down

0 comments on commit f02548a

Please sign in to comment.