private.go

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