gitlink-cli/shortcuts/template/template.go

151 lines
4.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package template
import (
"fmt"
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
)
func Shortcuts() []*common.Shortcut {
return []*common.Shortcut{
// +list列出某拥有者的项目模板
{
Name: "list",
Description: "列出某拥有者的项目模板",
Flags: []common.Flag{
{Name: "owner", Short: "o", Usage: "Owner (user or organization)", Required: true},
},
Run: func(ctx *common.RuntimeContext) error {
owner, err := ctx.RequireArg("owner")
if err != nil {
return err
}
env, err := ctx.CallAPI("GET", fmt.Sprintf("/v1/%s/project_templates", owner), nil)
if err != nil {
return err
}
return ctx.Output(env)
},
},
// +create创建项目模板
{
Name: "create",
Description: "创建项目模板",
Flags: []common.Flag{
{Name: "owner", Short: "o", Usage: "Owner (user or organization)", Required: true},
{Name: "name", Short: "n", Usage: "Template name", Required: true},
{Name: "description", Short: "d", Usage: "Template description"},
},
Run: func(ctx *common.RuntimeContext) error {
owner, err := ctx.RequireArg("owner")
if err != nil {
return err
}
name, err := ctx.RequireArg("name")
if err != nil {
return err
}
payload := map[string]interface{}{
"name": name,
}
if desc := ctx.Arg("description"); desc != "" {
payload["description"] = desc
}
env, err := ctx.CallAPI("POST", fmt.Sprintf("/v1/%s/project_templates", owner), payload)
if err != nil {
return err
}
return ctx.Output(env)
},
},
// +view查看项目模板
{
Name: "view",
Description: "查看项目模板",
Flags: []common.Flag{
{Name: "owner", Short: "o", Usage: "Owner (user or organization)", Required: true},
{Name: "id", Short: "i", Usage: "Template ID", Required: true},
{Name: "repo", Short: "r", Usage: "Repository name (optional, for repo-scoped templates)"},
},
Run: func(ctx *common.RuntimeContext) error {
owner, err := ctx.RequireArg("owner")
if err != nil {
return err
}
id, err := ctx.RequireArg("id")
if err != nil {
return err
}
var path string
if repo := ctx.Arg("repo"); repo != "" {
path = fmt.Sprintf("/v1/%s/%s/project_templates/%s", owner, repo, id)
} else {
path = fmt.Sprintf("/v1/%s/project_templates/%s", owner, id)
}
env, err := ctx.CallAPI("GET", path, nil)
if err != nil {
return err
}
return ctx.Output(env)
},
},
// +update更新项目模板
{
Name: "update",
Description: "更新项目模板",
Flags: []common.Flag{
{Name: "owner", Short: "o", Usage: "Owner (user or organization)", Required: true},
{Name: "id", Short: "i", Usage: "Template ID", Required: true},
{Name: "name", Short: "n", Usage: "New template name"},
{Name: "description", Short: "d", Usage: "New template description"},
},
Run: func(ctx *common.RuntimeContext) error {
owner, err := ctx.RequireArg("owner")
if err != nil {
return err
}
id, err := ctx.RequireArg("id")
if err != nil {
return err
}
payload := map[string]interface{}{}
if name := ctx.Arg("name"); name != "" {
payload["name"] = name
}
if desc := ctx.Arg("description"); desc != "" {
payload["description"] = desc
}
env, err := ctx.CallAPI("PUT", fmt.Sprintf("/v1/%s/project_templates/%s", owner, id), payload)
if err != nil {
return err
}
return ctx.Output(env)
},
},
// +delete删除项目模板
{
Name: "delete",
Description: "删除项目模板",
Flags: []common.Flag{
{Name: "owner", Short: "o", Usage: "Owner (user or organization)", Required: true},
{Name: "id", Short: "i", Usage: "Template ID", Required: true},
},
Run: func(ctx *common.RuntimeContext) error {
owner, err := ctx.RequireArg("owner")
if err != nil {
return err
}
id, err := ctx.RequireArg("id")
if err != nil {
return err
}
env, err := ctx.CallAPI("DELETE", fmt.Sprintf("/v1/%s/project_templates/%s", owner, id), nil)
if err != nil {
return err
}
return ctx.Output(env)
},
},
}
}