list.go

 1package cmd
 2
 3import (
 4	"github.com/charmbracelet/soft-serve/pkg/access"
 5	"github.com/charmbracelet/soft-serve/pkg/backend"
 6	"github.com/charmbracelet/soft-serve/pkg/sshutils"
 7	"github.com/spf13/cobra"
 8)
 9
10// listCommand returns a command that list file or directory at path.
11func listCommand() *cobra.Command {
12	var all bool
13
14	listCmd := &cobra.Command{
15		Use:     "list",
16		Aliases: []string{"ls"},
17		Short:   "List repositories",
18		Args:    cobra.NoArgs,
19		RunE: func(cmd *cobra.Command, _ []string) error {
20			ctx := cmd.Context()
21			be := backend.FromContext(ctx)
22			pk := sshutils.PublicKeyFromContext(ctx)
23			repos, err := be.Repositories(ctx)
24			if err != nil {
25				return err //nolint:wrapcheck
26			}
27			for _, r := range repos {
28				if be.AccessLevelByPublicKey(ctx, r.Name(), pk) >= access.ReadOnlyAccess {
29					if !r.IsHidden() || all {
30						cmd.Println(r.Name())
31					}
32				}
33			}
34			return nil
35		},
36	}
37
38	listCmd.Flags().BoolVarP(&all, "all", "a", false, "List all repositories")
39
40	return listCmd
41}