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

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/MakeNowJust/heredoc/v2"
     6  	"strconv"
     7  
     8  	"github.com/spf13/cobra"
     9  	"github.com/xanzy/go-gitlab"
    10  	lab "github.com/zaquestion/lab/internal/gitlab"
    11  )
    12  
    13  var projectListConfig struct {
    14  	All        bool
    15  	Owned      bool
    16  	Membership bool
    17  	Starred    bool
    18  	Number     string
    19  }
    20  
    21  var projectListCmd = &cobra.Command{
    22  	Use:     "list [search]",
    23  	Aliases: []string{"ls", "search"},
    24  	Short:   "List your projects",
    25  	Example: heredoc.Doc(`
    26  		lab project list -a
    27  		lab project list -m
    28  		lab project list --member
    29  		lab project list --starred
    30  		lab project list -n 10`),
    31  	PersistentPreRun: labPersistentPreRun,
    32  	Run: func(cmd *cobra.Command, args []string) {
    33  		search, _, err := parseArgsStringAndID(args)
    34  		if err != nil {
    35  			log.Fatal(err)
    36  		}
    37  
    38  		num, err := strconv.Atoi(projectListConfig.Number)
    39  		if projectListConfig.All || (err != nil) {
    40  			num = -1
    41  		}
    42  
    43  		opt := gitlab.ListProjectsOptions{
    44  			ListOptions: gitlab.ListOptions{
    45  				PerPage: num,
    46  			},
    47  			Simple:     gitlab.Bool(true),
    48  			OrderBy:    gitlab.String("id"),
    49  			Sort:       gitlab.String("asc"),
    50  			Owned:      gitlab.Bool(projectListConfig.Owned),
    51  			Membership: gitlab.Bool(projectListConfig.Membership),
    52  			Starred:    gitlab.Bool(projectListConfig.Starred),
    53  			Search:     gitlab.String(search),
    54  		}
    55  		projects, err := lab.ProjectList(opt, num)
    56  		if err != nil {
    57  			log.Fatal(err)
    58  		}
    59  
    60  		pager := newPager(cmd.Flags())
    61  		defer pager.Close()
    62  
    63  		for _, p := range projects {
    64  			fmt.Println(p.PathWithNamespace)
    65  		}
    66  	},
    67  }
    68  
    69  func init() {
    70  	projectCmd.AddCommand(projectListCmd)
    71  	projectListCmd.Flags().BoolVarP(&projectListConfig.All, "all", "a", false, "list all projects on the instance")
    72  	projectListCmd.Flags().BoolVarP(&projectListConfig.Owned, "mine", "m", false, "limit by your projects")
    73  	projectListCmd.Flags().BoolVar(&projectListConfig.Membership, "member", false, "limit by projects which you are a member")
    74  	projectListCmd.Flags().BoolVar(&projectListConfig.Starred, "starred", false, "limit by your starred projects")
    75  	projectListCmd.Flags().StringVarP(&projectListConfig.Number, "number", "n", "100", "Number of projects to return")
    76  	projectListCmd.Flags().SortFlags = false
    77  }