Export/Import json & Refactoring-1812 (#4)
* Config Export. * Import feature. * Refactoring config accessibility.
This commit is contained in:
19
cli/cli.go
19
cli/cli.go
@ -19,7 +19,7 @@ var hostname *url.URL
|
||||
var hostkey *rsa.PrivateKey
|
||||
var redClient *redis.Client
|
||||
var macServer *machinery.Server
|
||||
var relConfig relayconf.RelayConfig
|
||||
var exportConfig relayconf.ExportConfig
|
||||
|
||||
func main() {
|
||||
pemPath := os.Getenv("ACTOR_PEM")
|
||||
@ -111,6 +111,22 @@ func main() {
|
||||
Usage: "Show all relay configrations",
|
||||
Action: listConfigs,
|
||||
},
|
||||
{
|
||||
Name: "export",
|
||||
Usage: "Export all relay information (json)",
|
||||
Action: exportConfigs,
|
||||
},
|
||||
{
|
||||
Name: "import",
|
||||
Usage: "Import relay information (json)",
|
||||
Action: importConfigs,
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "json, j",
|
||||
Usage: "json file path",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "service-block",
|
||||
Usage: "Enable blocking for service-type actor",
|
||||
@ -181,6 +197,7 @@ func main() {
|
||||
},
|
||||
}
|
||||
|
||||
exportConfig = relayconf.NewConfig(redClient)
|
||||
err = app.Run(os.Args)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
"github.com/RichardKnop/machinery/v1/config"
|
||||
"github.com/go-redis/redis"
|
||||
"github.com/yukimochi/Activity-Relay/KeyLoader"
|
||||
"github.com/yukimochi/Activity-Relay/RelayConf"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
@ -30,7 +31,8 @@ func TestMain(m *testing.M) {
|
||||
}
|
||||
macServer, _ = machinery.NewServer(macConfig)
|
||||
redClient.FlushAll().Result()
|
||||
relConfig.Load(redClient)
|
||||
exportConfig = relayconf.NewConfig(redClient)
|
||||
code := m.Run()
|
||||
os.Exit(code)
|
||||
redClient.FlushAll().Result()
|
||||
}
|
||||
|
@ -1,7 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
"github.com/yukimochi/Activity-Relay/RelayConf"
|
||||
@ -15,38 +18,86 @@ const (
|
||||
|
||||
func serviceBlock(c *cli.Context) {
|
||||
if c.Bool("undo") {
|
||||
relConfig.Set(redClient, BlockService, false)
|
||||
exportConfig.SetConfig(BlockService, false)
|
||||
fmt.Println("Blocking for service-type actor is Disabled.")
|
||||
} else {
|
||||
relConfig.Set(redClient, BlockService, true)
|
||||
exportConfig.SetConfig(BlockService, true)
|
||||
fmt.Println("Blocking for service-type actor is Enabled.")
|
||||
}
|
||||
}
|
||||
|
||||
func manuallyAccept(c *cli.Context) {
|
||||
if c.Bool("undo") {
|
||||
relConfig.Set(redClient, ManuallyAccept, false)
|
||||
exportConfig.SetConfig(ManuallyAccept, false)
|
||||
fmt.Println("Manually accept follow-request is Disabled.")
|
||||
} else {
|
||||
relConfig.Set(redClient, ManuallyAccept, true)
|
||||
exportConfig.SetConfig(ManuallyAccept, true)
|
||||
fmt.Println("Manually accept follow-request is Enabled.")
|
||||
}
|
||||
}
|
||||
|
||||
func createAsAnnounce(c *cli.Context) {
|
||||
if c.Bool("undo") {
|
||||
relConfig.Set(redClient, CreateAsAnnounce, false)
|
||||
exportConfig.SetConfig(CreateAsAnnounce, false)
|
||||
fmt.Println("Announce activity instead of relay create activity is Disabled.")
|
||||
} else {
|
||||
relConfig.Set(redClient, CreateAsAnnounce, true)
|
||||
exportConfig.SetConfig(CreateAsAnnounce, true)
|
||||
fmt.Println("Announce activity instead of relay create activity is Enabled.")
|
||||
}
|
||||
}
|
||||
|
||||
func listConfigs(c *cli.Context) {
|
||||
relConfig.Load(redClient)
|
||||
|
||||
fmt.Println("Blocking for service-type actor : ", relConfig.BlockService)
|
||||
fmt.Println("Manually accept follow-request : ", relConfig.ManuallyAccept)
|
||||
fmt.Println("Announce activity instead of relay create activity : ", relConfig.CreateAsAnnounce)
|
||||
fmt.Println("Blocking for service-type actor : ", exportConfig.RelayConfig.BlockService)
|
||||
fmt.Println("Manually accept follow-request : ", exportConfig.RelayConfig.ManuallyAccept)
|
||||
fmt.Println("Announce activity instead of relay create activity : ", exportConfig.RelayConfig.CreateAsAnnounce)
|
||||
}
|
||||
|
||||
func exportConfigs(c *cli.Context) {
|
||||
jsonData, _ := json.Marshal(&exportConfig)
|
||||
fmt.Println(string(jsonData))
|
||||
}
|
||||
|
||||
func importConfigs(c *cli.Context) {
|
||||
file, err := os.Open(c.String("json"))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
jsonData, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
var data relayconf.ExportConfig
|
||||
err = json.Unmarshal(jsonData, &data)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
if data.RelayConfig.BlockService {
|
||||
exportConfig.SetConfig(BlockService, true)
|
||||
}
|
||||
if data.RelayConfig.ManuallyAccept {
|
||||
exportConfig.SetConfig(ManuallyAccept, true)
|
||||
}
|
||||
if data.RelayConfig.CreateAsAnnounce {
|
||||
exportConfig.SetConfig(CreateAsAnnounce, true)
|
||||
}
|
||||
for _, LimitedDomain := range data.LimitedDomains {
|
||||
exportConfig.SetLimitedDomain(LimitedDomain, true)
|
||||
redClient.HSet("relay:config:limitedDomain", LimitedDomain, "1")
|
||||
}
|
||||
for _, BlockedDomain := range data.BlockedDomains {
|
||||
exportConfig.SetLimitedDomain(BlockedDomain, true)
|
||||
redClient.HSet("relay:config:blockedDomain", BlockedDomain, "1")
|
||||
}
|
||||
for _, Subscription := range data.Subscriptions {
|
||||
exportConfig.AddSubscription(relayconf.Subscription{
|
||||
Domain: Subscription.Domain,
|
||||
InboxURL: Subscription.InboxURL,
|
||||
ActivityID: Subscription.ActivityID,
|
||||
ActorID: Subscription.ActorID,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -25,14 +25,14 @@ func TestServiceBlock(t *testing.T) {
|
||||
fooCmd,
|
||||
}
|
||||
|
||||
relConfig.Set(redClient, BlockService, false)
|
||||
exportConfig.SetConfig(BlockService, false)
|
||||
app.Run([]string{"", "service-block"})
|
||||
if !relConfig.BlockService {
|
||||
if !exportConfig.RelayConfig.BlockService {
|
||||
t.Fatalf("Not Enabled ServiceBlock feature,")
|
||||
}
|
||||
|
||||
app.Run([]string{"", "service-block", "-u"})
|
||||
if relConfig.BlockService {
|
||||
if exportConfig.RelayConfig.BlockService {
|
||||
t.Fatalf("Not Disabled ServiceBlock feature,")
|
||||
}
|
||||
}
|
||||
@ -54,14 +54,14 @@ func TestManuallyAccept(t *testing.T) {
|
||||
fooCmd,
|
||||
}
|
||||
|
||||
relConfig.Set(redClient, ManuallyAccept, false)
|
||||
exportConfig.SetConfig(ManuallyAccept, false)
|
||||
app.Run([]string{"", "manually-accept"})
|
||||
if !relConfig.ManuallyAccept {
|
||||
if !exportConfig.RelayConfig.ManuallyAccept {
|
||||
t.Fatalf("Not Enabled Manually accept follow-request feature,")
|
||||
}
|
||||
|
||||
app.Run([]string{"", "manually-accept", "-u"})
|
||||
if relConfig.ManuallyAccept {
|
||||
if exportConfig.RelayConfig.ManuallyAccept {
|
||||
t.Fatalf("Not Disabled Manually accept follow-request feature,")
|
||||
}
|
||||
}
|
||||
@ -83,14 +83,14 @@ func TestCreateAsAnnounce(t *testing.T) {
|
||||
fooCmd,
|
||||
}
|
||||
|
||||
relConfig.Set(redClient, CreateAsAnnounce, false)
|
||||
exportConfig.SetConfig(CreateAsAnnounce, false)
|
||||
app.Run([]string{"", "create-as-announce"})
|
||||
if !relConfig.CreateAsAnnounce {
|
||||
if !exportConfig.RelayConfig.CreateAsAnnounce {
|
||||
t.Fatalf("Not Enabled Announce activity instead of relay create activity feature,")
|
||||
}
|
||||
|
||||
app.Run([]string{"", "create-as-announce", "-u"})
|
||||
if relConfig.CreateAsAnnounce {
|
||||
if exportConfig.RelayConfig.CreateAsAnnounce {
|
||||
t.Fatalf("Not Disabled Announce activity instead of relay create activity feature,")
|
||||
}
|
||||
}
|
||||
@ -106,9 +106,9 @@ func TestListConfigs(t *testing.T) {
|
||||
fooCmd,
|
||||
}
|
||||
|
||||
relConfig.Set(redClient, BlockService, true)
|
||||
relConfig.Set(redClient, ManuallyAccept, true)
|
||||
relConfig.Set(redClient, CreateAsAnnounce, true)
|
||||
exportConfig.SetConfig(BlockService, true)
|
||||
exportConfig.SetConfig(ManuallyAccept, true)
|
||||
exportConfig.SetConfig(CreateAsAnnounce, true)
|
||||
out := capturer.CaptureStdout(func() {
|
||||
app.Run([]string{"", "show"})
|
||||
})
|
||||
@ -130,9 +130,9 @@ func TestListConfigs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
relConfig.Set(redClient, BlockService, false)
|
||||
relConfig.Set(redClient, ManuallyAccept, false)
|
||||
relConfig.Set(redClient, CreateAsAnnounce, false)
|
||||
exportConfig.SetConfig(BlockService, false)
|
||||
exportConfig.SetConfig(ManuallyAccept, false)
|
||||
exportConfig.SetConfig(CreateAsAnnounce, false)
|
||||
out = capturer.CaptureStdout(func() {
|
||||
app.Run([]string{"", "show"})
|
||||
})
|
||||
|
@ -2,39 +2,26 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
func listDomains(c *cli.Context) error {
|
||||
var err error
|
||||
var domains []string
|
||||
var message string
|
||||
switch c.String("type") {
|
||||
case "limited":
|
||||
fmt.Println(" - Limited domain :")
|
||||
domains, err = redClient.HKeys("relay:config:limitedDomain").Result()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
domains = exportConfig.LimitedDomains
|
||||
case "blocked":
|
||||
fmt.Println(" - Blocked domain :")
|
||||
domains, err = redClient.HKeys("relay:config:blockedDomain").Result()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
domains = exportConfig.BlockedDomains
|
||||
default:
|
||||
fmt.Println(" - Subscribed domain :")
|
||||
temp, err := redClient.Keys("relay:subscription:*").Result()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
temp := exportConfig.Subscriptions
|
||||
for _, domain := range temp {
|
||||
domains = append(domains, strings.Replace(domain, "relay:subscription:", "", 1))
|
||||
domains = append(domains, domain.Domain)
|
||||
}
|
||||
}
|
||||
fmt.Println(message)
|
||||
for _, domain := range domains {
|
||||
fmt.Println(domain)
|
||||
}
|
||||
@ -50,18 +37,18 @@ func setDomainType(c *cli.Context) error {
|
||||
switch c.String("type") {
|
||||
case "limited":
|
||||
if c.Bool("undo") {
|
||||
redClient.HDel("relay:config:limitedDomain", c.String("domain"))
|
||||
exportConfig.SetLimitedDomain(c.String("domain"), false)
|
||||
fmt.Println("Unset [" + c.String("domain") + "] as Limited domain.")
|
||||
} else {
|
||||
redClient.HSet("relay:config:limitedDomain", c.String("domain"), "1")
|
||||
exportConfig.SetLimitedDomain(c.String("domain"), true)
|
||||
fmt.Println("Set [" + c.String("domain") + "] as Limited domain.")
|
||||
}
|
||||
case "blocked":
|
||||
if c.Bool("undo") {
|
||||
redClient.HDel("relay:config:blockedDomain", c.String("domain"))
|
||||
exportConfig.SetBlockedDomain(c.String("domain"), false)
|
||||
fmt.Println("Unset [" + c.String("domain") + "] as Blocked domain.")
|
||||
} else {
|
||||
redClient.HSet("relay:config:blockedDomain", c.String("domain"), "1")
|
||||
exportConfig.SetBlockedDomain(c.String("domain"), true)
|
||||
fmt.Println("Set [" + c.String("domain") + "] as Blocked domain.")
|
||||
}
|
||||
default:
|
||||
|
Reference in New Issue
Block a user