Skip to content

Commit

Permalink
extension create: make initial commit (#6833)
Browse files Browse the repository at this point in the history
  • Loading branch information
mntlty committed Jan 24, 2023
1 parent 9dc2653 commit a231b4a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 16 deletions.
19 changes: 14 additions & 5 deletions pkg/cmd/extension/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,18 @@ func NewCmdExtension(f *cmdutil.Factory) *cobra.Command {
} else {
fullName = "gh-" + extName
}

cs := io.ColorScheme()

commitIcon := cs.SuccessIcon()
if err := m.Create(fullName, tmplType); err != nil {
return err
if errors.Is(err, ErrInitialCommitFailed) {
commitIcon = cs.FailureIcon()
} else {
return err
}
}

if !io.IsStdoutTTY() {
return nil
}
Expand All @@ -577,15 +586,14 @@ func NewCmdExtension(f *cmdutil.Factory) *cobra.Command {
"- run 'cd %[1]s; gh extension install .; gh %[2]s' to see your new extension in action",
fullName, extName)

cs := io.ColorScheme()
if tmplType == extensions.GoBinTemplateType {
goBinChecks = heredoc.Docf(`
%[1]s Downloaded Go dependencies
%[1]s Built %[2]s binary
`, cs.SuccessIcon(), fullName)
steps = heredoc.Docf(`
- run 'cd %[1]s; gh extension install .; gh %[2]s' to see your new extension in action
- use 'go build && gh %[2]s' to see changes in your code as you develop`, fullName, extName)
- run 'go build && gh %[2]s' to see changes in your code as you develop`, fullName, extName)
} else if tmplType == extensions.OtherBinTemplateType {
steps = heredoc.Docf(`
- run 'cd %[1]s; gh extension install .' to install your extension locally
Expand All @@ -596,17 +604,18 @@ func NewCmdExtension(f *cmdutil.Factory) *cobra.Command {
out := heredoc.Docf(`
%[1]s Created directory %[2]s
%[1]s Initialized git repository
%[7]s Made initial commit
%[1]s Set up extension scaffolding
%[6]s
%[2]s is ready for development!
%[4]s
%[5]s
- commit and use 'gh repo create' to share your extension with others
- run 'gh repo create' to share your extension with others
For more information on writing extensions:
%[3]s
`, cs.SuccessIcon(), fullName, link, cs.Bold("Next Steps"), steps, goBinChecks)
`, cs.SuccessIcon(), fullName, link, cs.Bold("Next Steps"), steps, goBinChecks, commitIcon)
fmt.Fprint(io.Out, out)
return nil
},
Expand Down
44 changes: 39 additions & 5 deletions pkg/cmd/extension/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,13 +605,14 @@ func TestNewCmdExtension(t *testing.T) {
wantStdout: heredoc.Doc(`
✓ Created directory gh-test
✓ Initialized git repository
✓ Made initial commit
✓ Set up extension scaffolding
gh-test is ready for development!
Next Steps
- run 'cd gh-test; gh extension install .; gh test' to see your new extension in action
- commit and use 'gh repo create' to share your extension with others
- run 'gh repo create' to share your extension with others
For more information on writing extensions:
https://docs.github.com/github-cli/github-cli/creating-github-cli-extensions
Expand All @@ -634,6 +635,7 @@ func TestNewCmdExtension(t *testing.T) {
wantStdout: heredoc.Doc(`
✓ Created directory gh-test
✓ Initialized git repository
✓ Made initial commit
✓ Set up extension scaffolding
✓ Downloaded Go dependencies
✓ Built gh-test binary
Expand All @@ -642,8 +644,8 @@ func TestNewCmdExtension(t *testing.T) {
Next Steps
- run 'cd gh-test; gh extension install .; gh test' to see your new extension in action
- use 'go build && gh test' to see changes in your code as you develop
- commit and use 'gh repo create' to share your extension with others
- run 'go build && gh test' to see changes in your code as you develop
- run 'gh repo create' to share your extension with others
For more information on writing extensions:
https://docs.github.com/github-cli/github-cli/creating-github-cli-extensions
Expand All @@ -666,6 +668,7 @@ func TestNewCmdExtension(t *testing.T) {
wantStdout: heredoc.Doc(`
✓ Created directory gh-test
✓ Initialized git repository
✓ Made initial commit
✓ Set up extension scaffolding
gh-test is ready for development!
Expand All @@ -674,7 +677,7 @@ func TestNewCmdExtension(t *testing.T) {
- run 'cd gh-test; gh extension install .' to install your extension locally
- fill in script/build.sh with your compilation script for automated builds
- compile a gh-test binary locally and run 'gh test' to see changes
- commit and use 'gh repo create' to share your extension with others
- run 'gh repo create' to share your extension with others
For more information on writing extensions:
https://docs.github.com/github-cli/github-cli/creating-github-cli-extensions
Expand All @@ -697,13 +700,44 @@ func TestNewCmdExtension(t *testing.T) {
wantStdout: heredoc.Doc(`
✓ Created directory gh-test
✓ Initialized git repository
✓ Made initial commit
✓ Set up extension scaffolding
gh-test is ready for development!
Next Steps
- run 'cd gh-test; gh extension install .; gh test' to see your new extension in action
- commit and use 'gh repo create' to share your extension with others
- run 'gh repo create' to share your extension with others
For more information on writing extensions:
https://docs.github.com/github-cli/github-cli/creating-github-cli-extensions
`),
},
{
name: "create extension tty with argument commit fails",
args: []string{"create", "test"},
managerStubs: func(em *extensions.ExtensionManagerMock) func(*testing.T) {
em.CreateFunc = func(name string, tmplType extensions.ExtTemplateType) error {
return ErrInitialCommitFailed
}
return func(t *testing.T) {
calls := em.CreateCalls()
assert.Equal(t, 1, len(calls))
assert.Equal(t, "gh-test", calls[0].Name)
}
},
isTTY: true,
wantStdout: heredoc.Doc(`
✓ Created directory gh-test
✓ Initialized git repository
X Made initial commit
✓ Set up extension scaffolding
gh-test is ready for development!
Next Steps
- run 'cd gh-test; gh extension install .; gh test' to see your new extension in action
- run 'gh repo create' to share your extension with others
For more information on writing extensions:
https://docs.github.com/github-cli/github-cli/creating-github-cli-extensions
Expand Down
36 changes: 30 additions & 6 deletions pkg/cmd/extension/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import (
"gopkg.in/yaml.v3"
)

// ErrInitialCommitFailed indicates the initial commit when making a new extension failed.
var ErrInitialCommitFailed = errors.New("initial commit failed")

type Manager struct {
dataDir func() string
lookPath func(string) (string, error)
Expand Down Expand Up @@ -654,8 +657,15 @@ func (m *Manager) Create(name string, tmplType extensions.ExtTemplateType) error
}

scopedClient := m.gitClient.ForRepo(name)
_, err := scopedClient.CommandOutput([]string{"add", name, "--chmod=+x"})
return err
if _, err := scopedClient.CommandOutput([]string{"add", name, "--chmod=+x"}); err != nil {
return err
}

if _, err := scopedClient.CommandOutput([]string{"commit", "-m", "initial commit"}); err != nil {
return ErrInitialCommitFailed
}

return nil
}

func (m *Manager) otherBinScaffolding(name string) error {
Expand All @@ -672,8 +682,15 @@ func (m *Manager) otherBinScaffolding(name string) error {
return err
}

_, err := scopedClient.CommandOutput([]string{"add", "."})
return err
if _, err := scopedClient.CommandOutput([]string{"add", "."}); err != nil {
return err
}

if _, err := scopedClient.CommandOutput([]string{"commit", "-m", "initial commit"}); err != nil {
return ErrInitialCommitFailed
}

return nil
}

func (m *Manager) goBinScaffolding(name string) error {
Expand Down Expand Up @@ -718,8 +735,15 @@ func (m *Manager) goBinScaffolding(name string) error {
}

scopedClient := m.gitClient.ForRepo(name)
_, err = scopedClient.CommandOutput([]string{"add", "."})
return err
if _, err := scopedClient.CommandOutput([]string{"add", "."}); err != nil {
return err
}

if _, err := scopedClient.CommandOutput([]string{"commit", "-m", "initial commit"}); err != nil {
return ErrInitialCommitFailed
}

return nil
}

func isSymlink(m os.FileMode) bool {
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/extension/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,7 @@ func TestManager_Create(t *testing.T) {
gc.On("ForRepo", "gh-test").Return(gcOne).Once()
gc.On("CommandOutput", []string{"init", "--quiet", "gh-test"}).Return("", nil).Once()
gcOne.On("CommandOutput", []string{"add", "gh-test", "--chmod=+x"}).Return("", nil).Once()
gcOne.On("CommandOutput", []string{"commit", "-m", "initial commit"}).Return("", nil).Once()

m := newTestManager(".", nil, gc, ios)

Expand Down Expand Up @@ -1068,6 +1069,7 @@ func TestManager_Create_go_binary(t *testing.T) {
gc.On("ForRepo", "gh-test").Return(gcOne).Once()
gc.On("CommandOutput", []string{"init", "--quiet", "gh-test"}).Return("", nil).Once()
gcOne.On("CommandOutput", []string{"add", "."}).Return("", nil).Once()
gcOne.On("CommandOutput", []string{"commit", "-m", "initial commit"}).Return("", nil).Once()

m := newTestManager(".", &http.Client{Transport: &reg}, gc, ios)

Expand Down Expand Up @@ -1111,6 +1113,7 @@ func TestManager_Create_other_binary(t *testing.T) {
gc.On("CommandOutput", []string{"init", "--quiet", "gh-test"}).Return("", nil).Once()
gcOne.On("CommandOutput", []string{"add", filepath.Join("script", "build.sh"), "--chmod=+x"}).Return("", nil).Once()
gcOne.On("CommandOutput", []string{"add", "."}).Return("", nil).Once()
gcOne.On("CommandOutput", []string{"commit", "-m", "initial commit"}).Return("", nil).Once()

m := newTestManager(".", nil, gc, ios)

Expand Down

0 comments on commit a231b4a

Please sign in to comment.