File: create_from_template.go

package info (click to toggle)
tea-cli 0.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,364 kB
  • sloc: makefile: 120; sh: 17
file content (121 lines) | stat: -rw-r--r-- 3,043 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package repos

import (
	"fmt"

	"code.gitea.io/tea/cmd/flags"
	"code.gitea.io/tea/modules/context"
	"code.gitea.io/tea/modules/print"
	"code.gitea.io/tea/modules/utils"

	"code.gitea.io/sdk/gitea"
	"github.com/urfave/cli/v2"
)

// CmdRepoCreateFromTemplate represents a sub command of repos to generate one from a template repo
var CmdRepoCreateFromTemplate = cli.Command{
	Name:        "create-from-template",
	Aliases:     []string{"ct"},
	Usage:       "Create a repository based on an existing template",
	Description: "Create a repository based on an existing template",
	Action:      runRepoCreateFromTemplate,
	Flags: append([]cli.Flag{
		&cli.StringFlag{
			Name:     "template",
			Aliases:  []string{"t"},
			Required: true,
			Usage:    "source template to copy from",
		},
		&cli.StringFlag{
			Name:     "name",
			Aliases:  []string{"n"},
			Required: true,
			Usage:    "name of new repo",
		},
		&cli.StringFlag{
			Name:    "owner",
			Aliases: []string{"O"},
			Usage:   "name of repo owner",
		},
		&cli.BoolFlag{
			Name:  "private",
			Usage: "make new repo private",
		},
		&cli.StringFlag{
			Name:    "description",
			Aliases: []string{"desc"},
			Usage:   "add custom description to repo",
		},
		&cli.BoolFlag{
			Name:  "content",
			Value: true,
			Usage: "copy git content from template",
		},
		&cli.BoolFlag{
			Name:  "githooks",
			Value: true,
			Usage: "copy git hooks from template",
		},
		&cli.BoolFlag{
			Name:  "avatar",
			Value: true,
			Usage: "copy repo avatar from template",
		},
		&cli.BoolFlag{
			Name:  "labels",
			Value: true,
			Usage: "copy repo labels from template",
		},
		&cli.BoolFlag{
			Name:  "topics",
			Value: true,
			Usage: "copy topics from template",
		},
		&cli.BoolFlag{
			Name:  "webhooks",
			Usage: "copy webhooks from template",
		},
	}, flags.LoginOutputFlags...),
}

func runRepoCreateFromTemplate(cmd *cli.Context) error {
	ctx := context.InitCommand(cmd)
	client := ctx.Login.Client()

	templateOwner, templateRepo := utils.GetOwnerAndRepo(ctx.String("template"), ctx.Login.User)
	owner := ctx.Login.User
	if ctx.IsSet("owner") {
		owner = ctx.String("owner")
	}

	opts := gitea.CreateRepoFromTemplateOption{
		Name:        ctx.String("name"),
		Owner:       owner,
		Description: ctx.String("description"),
		Private:     ctx.Bool("private"),
		GitContent:  ctx.Bool("content"),
		GitHooks:    ctx.Bool("githooks"),
		Avatar:      ctx.Bool("avatar"),
		Labels:      ctx.Bool("labels"),
		Topics:      ctx.Bool("topics"),
		Webhooks:    ctx.Bool("webhooks"),
	}

	repo, _, err := client.CreateRepoFromTemplate(templateOwner, templateRepo, opts)
	if err != nil {
		return err
	}

	topics, _, err := client.ListRepoTopics(repo.Owner.UserName, repo.Name, gitea.ListRepoTopicsOptions{})
	if err != nil {
		return err
	}
	print.RepoDetails(repo, topics)

	fmt.Printf("%s\n", repo.HTMLURL)
	return nil
}