github.com/driusan/bug@v0.3.2-0.20190306121946-d7f4e7f33fea/bugapp/Edit.go (about)

     1  package bugapp
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/driusan/bug/bugs"
     6  	"log"
     7  	"os"
     8  	"os/exec"
     9  	"strings"
    10  )
    11  
    12  func Edit(args ArgumentList) {
    13  
    14  	var file, bugID string
    15  	switch len(args) {
    16  	case 1:
    17  		// If there's only 1 argument, it's an issue
    18  		// identifier and it's editing the description.
    19  		// So set the variables and fallthrough to the
    20  		// 2 argument (editing a specific fieldname)
    21  		// case
    22  		bugID = args[0]
    23  		file = "Description"
    24  		fallthrough
    25  	case 2:
    26  		// If there's exactly 2 arguments, idx and
    27  		// file haven't been set by the first case
    28  		// statement, so set them, but everything else
    29  		// is the same
    30  		if len(args) == 2 {
    31  			bugID = args[1]
    32  			file = args[0]
    33  		}
    34  
    35  		b, err := bugs.LoadBugByHeuristic(bugID)
    36  		if err != nil {
    37  			fmt.Printf("Invalid BugID %s\n", bugID)
    38  			return
    39  		}
    40  
    41  		dir := b.GetDirectory()
    42  
    43  		switch title := strings.Title(file); title {
    44  		case "Milestone", "Status", "Priority", "Identifier":
    45  			file = title
    46  		}
    47  		fmt.Printf("Launching in %s/%s", dir, file)
    48  		cmd := exec.Command(getEditor(), string(dir)+"/"+file)
    49  
    50  		cmd.Stdin = os.Stdin
    51  		cmd.Stdout = os.Stdout
    52  		cmd.Stderr = os.Stderr
    53  
    54  		err = cmd.Run()
    55  		if err != nil {
    56  			log.Fatal(err)
    57  		}
    58  	default:
    59  		fmt.Printf("Usage: %s edit [fieldname] BugID\n", os.Args[0])
    60  		fmt.Printf("\nNo BugID specified\n")
    61  	}
    62  }