~xenrox/srhtctl

2a7398feba83136f507784fe0e8735685abbab8e — Thorben Günther 2 years ago e33f05f
lists: Add ability to print patchsets
2 files changed, 169 insertions(+), 0 deletions(-)

M api/lists.go
A cmd/lists.go
M api/lists.go => api/lists.go +120 -0
@@ 1,5 1,21 @@
package api

import (
	"fmt"
	"strings"

	"git.xenrox.net/~xenrox/srhtctl/config"
)

// ListName is the name of a mailing list
var ListName string

// PatchStatus is the status of a patchset
var PatchStatus string

// PatchPrefix is the prefix of a patchset
var PatchPrefix string

type emailStruct struct {
	ID        int          `json:"id"`
	Created   string       `json:"created"`


@@ 32,6 48,13 @@ type patchStruct struct {
	MessageID string  `json:"message_id"`
}

type patchPagerStruct struct {
	Next           *int           `json:"next,string"`
	Results        []*patchStruct `json:"results"`
	Total          int            `json:"total"`
	ResultsPerPage int            `json:"results_per_page"`
}

type listPermissions struct {
	NonSubscriber []string `json:"nonsubscriber"`
	Subscriber    []string `json:"subscriber"`


@@ 54,3 77,100 @@ type listsPagerStruct struct {
	Total          int            `json:"total"`
	ResultsPerPage int            `json:"results_per_page"`
}

// PrintPatchsets prints out patchsets
func PrintPatchsets(args []string) error {
	var patches patchPagerStruct

	if ListName != "" {
		err := getPatchsets(&patches, ListName)
		if err != nil {
			return err
		}

		for _, patch := range patches.Results {
			fmt.Print(patch.filterByPrefix().filterByStatus())
		}
		return nil
	}

	var lists listsPagerStruct
	err := getLists(&lists)
	if err != nil {
		return err
	}

	for _, list := range lists.Results {
		err = getPatchsets(&patches, list.Name)
		if err != nil {
			return err
		}

		for i, patch := range patches.Results {
			if i == 0 {
				fmt.Printf("List %s:\n\n", list.Name)
			}
			fmt.Print(patch.filterByPrefix().filterByStatus())
		}
	}

	return nil
}

func getPatchsets(response *patchPagerStruct, listName string) error {
	url := fmt.Sprintf("%s/api/lists/%s/patchsets", config.GetURL("lists"), listName)
	err := Request(url, "GET", "", &response)
	if err != nil {
		return err
	}
	return nil
}

func getLists(response *listsPagerStruct) error {
	url := fmt.Sprintf("%s/api/lists", config.GetURL("lists"))
	err := Request(url, "GET", "", &response)
	if err != nil {
		return err
	}
	return nil
}

func (patch patchStruct) String() string {
	if patch == (patchStruct{}) {
		return ""
	}

	prefix := patch.Prefix

	var version string
	if patch.Version != 1 {
		version = fmt.Sprintf("v%d", patch.Version)
	}

	var suffix string
	if prefix != "" || version != "" {
		suffix = fmt.Sprintf("%s %s", prefix, version)
		suffix = fmt.Sprintf(" [%s]", strings.TrimSpace(suffix))
	}

	str := fmt.Sprintf("Patchset %d%s: %s\n", patch.ID, suffix, patch.Subject)
	str += fmt.Sprintf("Submitter: %s\n", patch.Submitter)
	str += fmt.Sprintf("Status: %s\n", patch.Status)

	str += "\n"
	return str
}

func (patch patchStruct) filterByStatus() patchStruct {
	if PatchStatus == "all" || PatchStatus == patch.Status {
		return patch
	}
	return patchStruct{}
}

func (patch patchStruct) filterByPrefix() patchStruct {
	if PatchPrefix == "" || PatchPrefix == patch.Prefix {
		return patch
	}
	return patchStruct{}
}

A cmd/lists.go => cmd/lists.go +49 -0
@@ 0,0 1,49 @@
package cmd

import (
	"git.xenrox.net/~xenrox/srhtctl/api"
	"git.xenrox.net/~xenrox/srhtctl/helpers/errorhelper"
	"github.com/spf13/cobra"
)

var listsCmd = &cobra.Command{
	Use:     "lists",
	Short:   "Use the srht lists API",
	Aliases: []string{"list"},
	Run: func(cmd *cobra.Command, args []string) {
	},
}

var patchCmd = &cobra.Command{
	Use:   "patch",
	Short: "Work on patches/patchsets",
	Run: func(cmd *cobra.Command, args []string) {
	},
}

var patchListCmd = &cobra.Command{
	Use:   "list",
	Short: "List patchsets",
	Run: func(cmd *cobra.Command, args []string) {
		err := api.PrintPatchsets(args)
		errorhelper.ExitError(err)
	},
}

func init() {
	rootCmd.AddCommand(listsCmd)

	listsCmd.AddCommand(patchCmd)

	patchCmd.AddCommand(patchListCmd)
	patchListCmd.Flags().StringVarP(&api.ListName, "list", "l", "", "List name")
	patchListCmd.RegisterFlagCompletionFunc("list", completeNoFiles)
	patchListCmd.Flags().StringVarP(&api.PatchStatus, "status", "s", "proposed", "Patchset status")
	patchListCmd.RegisterFlagCompletionFunc("status", func(cmd *cobra.Command, args []string, toComplete string) (
		[]string, cobra.ShellCompDirective) {
		return []string{"proposed", "needs_revision", "superseded", "approved", "rejected", "applied", "all"},
			cobra.ShellCompDirectiveNoFileComp
	})
	patchListCmd.Flags().StringVarP(&api.PatchPrefix, "prefix", "p", "", "Patchset prefix")
	patchListCmd.RegisterFlagCompletionFunc("prefix", completeNoFiles)
}