forked from Gitlink/gitlink-cli
146 lines
4.0 KiB
Go
146 lines
4.0 KiB
Go
package template
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/gitlink-org/gitlink-cli/shortcuts/common"
|
|
)
|
|
|
|
func Shortcuts() []*common.Shortcut {
|
|
return []*common.Shortcut{
|
|
{
|
|
Name: "list",
|
|
Description: "List project templates for an owner",
|
|
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)
|
|
},
|
|
},
|
|
{
|
|
Name: "create",
|
|
Description: "Create a project template",
|
|
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)
|
|
},
|
|
},
|
|
{
|
|
Name: "view",
|
|
Description: "View a project template",
|
|
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)
|
|
},
|
|
},
|
|
{
|
|
Name: "update",
|
|
Description: "Update a project template",
|
|
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)
|
|
},
|
|
},
|
|
{
|
|
Name: "delete",
|
|
Description: "Delete a project template",
|
|
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)
|
|
},
|
|
},
|
|
}
|
|
}
|