diff --git a/pkg/cmd/gist/create/create.go b/pkg/cmd/gist/create/create.go index 10cdd42ea..7266bc1f9 100644 --- a/pkg/cmd/gist/create/create.go +++ b/pkg/cmd/gist/create/create.go @@ -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 +} diff --git a/pkg/cmd/gist/create/create_test.go b/pkg/cmd/gist/create/create_test.go index 686e543ed..bd57cfa2e 100644 --- a/pkg/cmd/gist/create/create_test.go +++ b/pkg/cmd/gist/create/create_test.go @@ -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{}{