git-bug/commands/title_edit.go

68 lines
1.3 KiB
Go
Raw Normal View History

2018-09-16 02:17:06 +03:00
package commands
import (
2020-06-28 19:26:29 +03:00
"github.com/spf13/cobra"
2018-09-16 02:17:06 +03:00
_select "github.com/MichaelMure/git-bug/commands/select"
2018-09-16 02:17:06 +03:00
"github.com/MichaelMure/git-bug/input"
)
2020-06-28 19:26:29 +03:00
type titleEditOptions struct {
title string
}
func newTitleEditCommand() *cobra.Command {
env := newEnv()
options := titleEditOptions{}
cmd := &cobra.Command{
Use: "edit [ID]",
Short: "Edit a title of a bug.",
PreRunE: loadBackendEnsureUser(env),
PostRunE: closeBackend(env),
2020-06-28 19:26:29 +03:00
RunE: func(cmd *cobra.Command, args []string) error {
return runTitleEdit(env, options, args)
},
}
flags := cmd.Flags()
flags.SortFlags = false
flags.StringVarP(&options.title, "title", "t", "",
"Provide a title to describe the issue",
)
2018-09-16 02:17:06 +03:00
2020-06-28 19:26:29 +03:00
return cmd
}
func runTitleEdit(env *Env, opts titleEditOptions, args []string) error {
b, args, err := _select.ResolveBug(env.backend, args)
2018-09-16 02:17:06 +03:00
if err != nil {
return err
}
snap := b.Snapshot()
2020-06-28 19:26:29 +03:00
if opts.title == "" {
opts.title, err = input.BugTitleEditorInput(env.repo, snap.Title)
2018-09-16 02:17:06 +03:00
if err == input.ErrEmptyTitle {
2020-06-28 19:26:29 +03:00
env.out.Println("Empty title, aborting.")
2018-09-16 02:17:06 +03:00
return nil
}
if err != nil {
return err
}
}
2020-06-28 19:26:29 +03:00
if opts.title == snap.Title {
env.err.Println("No change, aborting.")
}
2020-06-28 19:26:29 +03:00
_, err = b.SetTitle(opts.title)
2018-09-16 02:17:06 +03:00
if err != nil {
return err
}
return b.Commit()
}