This is an automated email from the ASF dual-hosted git repository.

robin0716 pushed a commit to branch refactor/add-info-yaml
in repository https://gitbox.apache.org/repos/asf/incubator-answer-plugins.git

commit bd83099a2f21cd8dde2e7c7758399ea75b18b191
Author: robin <[email protected]>
AuthorDate: Wed Jul 3 15:43:55 2024 +0800

    refactor: Use info.yaml to manage plugin configuration information
---
 cache-redis/go.mod             |  2 +-
 cache-redis/info.yaml          |  5 +++++
 cache-redis/redis.go           | 11 +++++++----
 cache-redis/util.go            | 34 ++++++++++++++++++++++++++++++++++
 captcha-basic/basic.go         | 11 +++++++----
 captcha-basic/go.mod           |  2 +-
 captcha-basic/info.yaml        |  5 +++++
 captcha-basic/util.go          | 34 ++++++++++++++++++++++++++++++++++
 captcha-google-v2/go.mod       |  2 +-
 captcha-google-v2/info.yaml    |  5 +++++
 captcha-google-v2/recaptcha.go | 11 +++++++----
 captcha-google-v2/util.go      | 34 ++++++++++++++++++++++++++++++++++
 12 files changed, 141 insertions(+), 15 deletions(-)

diff --git a/cache-redis/go.mod b/cache-redis/go.mod
index 6c054d8..6c12a06 100644
--- a/cache-redis/go.mod
+++ b/cache-redis/go.mod
@@ -5,6 +5,7 @@ go 1.19
 require (
        github.com/apache/incubator-answer v1.3.1-0.20240506084933-9681c026adfe
        github.com/go-redis/redis/v8 v8.11.5
+       gopkg.in/yaml.v2 v2.4.0
 )
 
 require (
@@ -42,7 +43,6 @@ require (
        golang.org/x/sys v0.18.0 // indirect
        golang.org/x/text v0.14.0 // indirect
        google.golang.org/protobuf v1.30.0 // indirect
-       gopkg.in/yaml.v2 v2.4.0 // indirect
        gopkg.in/yaml.v3 v3.0.1 // indirect
        sigs.k8s.io/yaml v1.3.0 // indirect
 )
diff --git a/cache-redis/info.yaml b/cache-redis/info.yaml
new file mode 100644
index 0000000..6ad7291
--- /dev/null
+++ b/cache-redis/info.yaml
@@ -0,0 +1,5 @@
+slug_name: redis_cache
+type: cache
+version: 1.2.7
+author: answerdev
+link: https://github.com/apache/incubator-answer-plugins/tree/main/cache-redis
diff --git a/cache-redis/redis.go b/cache-redis/redis.go
index e9e4206..39369a3 100644
--- a/cache-redis/redis.go
+++ b/cache-redis/redis.go
@@ -53,13 +53,16 @@ func init() {
 }
 
 func (c *Cache) Info() plugin.Info {
+       info := &Info{}
+       info.getInfo()
+
        return plugin.Info{
                Name:        plugin.MakeTranslator(i18n.InfoName),
-               SlugName:    "redis_cache",
+               SlugName:    info.SlugName,
                Description: plugin.MakeTranslator(i18n.InfoDescription),
-               Author:      "answerdev",
-               Version:     "1.2.6",
-               Link:        
"https://github.com/apache/incubator-answer-plugins/tree/main/cache-redis";,
+               Author:      info.Author,
+               Version:     info.Version,
+               Link:        info.Link,
        }
 }
 
diff --git a/cache-redis/util.go b/cache-redis/util.go
new file mode 100644
index 0000000..f166747
--- /dev/null
+++ b/cache-redis/util.go
@@ -0,0 +1,34 @@
+package redis
+
+import (
+       "fmt"
+       "os"
+       "path/filepath"
+       "runtime"
+
+       "gopkg.in/yaml.v2"
+)
+
+type Info struct {
+       SlugName string `yaml:"slug_name"`
+       Type     string `yaml:"type"`
+       Version  string `yaml:"version"`
+       Author   string `yaml:"author"`
+       Link     string `yaml:"link"`
+}
+
+func (c *Info) getInfo() *Info {
+       _, filename, _, _ := runtime.Caller(0)
+       wd := filepath.Dir(filename)
+
+       yamlFilePath := filepath.Join(wd, "info.yaml")
+       yamlFile, err := os.ReadFile(yamlFilePath)
+       if err != nil {
+               fmt.Println(err)
+       }
+       err = yaml.Unmarshal(yamlFile, c)
+       if err != nil {
+               fmt.Println(err)
+       }
+       return c
+}
diff --git a/captcha-basic/basic.go b/captcha-basic/basic.go
index 30722e8..4eca82f 100644
--- a/captcha-basic/basic.go
+++ b/captcha-basic/basic.go
@@ -35,13 +35,16 @@ func init() {
 }
 
 func (c *Captcha) Info() plugin.Info {
+       info := &Info{}
+       info.getInfo()
+
        return plugin.Info{
                Name:        plugin.MakeTranslator(i18n.InfoName),
-               SlugName:    "basic_captcha",
+               SlugName:    info.SlugName,
                Description: plugin.MakeTranslator(i18n.InfoDescription),
-               Author:      "answerdev",
-               Version:     "1.0.1",
-               Link:        
"https://github.com/apache/incubator-answer-plugins/tree/main/captcha-basic";,
+               Author:      info.Author,
+               Version:     info.Version,
+               Link:        info.Link,
        }
 }
 
diff --git a/captcha-basic/go.mod b/captcha-basic/go.mod
index c27b522..9a21e83 100644
--- a/captcha-basic/go.mod
+++ b/captcha-basic/go.mod
@@ -5,6 +5,7 @@ go 1.19
 require (
        github.com/apache/incubator-answer v1.3.1-0.20240506084933-9681c026adfe
        github.com/mojocn/base64Captcha v1.3.6
+       gopkg.in/yaml.v2 v2.4.0
 )
 
 require (
@@ -43,7 +44,6 @@ require (
        golang.org/x/sys v0.18.0 // indirect
        golang.org/x/text v0.14.0 // indirect
        google.golang.org/protobuf v1.30.0 // indirect
-       gopkg.in/yaml.v2 v2.4.0 // indirect
        gopkg.in/yaml.v3 v3.0.1 // indirect
        sigs.k8s.io/yaml v1.3.0 // indirect
 )
diff --git a/captcha-basic/info.yaml b/captcha-basic/info.yaml
new file mode 100644
index 0000000..44eea4d
--- /dev/null
+++ b/captcha-basic/info.yaml
@@ -0,0 +1,5 @@
+slug_name: basic_captcha
+type: captcha
+version: 1.0.1
+author: answerdev
+link: 
https://github.com/apache/incubator-answer-plugins/tree/main/captcha-basic
\ No newline at end of file
diff --git a/captcha-basic/util.go b/captcha-basic/util.go
new file mode 100644
index 0000000..1e0a800
--- /dev/null
+++ b/captcha-basic/util.go
@@ -0,0 +1,34 @@
+package basic
+
+import (
+       "fmt"
+       "os"
+       "path/filepath"
+       "runtime"
+
+       "gopkg.in/yaml.v2"
+)
+
+type Info struct {
+       SlugName string `yaml:"slug_name"`
+       Type     string `yaml:"type"`
+       Version  string `yaml:"version"`
+       Author   string `yaml:"author"`
+       Link     string `yaml:"link"`
+}
+
+func (c *Info) getInfo() *Info {
+       _, filename, _, _ := runtime.Caller(0)
+       wd := filepath.Dir(filename)
+
+       yamlFilePath := filepath.Join(wd, "info.yaml")
+       yamlFile, err := os.ReadFile(yamlFilePath)
+       if err != nil {
+               fmt.Println(err)
+       }
+       err = yaml.Unmarshal(yamlFile, c)
+       if err != nil {
+               fmt.Println(err)
+       }
+       return c
+}
diff --git a/captcha-google-v2/go.mod b/captcha-google-v2/go.mod
index 8ef0513..6d01e07 100644
--- a/captcha-google-v2/go.mod
+++ b/captcha-google-v2/go.mod
@@ -5,6 +5,7 @@ go 1.19
 require (
        github.com/apache/incubator-answer v1.3.1-0.20240506084933-9681c026adfe
        github.com/segmentfault/pacman v1.0.5-0.20230822083413-c0075a2d401f
+       gopkg.in/yaml.v2 v2.4.0
 )
 
 require (
@@ -40,7 +41,6 @@ require (
        golang.org/x/sys v0.18.0 // indirect
        golang.org/x/text v0.14.0 // indirect
        google.golang.org/protobuf v1.30.0 // indirect
-       gopkg.in/yaml.v2 v2.4.0 // indirect
        gopkg.in/yaml.v3 v3.0.1 // indirect
        sigs.k8s.io/yaml v1.3.0 // indirect
 )
diff --git a/captcha-google-v2/info.yaml b/captcha-google-v2/info.yaml
new file mode 100644
index 0000000..9f30f75
--- /dev/null
+++ b/captcha-google-v2/info.yaml
@@ -0,0 +1,5 @@
+slug_name: google_v2_captcha
+type: captcha
+version: 1.0.1
+author: answerdev
+link: 
https://github.com/apache/incubator-answer-plugins/tree/main/captcha-google-v2
\ No newline at end of file
diff --git a/captcha-google-v2/recaptcha.go b/captcha-google-v2/recaptcha.go
index e1d9515..5186207 100644
--- a/captcha-google-v2/recaptcha.go
+++ b/captcha-google-v2/recaptcha.go
@@ -53,13 +53,16 @@ func init() {
 }
 
 func (c *Captcha) Info() plugin.Info {
+       info := &Info{}
+       info.getInfo()
+
        return plugin.Info{
                Name:        plugin.MakeTranslator(i18n.InfoName),
-               SlugName:    "google_v2_captcha",
+               SlugName:    info.SlugName,
                Description: plugin.MakeTranslator(i18n.InfoDescription),
-               Author:      "answerdev",
-               Version:     "1.0.1",
-               Link:        
"https://github.com/apache/incubator-answer-plugins/tree/main/captcha-google-v2";,
+               Author:      info.Author,
+               Version:     info.Version,
+               Link:        info.Link,
        }
 }
 
diff --git a/captcha-google-v2/util.go b/captcha-google-v2/util.go
new file mode 100644
index 0000000..00739eb
--- /dev/null
+++ b/captcha-google-v2/util.go
@@ -0,0 +1,34 @@
+package recaptcha
+
+import (
+       "fmt"
+       "os"
+       "path/filepath"
+       "runtime"
+
+       "gopkg.in/yaml.v2"
+)
+
+type Info struct {
+       SlugName string `yaml:"slug_name"`
+       Type     string `yaml:"type"`
+       Version  string `yaml:"version"`
+       Author   string `yaml:"author"`
+       Link     string `yaml:"link"`
+}
+
+func (c *Info) getInfo() *Info {
+       _, filename, _, _ := runtime.Caller(0)
+       wd := filepath.Dir(filename)
+
+       yamlFilePath := filepath.Join(wd, "info.yaml")
+       yamlFile, err := os.ReadFile(yamlFilePath)
+       if err != nil {
+               fmt.Println(err)
+       }
+       err = yaml.Unmarshal(yamlFile, c)
+       if err != nil {
+               fmt.Println(err)
+       }
+       return c
+}

Reply via email to