diff --git a/client/client.go b/client/client.go index e7c51960e..9018290c5 100644 --- a/client/client.go +++ b/client/client.go @@ -52,22 +52,22 @@ type Client interface { // Client is the bscp client type client struct { pairs map[string]string - opts ClientOptions + opts Options fingerPrint sfs.FingerPrint watcher *watch.Watcher upstream upstream.Upstream } // New return a bscp client instance -func New(opts ...ClientOption) (Client, error) { - clientOpt := &ClientOptions{} +func New(opts ...Option) (Client, error) { + clientOpt := &Options{} fp, err := sfs.GetFingerPrint() if err != nil { return nil, fmt.Errorf("get instance fingerprint failed, err: %s", err.Error()) } logger.Info("instance fingerprint", slog.String("fingerprint", fp.Encode())) - clientOpt.Fingerprint = fp.Encode() - clientOpt.UID = clientOpt.Fingerprint + clientOpt.fingerprint = fp.Encode() + clientOpt.uid = clientOpt.fingerprint for _, opt := range opts { if e := opt(clientOpt); e != nil { return nil, e @@ -79,8 +79,8 @@ func New(opts ...ClientOption) (Client, error) { pairs[constant.SideUserKey] = "TODO-USER" // add finger printer mh := sfs.SidecarMetaHeader{ - BizID: clientOpt.BizID, - Fingerprint: clientOpt.Fingerprint, + BizID: clientOpt.bizID, + Fingerprint: clientOpt.fingerprint, } mhBytes, err := json.Marshal(mh) if err != nil { @@ -89,9 +89,9 @@ func New(opts ...ClientOption) (Client, error) { pairs[constant.SidecarMetaKey] = string(mhBytes) // prepare upstream u, err := upstream.New( - upstream.WithFeedAddrs(clientOpt.FeedAddrs), - upstream.WithDialTimeoutMS(clientOpt.DialTimeoutMS), - upstream.WithBizID(clientOpt.BizID)) + upstream.WithFeedAddrs(clientOpt.feedAddrs), + upstream.WithDialTimeoutMS(clientOpt.dialTimeoutMS), + upstream.WithBizID(clientOpt.bizID)) if err != nil { return nil, fmt.Errorf("init upstream client failed, err: %s", err.Error()) } @@ -106,7 +106,7 @@ func New(opts ...ClientOption) (Client, error) { msg := &pbfs.HandshakeMessage{ ApiVersion: sfs.CurrentAPIVersion, Spec: &pbfs.SidecarSpec{ - BizId: clientOpt.BizID, + BizId: clientOpt.bizID, Version: c.upstream.Version(), }, } @@ -119,16 +119,16 @@ func New(opts ...ClientOption) (Client, error) { if err != nil { return nil, fmt.Errorf("decode handshake payload failed, err: %s, rid: %s", err.Error(), vas.Rid) } - err = downloader.Init(vas, clientOpt.BizID, clientOpt.Token, u, pl.RuntimeOption.RepositoryTLS) + err = downloader.Init(vas, clientOpt.bizID, clientOpt.token, u, pl.RuntimeOption.RepositoryTLS) if err != nil { return nil, fmt.Errorf("init downloader failed, err: %s", err.Error()) } - if clientOpt.UseFileCache { - cache.Init(true, clientOpt.FileCacheDir) + if clientOpt.useFileCache { + cache.Init(true, clientOpt.fileCacheDir) } watcher, err := watch.New(u, watch.WatchOptions{ - BizID: clientOpt.BizID, - Labels: clientOpt.Labels, + BizID: clientOpt.bizID, + Labels: clientOpt.labels, Fingerprint: fp.Encode(), }) if err != nil { @@ -156,7 +156,7 @@ func (c *client) StopWatch() { // ResetLabels reset bscp client labels, if key conflict, app value will overwrite client value func (c *client) ResetLabels(labels map[string]string) { - c.opts.Labels = labels + c.opts.labels = labels for _, subscriber := range c.watcher.Subscribers() { subscriber.ResetLabels(labels) } @@ -173,17 +173,17 @@ func (c *client) PullFiles(app string, opts ...types.AppOption) (*types.Release, vas, _ := c.buildVas() req := &pbfs.PullAppFileMetaReq{ ApiVersion: sfs.CurrentAPIVersion, - BizId: c.opts.BizID, + BizId: c.opts.bizID, AppMeta: &pbfs.AppMeta{ App: app, - Labels: c.opts.Labels, - Uid: c.opts.UID, + Labels: c.opts.labels, + Uid: c.opts.uid, }, - Token: c.opts.Token, + Token: c.opts.token, Key: option.Key, } // merge labels, if key conflict, app value will overwrite client value - req.AppMeta.Labels = util.MergeLabels(c.opts.Labels, option.Labels) + req.AppMeta.Labels = util.MergeLabels(c.opts.labels, option.Labels) // reset uid if option.UID != "" { req.AppMeta.Uid = option.UID @@ -227,16 +227,16 @@ func (c *client) Get(app string, key string, opts ...types.AppOption) (string, e vas, _ := c.buildVas() req := &pbfs.GetKvValueReq{ ApiVersion: sfs.CurrentAPIVersion, - BizId: c.opts.BizID, + BizId: c.opts.bizID, AppMeta: &pbfs.AppMeta{ App: app, - Labels: c.opts.Labels, - Uid: c.opts.UID, + Labels: c.opts.labels, + Uid: c.opts.uid, }, - Token: c.opts.Token, + Token: c.opts.token, Key: key, } - req.AppMeta.Labels = util.MergeLabels(c.opts.Labels, option.Labels) + req.AppMeta.Labels = util.MergeLabels(c.opts.labels, option.Labels) // reset uid if option.UID != "" { req.AppMeta.Uid = option.UID diff --git a/client/options.go b/client/options.go index 16bd823ac..74dfa7e9a 100644 --- a/client/options.go +++ b/client/options.go @@ -12,94 +12,77 @@ package client -// ClientOptions options for bscp sdk client -type ClientOptions struct { +// Options options for bscp sdk +type Options struct { // FeedAddr BSCP feed_server address - FeedAddrs []string + feedAddrs []string // BizID BSCP business id - BizID uint32 + bizID uint32 // Labels instance labels - Labels map[string]string - // Version SDK version - Version string + labels map[string]string // Fingerprint sdk fingerprint - Fingerprint string + fingerprint string // UID sdk uid - UID string + uid string // UseFileCache use file cache - UseFileCache bool + useFileCache bool // FileCacheDir file cache directory - FileCacheDir string + fileCacheDir string // DialTimeoutMS dial upstream timeout in millisecond - DialTimeoutMS int64 + dialTimeoutMS int64 // Token sdk token - Token string + token string } -// ClientOption setter for bscp sdk options -type ClientOption func(*ClientOptions) error +// Option setter for bscp sdk options +type Option func(*Options) error -// FeedAddrs set feed_server addresses -func FeedAddrs(addrs []string) ClientOption { +// WithFeedAddrs set feed_server addresses +func WithFeedAddrs(addrs []string) Option { // TODO: validate Address - return func(o *ClientOptions) error { - o.FeedAddrs = addrs + return func(o *Options) error { + o.feedAddrs = addrs return nil } } -// BizID set bscp business id -func BizID(id uint32) ClientOption { - return func(o *ClientOptions) error { - o.BizID = id - return nil - } -} - -// Labels set instance labels -func Labels(labels map[string]string) ClientOption { - return func(o *ClientOptions) error { - o.Labels = labels - return nil - } -} - -// UID set sdk uid -func UID(uid string) ClientOption { - return func(o *ClientOptions) error { - o.UID = uid +// WithFeedAddr set feed_server addresse +func WithFeedAddr(addr string) Option { + // TODO: validate Address + return func(o *Options) error { + o.feedAddrs = []string{addr} return nil } } -// UseFileCache cache file to local file system -func UseFileCache(useFileCache bool) ClientOption { - return func(o *ClientOptions) error { - o.UseFileCache = useFileCache +// WithBizID set bscp business id +func WithBizID(id uint32) Option { + return func(o *Options) error { + o.bizID = id return nil } } -// FileCacheDir file local cache directory -func FileCacheDir(dir string) ClientOption { - return func(o *ClientOptions) error { - o.FileCacheDir = dir +// WithLabels set instance labels +func WithLabels(labels map[string]string) Option { + return func(o *Options) error { + o.labels = labels return nil } } -// WithDialTimeoutMS set dial timeout in millisecond -func WithDialTimeoutMS(timeout int64) ClientOption { - return func(o *ClientOptions) error { - o.DialTimeoutMS = timeout +// WithUID set sdk uid +func WithUID(uid string) Option { + return func(o *Options) error { + o.uid = uid return nil } } -// Token set sdk token -func Token(token string) ClientOption { - return func(o *ClientOptions) error { - o.Token = token +// WithToken set sdk token +func WithToken(token string) Option { + return func(o *Options) error { + o.token = token return nil } } diff --git a/cmd/bscp/pull.go b/cmd/bscp/pull.go index 7fd18c6bf..0158932ea 100644 --- a/cmd/bscp/pull.go +++ b/cmd/bscp/pull.go @@ -58,11 +58,11 @@ func Pull(cmd *cobra.Command, args []string) { conf.Labels = pkgutil.MergeLabels(conf.Labels, labels) } bscp, err := client.New( - client.FeedAddrs(conf.FeedAddrs), - client.BizID(conf.Biz), - client.Token(conf.Token), - client.Labels(conf.Labels), - client.UID(conf.UID), + client.WithFeedAddrs(conf.FeedAddrs), + client.WithBizID(conf.Biz), + client.WithToken(conf.Token), + client.WithLabels(conf.Labels), + client.WithUID(conf.UID), ) if err != nil { logger.Error("init client", logger.ErrAttr(err)) diff --git a/cmd/bscp/watch.go b/cmd/bscp/watch.go index b278f367d..474ae899f 100644 --- a/cmd/bscp/watch.go +++ b/cmd/bscp/watch.go @@ -74,11 +74,11 @@ func Watch(cmd *cobra.Command, args []string) { } bscp, err := client.New( - client.FeedAddrs(conf.FeedAddrs), - client.BizID(conf.Biz), - client.Token(conf.Token), - client.Labels(confLabels), - client.UID(conf.UID), + client.WithFeedAddrs(conf.FeedAddrs), + client.WithBizID(conf.Biz), + client.WithToken(conf.Token), + client.WithLabels(confLabels), + client.WithUID(conf.UID), ) if err != nil { logger.Error("init client", logger.ErrAttr(err)) diff --git a/examples/kv-ctl/main.go b/examples/kv-ctl/main.go index d086e44de..5c9d98186 100644 --- a/examples/kv-ctl/main.go +++ b/examples/kv-ctl/main.go @@ -89,10 +89,10 @@ func execute() { } bscp, err := client.New( - client.FeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")), - client.BizID(uint32(biz)), - client.Token(os.Getenv("BSCP_TOKEN")), - client.Labels(labels), + client.WithFeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")), + client.WithBizID(uint32(biz)), + client.WithToken(os.Getenv("BSCP_TOKEN")), + client.WithLabels(labels), ) if err != nil { logger.Error("init client", logger.ErrAttr(err)) diff --git a/examples/pull-file/main.go b/examples/pull-file/main.go index 3ef7dc52a..519d91374 100644 --- a/examples/pull-file/main.go +++ b/examples/pull-file/main.go @@ -16,7 +16,6 @@ package main import ( "os" "strconv" - "strings" "golang.org/x/exp/slog" @@ -38,9 +37,9 @@ func main() { } bscp, err := client.New( - client.FeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")), - client.BizID(uint32(biz)), - client.Token(os.Getenv("BSCP_TOKEN")), + client.WithFeedAddr(os.Getenv("BSCP_FEED_ADDRS")), + client.WithBizID(uint32(biz)), + client.WithToken(os.Getenv("BSCP_TOKEN")), ) if err != nil { slog.Error("init client", logger.ErrAttr(err)) diff --git a/examples/pull-kv/main.go b/examples/pull-kv/main.go index bc019f9c7..913164b13 100644 --- a/examples/pull-kv/main.go +++ b/examples/pull-kv/main.go @@ -45,10 +45,10 @@ func main() { } bscp, err := client.New( - client.FeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")), - client.BizID(uint32(biz)), - client.Token(os.Getenv("BSCP_TOKEN")), - client.Labels(labels), + client.WithFeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")), + client.WithBizID(uint32(biz)), + client.WithToken(os.Getenv("BSCP_TOKEN")), + client.WithLabels(labels), ) if err != nil { logger.Error("init client", logger.ErrAttr(err)) diff --git a/examples/watch-file/main.go b/examples/watch-file/main.go index 8dbeb5c6b..3f2f4e201 100644 --- a/examples/watch-file/main.go +++ b/examples/watch-file/main.go @@ -41,9 +41,9 @@ func main() { } bscp, err := client.New( - client.FeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")), - client.BizID(uint32(biz)), - client.Token(os.Getenv("BSCP_TOKEN")), + client.WithFeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")), + client.WithBizID(uint32(biz)), + client.WithToken(os.Getenv("BSCP_TOKEN")), ) if err != nil { logger.Error("init client", logger.ErrAttr(err)) diff --git a/examples/watch-kv/main.go b/examples/watch-kv/main.go index d75f72748..b1789add7 100644 --- a/examples/watch-kv/main.go +++ b/examples/watch-kv/main.go @@ -48,10 +48,10 @@ func main() { } bscp, err := client.New( - client.FeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")), - client.BizID(uint32(biz)), - client.Token(os.Getenv("BSCP_TOKEN")), - client.Labels(labels), + client.WithFeedAddrs(strings.Split(os.Getenv("BSCP_FEED_ADDRS"), ",")), + client.WithBizID(uint32(biz)), + client.WithToken(os.Getenv("BSCP_TOKEN")), + client.WithLabels(labels), ) if err != nil { logger.Error("init bscp client", logger.ErrAttr(err))