github.com/zaquestion/lab@v0.25.1/cmd/issue_close.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/MakeNowJust/heredoc/v2"
     8  	"github.com/rsteube/carapace"
     9  	"github.com/spf13/cobra"
    10  	"github.com/zaquestion/lab/internal/action"
    11  	lab "github.com/zaquestion/lab/internal/gitlab"
    12  )
    13  
    14  var issueCloseCmd = &cobra.Command{
    15  	Use:              "close [remote] <id>",
    16  	Aliases:          []string{"delete"},
    17  	Short:            "Close issue by ID",
    18  	Args:             cobra.MinimumNArgs(1),
    19  	PersistentPreRun: labPersistentPreRun,
    20  	Example: heredoc.Doc(`
    21  		lab issue close 1234
    22  		lab issue close origin --duplicate 123 1234
    23  		lab issue close --duplicate other-project#123 1234`),
    24  	Run: func(cmd *cobra.Command, args []string) {
    25  		rn, id, err := parseArgsRemoteAndID(args)
    26  		if err != nil {
    27  			log.Fatal(err)
    28  		}
    29  
    30  		dupID, _ := cmd.Flags().GetString("duplicate")
    31  		if dupID != "" {
    32  			if !strings.Contains(dupID, "#") {
    33  				dupID = "#" + dupID
    34  			}
    35  			err = lab.IssueDuplicate(rn, int(id), dupID)
    36  			if err != nil {
    37  				log.Fatal(err)
    38  			}
    39  			fmt.Printf("Issue #%d closed as duplicate of %s\n", id, dupID)
    40  		} else {
    41  			err = lab.IssueClose(rn, int(id))
    42  			if err != nil {
    43  				log.Fatal(err)
    44  			}
    45  			fmt.Printf("Issue #%d closed\n", id)
    46  		}
    47  	},
    48  }
    49  
    50  func init() {
    51  	issueCloseCmd.Flags().StringP("duplicate", "", "", "mark as duplicate of another issue")
    52  	issueCmd.AddCommand(issueCloseCmd)
    53  	carapace.Gen(issueCloseCmd).PositionalCompletion(
    54  		action.Remotes(),
    55  		action.Issues(issueList),
    56  	)
    57  }