Skip to content

Commit 0d60798

Browse files
author
Brandon Wilson
authored
Gist Create - Output Filename (cli#1498)
1 parent 26a5f11 commit 0d60798

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

pkg/cmd/gist/create/create.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"io/ioutil"
88
"net/http"
99
"path"
10+
"regexp"
11+
"sort"
1012
"strings"
1113

1214
"github.com/MakeNowJust/heredoc"
@@ -96,8 +98,21 @@ func createRun(opts *CreateOptions) error {
9698
return fmt.Errorf("failed to collect files for posting: %w", err)
9799
}
98100

101+
gistName := guessGistName(files)
102+
103+
processMessage := "Creating gist..."
104+
completionMessage := "Created gist"
105+
if gistName != "" {
106+
if len(files) > 1 {
107+
processMessage = "Creating gist with multiple files"
108+
} else {
109+
processMessage = fmt.Sprintf("Creating gist %s", gistName)
110+
}
111+
completionMessage = fmt.Sprintf("Created gist %s", gistName)
112+
}
113+
99114
errOut := opts.IO.ErrOut
100-
fmt.Fprintf(errOut, "%s Creating gist...\n", utils.Gray("-"))
115+
fmt.Fprintf(errOut, "%s %s\n", utils.Gray("-"), processMessage)
101116

102117
httpClient, err := opts.HttpClient()
103118
if err != nil {
@@ -116,7 +131,7 @@ func createRun(opts *CreateOptions) error {
116131
return fmt.Errorf("%s Failed to create gist: %w", utils.Red("X"), err)
117132
}
118133

119-
fmt.Fprintf(errOut, "%s Created gist\n", utils.Green("✓"))
134+
fmt.Fprintf(errOut, "%s %s\n", utils.Green("✓"), completionMessage)
120135

121136
fmt.Fprintln(opts.IO.Out, gist.HTMLURL)
122137

@@ -154,3 +169,22 @@ func processFiles(stdin io.ReadCloser, filenames []string) (map[string]string, e
154169

155170
return fs, nil
156171
}
172+
173+
func guessGistName(files map[string]string) string {
174+
filenames := make([]string, 0, len(files))
175+
gistName := ""
176+
177+
re := regexp.MustCompile(`^gistfile\d+\.txt$`)
178+
for k := range files {
179+
if !re.MatchString(k) {
180+
filenames = append(filenames, k)
181+
}
182+
}
183+
184+
if len(filenames) > 0 {
185+
sort.Strings(filenames)
186+
gistName = filenames[0]
187+
}
188+
189+
return gistName
190+
}

pkg/cmd/gist/create/create_test.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ func Test_processFiles(t *testing.T) {
3030
assert.Equal(t, "hey cool how is it going", files["gistfile0.txt"])
3131
}
3232

33+
func Test_guessGistName_stdin(t *testing.T) {
34+
files := map[string]string{"gistfile0.txt": "sample content"}
35+
36+
gistName := guessGistName(files)
37+
assert.Equal(t, "", gistName)
38+
}
39+
40+
func Test_guessGistName_userFiles(t *testing.T) {
41+
files := map[string]string{
42+
"fig.txt": "I am a fig.",
43+
"apple.txt": "I am an apple.",
44+
"gistfile0.txt": "sample content",
45+
}
46+
47+
gistName := guessGistName(files)
48+
assert.Equal(t, "apple.txt", gistName)
49+
}
50+
3351
func TestNewCmdCreate(t *testing.T) {
3452
tests := []struct {
3553
name string
@@ -157,7 +175,7 @@ func Test_createRun(t *testing.T) {
157175
Filenames: []string{fixtureFile},
158176
},
159177
wantOut: "https://gist.github.com/aa5a315d61ae9438b18d\n",
160-
wantStderr: "- Creating gist...\n✓ Created gist\n",
178+
wantStderr: "- Creating gist fixture.txt\n✓ Created gist fixture.txt\n",
161179
wantErr: false,
162180
wantParams: map[string]interface{}{
163181
"public": true,
@@ -175,7 +193,7 @@ func Test_createRun(t *testing.T) {
175193
Filenames: []string{fixtureFile},
176194
},
177195
wantOut: "https://gist.github.com/aa5a315d61ae9438b18d\n",
178-
wantStderr: "- Creating gist...\n✓ Created gist\n",
196+
wantStderr: "- Creating gist fixture.txt\n✓ Created gist fixture.txt\n",
179197
wantErr: false,
180198
wantParams: map[string]interface{}{
181199
"description": "an incredibly interesting gist",
@@ -193,7 +211,7 @@ func Test_createRun(t *testing.T) {
193211
},
194212
stdin: "cool stdin content",
195213
wantOut: "https://gist.github.com/aa5a315d61ae9438b18d\n",
196-
wantStderr: "- Creating gist...\n✓ Created gist\n",
214+
wantStderr: "- Creating gist with multiple files\n✓ Created gist fixture.txt\n",
197215
wantErr: false,
198216
wantParams: map[string]interface{}{
199217
"files": map[string]interface{}{

0 commit comments

Comments
 (0)