Gist Create - Output Filename (#1498)

This commit is contained in:
Brandon Wilson 2020-08-13 07:38:49 -04:00 committed by GitHub
parent 26a5f118c1
commit 0d60798ca4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 5 deletions

View file

@ -7,6 +7,8 @@ import (
"io/ioutil"
"net/http"
"path"
"regexp"
"sort"
"strings"
"github.com/MakeNowJust/heredoc"
@ -96,8 +98,21 @@ func createRun(opts *CreateOptions) error {
return fmt.Errorf("failed to collect files for posting: %w", err)
}
gistName := guessGistName(files)
processMessage := "Creating gist..."
completionMessage := "Created gist"
if gistName != "" {
if len(files) > 1 {
processMessage = "Creating gist with multiple files"
} else {
processMessage = fmt.Sprintf("Creating gist %s", gistName)
}
completionMessage = fmt.Sprintf("Created gist %s", gistName)
}
errOut := opts.IO.ErrOut
fmt.Fprintf(errOut, "%s Creating gist...\n", utils.Gray("-"))
fmt.Fprintf(errOut, "%s %s\n", utils.Gray("-"), processMessage)
httpClient, err := opts.HttpClient()
if err != nil {
@ -116,7 +131,7 @@ func createRun(opts *CreateOptions) error {
return fmt.Errorf("%s Failed to create gist: %w", utils.Red("X"), err)
}
fmt.Fprintf(errOut, "%s Created gist\n", utils.Green("✓"))
fmt.Fprintf(errOut, "%s %s\n", utils.Green("✓"), completionMessage)
fmt.Fprintln(opts.IO.Out, gist.HTMLURL)
@ -154,3 +169,22 @@ func processFiles(stdin io.ReadCloser, filenames []string) (map[string]string, e
return fs, nil
}
func guessGistName(files map[string]string) string {
filenames := make([]string, 0, len(files))
gistName := ""
re := regexp.MustCompile(`^gistfile\d+\.txt$`)
for k := range files {
if !re.MatchString(k) {
filenames = append(filenames, k)
}
}
if len(filenames) > 0 {
sort.Strings(filenames)
gistName = filenames[0]
}
return gistName
}

View file

@ -30,6 +30,24 @@ func Test_processFiles(t *testing.T) {
assert.Equal(t, "hey cool how is it going", files["gistfile0.txt"])
}
func Test_guessGistName_stdin(t *testing.T) {
files := map[string]string{"gistfile0.txt": "sample content"}
gistName := guessGistName(files)
assert.Equal(t, "", gistName)
}
func Test_guessGistName_userFiles(t *testing.T) {
files := map[string]string{
"fig.txt": "I am a fig.",
"apple.txt": "I am an apple.",
"gistfile0.txt": "sample content",
}
gistName := guessGistName(files)
assert.Equal(t, "apple.txt", gistName)
}
func TestNewCmdCreate(t *testing.T) {
tests := []struct {
name string
@ -157,7 +175,7 @@ func Test_createRun(t *testing.T) {
Filenames: []string{fixtureFile},
},
wantOut: "https://gist.github.com/aa5a315d61ae9438b18d\n",
wantStderr: "- Creating gist...\n✓ Created gist\n",
wantStderr: "- Creating gist fixture.txt\n✓ Created gist fixture.txt\n",
wantErr: false,
wantParams: map[string]interface{}{
"public": true,
@ -175,7 +193,7 @@ func Test_createRun(t *testing.T) {
Filenames: []string{fixtureFile},
},
wantOut: "https://gist.github.com/aa5a315d61ae9438b18d\n",
wantStderr: "- Creating gist...\n✓ Created gist\n",
wantStderr: "- Creating gist fixture.txt\n✓ Created gist fixture.txt\n",
wantErr: false,
wantParams: map[string]interface{}{
"description": "an incredibly interesting gist",
@ -193,7 +211,7 @@ func Test_createRun(t *testing.T) {
},
stdin: "cool stdin content",
wantOut: "https://gist.github.com/aa5a315d61ae9438b18d\n",
wantStderr: "- Creating gist...\n✓ Created gist\n",
wantStderr: "- Creating gist with multiple files\n✓ Created gist fixture.txt\n",
wantErr: false,
wantParams: map[string]interface{}{
"files": map[string]interface{}{