private.go

 1package cmd
 2
 3import (
 4	"strconv"
 5	"strings"
 6
 7	"github.com/charmbracelet/soft-serve/pkg/backend"
 8	"github.com/spf13/cobra"
 9)
10
11func privateCommand() *cobra.Command {
12	cmd := &cobra.Command{
13		Use:               "private REPOSITORY [true|false]",
14		Short:             "Set or get a repository private property",
15		Args:              cobra.RangeArgs(1, 2),
16		PersistentPreRunE: checkIfReadable,
17		RunE: func(cmd *cobra.Command, args []string) error {
18			ctx := cmd.Context()
19			be := backend.FromContext(ctx)
20			rn := strings.TrimSuffix(args[0], ".git")
21
22			switch len(args) {
23			case 1:
24				isPrivate, err := be.IsPrivate(ctx, rn)
25				if err != nil {
26					return err //nolint:wrapcheck
27				}
28
29				cmd.Println(isPrivate)
30			case 2:
31				isPrivate, err := strconv.ParseBool(args[1])
32				if err != nil {
33					return err //nolint:wrapcheck
34				}
35				if err := checkIfCollab(cmd, args); err != nil {
36					return err
37				}
38				if err := be.SetPrivate(ctx, rn, isPrivate); err != nil {
39					return err //nolint:wrapcheck
40				}
41			}
42			return nil
43		},
44	}
45
46	return cmd
47}