2021-03-14 13:08:50 +13:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2023-01-13 12:39:30 +13:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-03-14 13:08:50 +13:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
2021-03-17 03:28:40 +13:00
|
|
|
|
2022-07-14 10:58:22 +12:00
|
|
|
"github.com/demodesk/neko/internal/member/file"
|
2023-01-13 12:00:09 +13:00
|
|
|
"github.com/demodesk/neko/internal/member/multiuser"
|
2022-07-14 10:58:22 +12:00
|
|
|
"github.com/demodesk/neko/internal/member/object"
|
2023-01-13 12:39:30 +13:00
|
|
|
"github.com/demodesk/neko/pkg/utils"
|
2021-03-14 13:08:50 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
type Member struct {
|
2021-03-17 03:24:58 +13:00
|
|
|
Provider string
|
|
|
|
|
2021-03-17 03:28:40 +13:00
|
|
|
// providers
|
2023-01-13 12:00:09 +13:00
|
|
|
File file.Config
|
|
|
|
Object object.Config
|
|
|
|
Multiuser multiuser.Config
|
2021-03-14 13:08:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func (Member) Init(cmd *cobra.Command) error {
|
2023-01-13 12:59:00 +13:00
|
|
|
cmd.PersistentFlags().String("member.provider", "multiuser", "choose member provider")
|
2021-03-17 03:24:58 +13:00
|
|
|
if err := viper.BindPFlag("member.provider", cmd.PersistentFlags().Lookup("member.provider")); err != nil {
|
2021-03-14 13:08:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-17 03:24:58 +13:00
|
|
|
// file provider
|
|
|
|
cmd.PersistentFlags().String("member.file.path", "", "member file provider: storage path")
|
|
|
|
if err := viper.BindPFlag("member.file.path", cmd.PersistentFlags().Lookup("member.file.path")); err != nil {
|
2021-03-14 13:08:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-17 03:24:58 +13:00
|
|
|
// object provider
|
2023-01-13 12:39:30 +13:00
|
|
|
cmd.PersistentFlags().String("member.object.users", "[]", "member object provider: users in JSON format")
|
|
|
|
if err := viper.BindPFlag("member.object.users", cmd.PersistentFlags().Lookup("member.object.users")); err != nil {
|
2021-03-14 13:08:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-13 12:00:09 +13:00
|
|
|
// multiuser provider
|
2023-01-13 12:59:00 +13:00
|
|
|
cmd.PersistentFlags().String("member.multiuser.user_password", "neko", "member multiuser provider: user password")
|
2023-01-13 12:00:09 +13:00
|
|
|
if err := viper.BindPFlag("member.multiuser.user_password", cmd.PersistentFlags().Lookup("member.multiuser.user_password")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-13 12:59:00 +13:00
|
|
|
cmd.PersistentFlags().String("member.multiuser.admin_password", "admin", "member multiuser provider: admin password")
|
2023-01-13 12:00:09 +13:00
|
|
|
if err := viper.BindPFlag("member.multiuser.admin_password", cmd.PersistentFlags().Lookup("member.multiuser.admin_password")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-14 13:08:50 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Member) Set() {
|
2021-03-17 03:24:58 +13:00
|
|
|
s.Provider = viper.GetString("member.provider")
|
|
|
|
|
|
|
|
// file provider
|
2021-03-17 03:28:40 +13:00
|
|
|
s.File.Path = viper.GetString("member.file.path")
|
2021-03-17 03:24:58 +13:00
|
|
|
|
|
|
|
// object provider
|
2023-01-13 12:39:30 +13:00
|
|
|
if err := viper.UnmarshalKey("member.object.users", &s.Object.Users, viper.DecodeHook(
|
|
|
|
utils.JsonStringAutoDecode(s.Object.Users),
|
|
|
|
)); err != nil {
|
|
|
|
log.Warn().Err(err).Msgf("unable to parse member object users")
|
|
|
|
}
|
2023-01-13 12:00:09 +13:00
|
|
|
|
|
|
|
// multiuser provider
|
|
|
|
s.Multiuser.UserPassword = viper.GetString("member.multiuser.user_password")
|
|
|
|
s.Multiuser.AdminPassword = viper.GetString("member.multiuser.admin_password")
|
2021-03-14 13:08:50 +13:00
|
|
|
}
|