github.com/grantbow/fit@v0.7.1-0.20220916164603-1f7c88ac81e6/fitapp/Edit.go (about)

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