106 lines
2.7 KiB
Go
106 lines
2.7 KiB
Go
package tenants
|
|
|
|
import (
|
|
"time"
|
|
|
|
"backend/modules/commands/tenant"
|
|
"backend/modules/medias"
|
|
"backend/modules/users"
|
|
"backend/providers/app"
|
|
"backend/providers/hashids"
|
|
"backend/providers/postgres"
|
|
"backend/providers/storage"
|
|
|
|
"git.ipao.vip/rogeecn/atom"
|
|
"git.ipao.vip/rogeecn/atom/container"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/cast"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func defaultProviders(providers ...container.ProviderContainer) container.Providers {
|
|
return append(container.Providers{
|
|
app.DefaultProvider(),
|
|
storage.DefaultProvider(),
|
|
postgres.DefaultProvider(),
|
|
hashids.DefaultProvider(),
|
|
}, providers...)
|
|
}
|
|
|
|
func Command() atom.Option {
|
|
return atom.Command(
|
|
atom.Name("tenants"),
|
|
atom.Short("租户相关操作"),
|
|
atom.Example("create Name --slug [slug] --contact [wechat_contact]"),
|
|
atom.Command(
|
|
atom.Name("create"),
|
|
atom.Providers(defaultProviders().With(
|
|
medias.Provide,
|
|
users.Provide,
|
|
tenant.Provide,
|
|
)),
|
|
atom.Arguments(func(cmd *cobra.Command) {
|
|
cmd.Flags().String("slug", "", "slug")
|
|
cmd.Flags().String("contact", "", "contact")
|
|
}),
|
|
atom.RunE(func(cmd *cobra.Command, args []string) error {
|
|
return container.Container.Invoke(func(t *tenant.Create) error {
|
|
slug := cmd.Flag("slug").Value.String()
|
|
return t.RunE(args[0], slug)
|
|
})
|
|
}),
|
|
),
|
|
atom.Command(
|
|
atom.Name("expire"),
|
|
atom.Short("设置用户的过期时间"),
|
|
atom.Example("expire [slug] [2024-01-01]"),
|
|
atom.Providers(defaultProviders().With(
|
|
medias.Provide,
|
|
users.Provide,
|
|
tenant.Provide,
|
|
)),
|
|
atom.Arguments(func(cmd *cobra.Command) {
|
|
}),
|
|
atom.RunE(func(cmd *cobra.Command, args []string) error {
|
|
return container.Container.Invoke(func(t *tenant.Expire) error {
|
|
slug := args[0]
|
|
expireStr := args[1] // format 2024-01-01
|
|
// parse expire string as time.Time
|
|
expire, err := time.Parse("2006-01-02", expireStr)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "parse expire time failed: %s", expireStr)
|
|
}
|
|
|
|
return t.RunE(slug, expire)
|
|
})
|
|
}),
|
|
),
|
|
|
|
atom.Command(
|
|
atom.Name("bind"),
|
|
atom.Short("设置租户的管理员ID"),
|
|
atom.Example("bind slug user_id"),
|
|
atom.Providers(defaultProviders().With(
|
|
medias.Provide,
|
|
users.Provide,
|
|
tenant.Provide,
|
|
)),
|
|
atom.Arguments(func(cmd *cobra.Command) {
|
|
}),
|
|
atom.RunE(func(cmd *cobra.Command, args []string) error {
|
|
return container.Container.Invoke(func(t *tenant.Bind) error {
|
|
slug := args[0]
|
|
userID, err := cast.ToInt64E(args[1]) // format 2024-01-01
|
|
if err != nil {
|
|
return errors.Wrapf(err, "parse user_id failed: %s", args[1])
|
|
}
|
|
|
|
// parse expire string as time.Time
|
|
|
|
return t.RunE(slug, userID)
|
|
})
|
|
}),
|
|
),
|
|
)
|
|
}
|