aboutsummaryrefslogtreecommitdiffstats
path: root/commands/new.go
blob: d2e1ed59cdc7d65ec10c45db25764cd18ae0d6d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package commands

import (
	"errors"
	"flag"
	"fmt"
	"github.com/MichaelMure/git-bug/bug"
	"github.com/MichaelMure/git-bug/bug/operations"
	"github.com/MichaelMure/git-bug/commands/input"
	"github.com/MichaelMure/git-bug/repository"
)

var newFlagSet = flag.NewFlagSet("new", flag.ExitOnError)

var (
	newMessageFile = newFlagSet.String("F", "", "Take the message from the given file. Use - to read the message from the standard input")
	newMessage     = newFlagSet.String("m", "", "Provide a message to describe the issue")
)

func RunNewBug(repo repository.Repo, args []string) error {
	newFlagSet.Parse(args)
	args = newFlagSet.Args()

	var err error

	if len(args) == 0 {
		return errors.New("No title provided")
	}
	if len(args) > 1 {
		return errors.New("Only accepting one title is supported")
	}

	title := args[0]

	if *newMessageFile != "" && *newMessage == "" {
		*newMessage, err = input.FromFile(*newMessageFile)
		if err != nil {
			return err
		}
	}
	if *newMessageFile == "" && *newMessage == "" {
		*newMessage, err = input.LaunchEditor(repo, messageFilename)
		if err != nil {
			return err
		}
	}

	// Note: this is very primitive for now
	author, err := bug.GetUser(repo)
	if err != nil {
		return err
	}

	newbug, err := bug.NewBug()
	if err != nil {
		return err
	}

	createOp := operations.NewCreateOp(author, title, *newMessage)

	newbug.Append(createOp)

	err = newbug.Commit(repo)

	return err

}

var newCmd = &Command{
	Usage: func(arg0 string) {
		fmt.Printf("Usage: %s new <title> [<option>...]\n\nOptions:\n", arg0)
		newFlagSet.PrintDefaults()
	},
	RunMethod: RunNewBug,
}