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
|
|
|
|
2020-07-28 21:24:24 +03:00
|
|
|
_select "github.com/MichaelMure/git-bug/commands/select"
|
2018-09-16 02:17:06 +03:00
|
|
|
"github.com/MichaelMure/git-bug/input"
|
2021-04-17 18:40:11 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/text"
|
2018-09-16 02:17:06 +03:00
|
|
|
)
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
type titleEditOptions struct {
|
2021-05-09 12:14:45 +03:00
|
|
|
title string
|
|
|
|
nonInteractive bool
|
2020-06-28 19:26:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTitleEditCommand() *cobra.Command {
|
|
|
|
env := newEnv()
|
|
|
|
options := titleEditOptions{}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2021-05-09 12:33:20 +03:00
|
|
|
Use: "edit [ID]",
|
|
|
|
Short: "Edit a title of a bug.",
|
|
|
|
PreRunE: loadBackendEnsureUser(env),
|
|
|
|
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
|
2020-06-28 19:26:29 +03:00
|
|
|
return runTitleEdit(env, options, args)
|
2021-05-09 12:33:20 +03:00
|
|
|
}),
|
2021-01-24 16:05:40 +03:00
|
|
|
ValidArgsFunction: completeBug(env),
|
2020-06-28 19:26:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.SortFlags = false
|
|
|
|
|
|
|
|
flags.StringVarP(&options.title, "title", "t", "",
|
|
|
|
"Provide a title to describe the issue",
|
|
|
|
)
|
2021-05-09 12:14:45 +03:00
|
|
|
flags.BoolVar(&options.nonInteractive, "non-interactive", false, "Do not ask for user input")
|
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 {
|
2020-06-28 20:09:32 +03:00
|
|
|
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 == "" {
|
2021-05-09 12:14:45 +03:00
|
|
|
if opts.nonInteractive {
|
|
|
|
env.err.Println("No title given. Use -m or -F option to specify a title. Aborting.")
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-28 19:26:29 +03:00
|
|
|
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.")
|
2018-09-26 17:28:57 +03:00
|
|
|
}
|
|
|
|
|
2021-04-17 18:40:11 +03:00
|
|
|
_, err = b.SetTitle(text.CleanupOneLine(opts.title))
|
2018-09-16 02:17:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Commit()
|
|
|
|
}
|