Skip to content

Commit

Permalink
Merge pull request #8 from k1LoW/gist
Browse files Browse the repository at this point in the history
Add `ghput gist`
  • Loading branch information
k1LoW authored May 1, 2020
2 parents 33b3bc2 + 200c897 commit e2b3e99
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ $ echo 'This is comment !!' | GITHUB_TOKEN=XXXXXxxxxxXXxxxx ghput pr-comment --o
$ GITHUB_TOKEN=XXXXXxxxxxXXxxxx ghput commit --owner k1LoW --repo myrepo --branch master --file file.txt --path path/to/file.txt --message 'Commit file !!'
```

**Put file to Gist:**

``` console
$ GITHUB_TOKEN=XXXXXxxxxxXXxxxx ghput gist --file file.txt
```

``` console
$ cat file.txt | GITHUB_TOKEN=XXXXXxxxxxXXxxxx ghput gist
```

**Use on GitHub Enterprise:**

``` console
Expand Down
2 changes: 1 addition & 1 deletion cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func init() {
commitCmd.Flags().StringVarP(&owner, "owner", "", "", "owner")
commitCmd.Flags().StringVarP(&repo, "repo", "", "", "repo")
commitCmd.Flags().StringVarP(&branch, "branch", "", "master", "branch")
commitCmd.Flags().StringVarP(&file, "file", "", "", "commit target file")
commitCmd.Flags().StringVarP(&file, "file", "", "", "target file")
commitCmd.Flags().StringVarP(&path, "path", "", "", "commit path")
commitCmd.Flags().StringVarP(&message, "message", "", "commit by ghput", "commit message")
}
92 changes: 92 additions & 0 deletions cmd/gist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright © 2020 Ken'ichiro Oyama <[email protected]>
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.
*/
package cmd

import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"

"github.com/k1LoW/ghput/gh"
"github.com/spf13/cobra"
)

// gistCmd represents the gist command
var gistCmd = &cobra.Command{
Use: "gist",
Short: "Put gist",
Long: `Put gist.`,
Args: func(cmd *cobra.Command, args []string) error {
fi, err := os.Stdin.Stat()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
if (fi.Mode()&os.ModeCharDevice) != 0 && file == "" {
return errors.New("`ghput gist` need `--file` OR STDIN")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
err := runGist(os.Stdin, os.Stdout)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
},
}

func runGist(stdin io.Reader, stdout io.Writer) (err error) {
ctx := context.Background()
g, err := gh.New(owner, repo, key)
if err != nil {
return err
}
var (
r io.Reader
fname string
)
if file != "" {
f, err := os.Open(file)
if err != nil {
return err
}
defer func() {
err = f.Close()
}()
r = f
fname = filepath.Base(file)
} else {
r = stdin
fname = "stdin"
}
return g.CreateGist(ctx, fname, public, r, stdout)
}

func init() {
rootCmd.AddCommand(gistCmd)
gistCmd.Flags().StringVarP(&file, "file", "", "", "target file")
gistCmd.Flags().BoolVarP(&public, "public", "", false, "public gist")
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
file string
path string
message string
public bool
)

// rootCmd represents the base command when called without any subcommands
Expand Down
27 changes: 27 additions & 0 deletions gh/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,33 @@ func (g *Gh) CommitAndPush(ctx context.Context, branch, file, rPath, message str
return nil
}

func (g *Gh) CreateGist(ctx context.Context, fname string, public bool, in io.Reader, out io.Writer) error {
b, err := ioutil.ReadAll(in)
if err != nil {
return err
}

content := string(b)
files := make(map[github.GistFilename]github.GistFile, 1)
files[github.GistFilename(fname)] = github.GistFile{
Size: github.Int(len(content)),
Filename: github.String(fname),
Content: github.String(content),
}

input := &github.Gist{
Description: github.String("Put by ghput"),
Public: github.Bool(public),
Files: files,
}
gist, _, err := g.client.Gists.Create(ctx, input)
if err != nil {
return err
}
_, _ = fmt.Fprintf(out, "%s\n", *gist.HTMLURL)
return nil
}

type roundTripper struct {
transport *http.Transport
accessToken string
Expand Down

0 comments on commit e2b3e99

Please sign in to comment.