create.go

 1package repo
 2
 3import (
 4	"fmt"
 5
 6	"github.com/charmbracelet/soft-serve/cmd"
 7	"github.com/charmbracelet/soft-serve/pkg/access"
 8	"github.com/charmbracelet/soft-serve/pkg/backend"
 9	"github.com/charmbracelet/soft-serve/pkg/config"
10	"github.com/charmbracelet/soft-serve/pkg/proto"
11	"github.com/spf13/cobra"
12)
13
14// createCommand is the command for creating a new repository.
15func createCommand() *cobra.Command {
16	var private bool
17	var description string
18	var projectName string
19	var hidden bool
20
21	cmd := &cobra.Command{
22		Use:   "create REPOSITORY",
23		Short: "Create a new repository",
24		Args:  cobra.ExactArgs(1),
25		RunE: func(co *cobra.Command, args []string) error {
26			ctx := co.Context()
27			cfg := config.FromContext(ctx)
28			be := backend.FromContext(ctx)
29			user := proto.UserFromContext(ctx)
30			name := args[0]
31			if !cmd.CheckUserHasAccess(co, name, access.ReadWriteAccess) {
32				return proto.ErrUnauthorized
33			}
34
35			r, err := be.CreateRepository(ctx, name, user, proto.RepositoryOptions{
36				Private:     private,
37				Description: description,
38				ProjectName: projectName,
39				Hidden:      hidden,
40			})
41			if err != nil {
42				return err
43			}
44
45			cloneurl := fmt.Sprintf("%s/%s.git", cfg.SSH.PublicURL, r.Name())
46			co.PrintErrf("Created repository %s\n", r.Name())
47			co.Println(cloneurl)
48
49			return nil
50		},
51	}
52
53	cmd.Flags().BoolVarP(&private, "private", "p", false, "make the repository private")
54	cmd.Flags().StringVarP(&description, "description", "d", "", "set the repository description")
55	cmd.Flags().StringVarP(&projectName, "name", "n", "", "set the project name")
56	cmd.Flags().BoolVarP(&hidden, "hidden", "H", false, "hide the repository from the UI")
57
58	return cmd
59}