Use cobra in CLI. (#10)

This commit is contained in:
Naoki Kosaka
2018-12-22 23:05:57 +09:00
committed by GitHub
parent daae5f188f
commit 93a00a13e2
9 changed files with 615 additions and 416 deletions

View File

@ -3,14 +3,13 @@ package main
import (
"crypto/rsa"
"fmt"
"log"
"net/url"
"os"
"github.com/RichardKnop/machinery/v1"
"github.com/RichardKnop/machinery/v1/config"
"github.com/go-redis/redis"
"github.com/urfave/cli"
"github.com/spf13/cobra"
"github.com/yukimochi/Activity-Relay/KeyLoader"
"github.com/yukimochi/Activity-Relay/State"
)
@ -21,6 +20,14 @@ var redClient *redis.Client
var macServer *machinery.Server
var relayState state.RelayState
func buildNewCmd() *cobra.Command {
var app = &cobra.Command{}
app.AddCommand(domainCmdInit())
app.AddCommand(followCmdInit())
app.AddCommand(configCmdInit())
return app
}
func main() {
pemPath := os.Getenv("ACTOR_PEM")
if pemPath == "" {
@ -59,147 +66,8 @@ func main() {
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
app := cli.NewApp()
app.Name = "Activity Relay Extarnal CLI"
app.Usage = "Configure Activity-Relay"
app.Version = "0.2.0rc2"
app.Commands = []cli.Command{
{
Name: "domain",
Usage: "Management domains",
Subcommands: []cli.Command{
{
Name: "list",
Usage: "List {subscribed,limited,blocked} domains",
Flags: []cli.Flag{
cli.StringFlag{
Name: "type, t",
Value: "subscribed",
Usage: "Domain type [subscribed,limited,blocked]",
},
},
Action: listDomains,
},
{
Name: "set",
Usage: "set domain type [limited,blocked]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "type, t",
Usage: "Domain type [limited,blocked]",
},
cli.StringFlag{
Name: "domain, d",
Usage: "Registrate domain",
},
cli.BoolFlag{
Name: "undo, u",
Usage: "Undo registrate",
},
},
Action: setDomainType,
},
},
},
{
Name: "config",
Usage: "Management relay config",
Subcommands: []cli.Command{
{
Name: "show",
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",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "undo, u",
Usage: "Undo block",
},
},
Action: serviceBlock,
},
{
Name: "manually-accept",
Usage: "Enable manually accept follow-request",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "undo, u",
Usage: "Undo block",
},
},
Action: manuallyAccept,
},
{
Name: "create-as-announce",
Usage: "Enable announce activity instead of relay create activity (Not recommend)",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "undo, u",
Usage: "Undo block",
},
},
Action: createAsAnnounce,
},
},
},
{
Name: "follow-request",
Usage: "Management follow-request",
Subcommands: []cli.Command{
{
Name: "show",
Usage: "Show all follow-request",
Action: listFollows,
},
{
Name: "reject",
Usage: "Reject follow-request",
Flags: []cli.Flag{
cli.StringFlag{
Name: "domain, d",
Usage: "domain name",
},
},
Action: rejectFollow,
},
{
Name: "accept",
Usage: "Accept follow-request",
Flags: []cli.Flag{
cli.StringFlag{
Name: "domain, d",
Usage: "domain name",
},
},
Action: acceptFollow,
},
},
},
}
relayState = state.NewState(redClient)
err = app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
var app = buildNewCmd()
app.Execute()
}

View File

@ -6,7 +6,7 @@ import (
"io/ioutil"
"os"
"github.com/urfave/cli"
"github.com/spf13/cobra"
"github.com/yukimochi/Activity-Relay/State"
)
@ -16,49 +16,107 @@ const (
CreateAsAnnounce
)
func serviceBlock(c *cli.Context) {
if c.Bool("undo") {
relayState.SetConfig(BlockService, false)
fmt.Println("Blocking for service-type actor is Disabled.")
} else {
relayState.SetConfig(BlockService, true)
fmt.Println("Blocking for service-type actor is Enabled.")
func configCmdInit() *cobra.Command {
var config = &cobra.Command{
Use: "config",
Short: "Manage configuration for relay",
Long: "Enable/disable relay costomize and import/export relay database.",
}
}
func manuallyAccept(c *cli.Context) {
if c.Bool("undo") {
relayState.SetConfig(ManuallyAccept, false)
fmt.Println("Manually accept follow-request is Disabled.")
} else {
relayState.SetConfig(ManuallyAccept, true)
fmt.Println("Manually accept follow-request is Enabled.")
var configList = &cobra.Command{
Use: "list",
Short: "List all relay configration",
Long: "List all relay configration.",
Run: listConfig,
}
}
config.AddCommand(configList)
func createAsAnnounce(c *cli.Context) {
if c.Bool("undo") {
relayState.SetConfig(CreateAsAnnounce, false)
fmt.Println("Announce activity instead of relay create activity is Disabled.")
} else {
relayState.SetConfig(CreateAsAnnounce, true)
fmt.Println("Announce activity instead of relay create activity is Enabled.")
var configExport = &cobra.Command{
Use: "export",
Short: "Export all relay information",
Long: "Export all relay information by JSON format.",
Run: exportConfig,
}
config.AddCommand(configExport)
var configImport = &cobra.Command{
Use: "import [flags]",
Short: "Import all relay information",
Long: "Import all relay information from JSON file.",
Run: importConfig,
}
configImport.Flags().String("json", "", "JSON file-path")
configImport.MarkFlagRequired("json")
config.AddCommand(configImport)
var configEnable = &cobra.Command{
Use: "enable",
Short: "Enable/disable relay configration",
Long: `Enable or disable relay configration.
- service-block
Blocking feature for service-type actor.
- manually-accept
Enable manually accept follow request.
- create-as-announce
Enable announce activity instead of relay create activity (not recommend)`,
Args: cobra.MinimumNArgs(1),
RunE: configEnable,
}
configEnable.Flags().BoolP("disable", "d", false, "Disable configration instead of Enable")
config.AddCommand(configEnable)
return config
}
func listConfigs(c *cli.Context) {
fmt.Println("Blocking for service-type actor : ", relayState.RelayConfig.BlockService)
fmt.Println("Manually accept follow-request : ", relayState.RelayConfig.ManuallyAccept)
fmt.Println("Announce activity instead of relay create activity : ", relayState.RelayConfig.CreateAsAnnounce)
func configEnable(cmd *cobra.Command, args []string) error {
disable := cmd.Flag("disable").Value.String() == "true"
for _, config := range args {
switch config {
case "service-block":
if disable {
relayState.SetConfig(BlockService, false)
cmd.Println("Blocking for service-type actor is Disabled.")
} else {
relayState.SetConfig(BlockService, true)
cmd.Println("Blocking for service-type actor is Enabled.")
}
case "manually-accept":
if disable {
relayState.SetConfig(ManuallyAccept, false)
cmd.Println("Manually accept follow-request is Disabled.")
} else {
relayState.SetConfig(ManuallyAccept, true)
cmd.Println("Manually accept follow-request is Enabled.")
}
case "create-as-announce":
if disable {
relayState.SetConfig(CreateAsAnnounce, false)
cmd.Println("Announce activity instead of relay create activity is Disabled.")
} else {
relayState.SetConfig(CreateAsAnnounce, true)
cmd.Println("Announce activity instead of relay create activity is Enabled.")
}
default:
cmd.Println("Invalid config given")
}
}
return nil
}
func exportConfigs(c *cli.Context) {
func listConfig(cmd *cobra.Command, args []string) {
cmd.Println("Blocking for service-type actor : ", relayState.RelayConfig.BlockService)
cmd.Println("Manually accept follow-request : ", relayState.RelayConfig.ManuallyAccept)
cmd.Println("Announce activity instead of relay create activity : ", relayState.RelayConfig.CreateAsAnnounce)
}
func exportConfig(cmd *cobra.Command, args []string) {
jsonData, _ := json.Marshal(&relayState)
fmt.Println(string(jsonData))
cmd.Println(string(jsonData))
}
func importConfigs(c *cli.Context) {
file, err := os.Open(c.String("json"))
func importConfig(cmd *cobra.Command, args []string) {
file, err := os.Open(cmd.Flag("json").Value.String())
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
@ -77,18 +135,23 @@ func importConfigs(c *cli.Context) {
if data.RelayConfig.BlockService {
relayState.SetConfig(BlockService, true)
cmd.Println("Blocking for service-type actor is Enabled.")
}
if data.RelayConfig.ManuallyAccept {
relayState.SetConfig(ManuallyAccept, true)
cmd.Println("Manually accept follow-request is Enabled.")
}
if data.RelayConfig.CreateAsAnnounce {
relayState.SetConfig(CreateAsAnnounce, true)
cmd.Println("Announce activity instead of relay create activity is Enabled.")
}
for _, LimitedDomain := range data.LimitedDomains {
relayState.SetLimitedDomain(LimitedDomain, true)
cmd.Println("Set [" + LimitedDomain + "] as limited domain")
}
for _, BlockedDomain := range data.BlockedDomains {
relayState.SetLimitedDomain(BlockedDomain, true)
relayState.SetBlockedDomain(BlockedDomain, true)
cmd.Println("Set [" + BlockedDomain + "] as blocked domain")
}
for _, Subscription := range data.Subscriptions {
relayState.AddSubscription(state.Subscription{
@ -97,5 +160,6 @@ func importConfigs(c *cli.Context) {
ActivityID: Subscription.ActivityID,
ActorID: Subscription.ActorID,
})
cmd.Println("Regist [" + Subscription.Domain + "] as subscriber")
}
}

View File

@ -1,156 +1,142 @@
package main
import (
"bytes"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/kami-zh/go-capturer"
"github.com/urfave/cli"
)
func TestServiceBlock(t *testing.T) {
app := cli.NewApp()
fooCmd := cli.Command{
Name: "service-block",
Usage: "Enable blocking for service-type actor",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "undo, u",
Usage: "Undo block",
},
},
Action: serviceBlock,
}
app.Commands = []cli.Command{
fooCmd,
}
app := buildNewCmd()
relayState.SetConfig(BlockService, false)
app.Run([]string{"", "service-block"})
app.SetArgs([]string{"config", "enable", "service-block"})
app.Execute()
if !relayState.RelayConfig.BlockService {
t.Fatalf("Not Enabled ServiceBlock feature,")
t.Fatalf("Not Enabled Blocking feature for service-type actor")
}
app.Run([]string{"", "service-block", "-u"})
app.SetArgs([]string{"config", "enable", "-d", "service-block"})
app.Execute()
if relayState.RelayConfig.BlockService {
t.Fatalf("Not Disabled ServiceBlock feature,")
t.Fatalf("Not Disabled Blocking feature for service-type actor")
}
}
func TestManuallyAccept(t *testing.T) {
app := cli.NewApp()
fooCmd := cli.Command{
Name: "manually-accept",
Usage: "Enable Manually accept follow-request",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "undo, u",
Usage: "Undo block",
},
},
Action: manuallyAccept,
}
app.Commands = []cli.Command{
fooCmd,
}
app := buildNewCmd()
relayState.SetConfig(ManuallyAccept, false)
app.Run([]string{"", "manually-accept"})
app.SetArgs([]string{"config", "enable", "manually-accept"})
app.Execute()
if !relayState.RelayConfig.ManuallyAccept {
t.Fatalf("Not Enabled Manually accept follow-request feature,")
t.Fatalf("Not Enabled Manually accept follow-request feature")
}
app.Run([]string{"", "manually-accept", "-u"})
app.SetArgs([]string{"config", "enable", "-d", "manually-accept"})
app.Execute()
if relayState.RelayConfig.ManuallyAccept {
t.Fatalf("Not Disabled Manually accept follow-request feature,")
t.Fatalf("Not Disabled Manually accept follow-request feature")
}
}
func TestCreateAsAnnounce(t *testing.T) {
app := cli.NewApp()
fooCmd := cli.Command{
Name: "create-as-announce",
Usage: "Enable Announce activity instead of relay create activity (Not recommended)",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "undo, u",
Usage: "Undo block",
},
},
Action: createAsAnnounce,
}
app.Commands = []cli.Command{
fooCmd,
}
app := buildNewCmd()
relayState.SetConfig(CreateAsAnnounce, false)
app.Run([]string{"", "create-as-announce"})
app.SetArgs([]string{"config", "enable", "create-as-announce"})
app.Execute()
if !relayState.RelayConfig.CreateAsAnnounce {
t.Fatalf("Not Enabled Announce activity instead of relay create activity feature,")
t.Fatalf("Enable announce activity instead of relay create activity")
}
app.Run([]string{"", "create-as-announce", "-u"})
app.SetArgs([]string{"config", "enable", "-d", "create-as-announce"})
app.Execute()
if relayState.RelayConfig.CreateAsAnnounce {
t.Fatalf("Not Disabled Announce activity instead of relay create activity feature,")
t.Fatalf("Enable announce activity instead of relay create activity")
}
}
func TestInvalidConfig(t *testing.T) {
app := buildNewCmd()
buffer := new(bytes.Buffer)
app.SetOutput(buffer)
app.SetArgs([]string{"config", "enable", "hoge"})
app.Execute()
output := buffer.String()
if strings.Split(output, "\n")[0] != "Invalid config given" {
t.Fatalf("Invalid Responce.")
}
}
func TestListConfigs(t *testing.T) {
app := cli.NewApp()
fooCmd := cli.Command{
Name: "show",
Usage: "Show all relay configrations",
Action: listConfigs,
}
app.Commands = []cli.Command{
fooCmd,
}
func TestListConfig(t *testing.T) {
app := buildNewCmd()
buffer := new(bytes.Buffer)
app.SetOutput(buffer)
relayState.SetConfig(BlockService, true)
relayState.SetConfig(ManuallyAccept, true)
relayState.SetConfig(CreateAsAnnounce, true)
out := capturer.CaptureStdout(func() {
app.Run([]string{"", "show"})
})
app.SetArgs([]string{"config", "list"})
app.Execute()
for _, row := range strings.Split(out, "\n") {
output := buffer.String()
for _, row := range strings.Split(output, "\n") {
switch strings.Split(row, ":")[0] {
case "Blocking for service-type actor ":
if !(strings.Split(row, ":")[1] == " true") {
t.Fatalf(strings.Split(row, ":")[1])
}
case "Manually accept follow-request ":
if !(strings.Split(row, ":")[1] == " true") {
t.Fatalf("Invalid Responce.")
}
case "Announce activity instead of relay create activity ":
if !(strings.Split(row, ":")[1] == " true") {
t.Fatalf("Invalid Responce.")
}
}
}
relayState.SetConfig(BlockService, false)
relayState.SetConfig(ManuallyAccept, false)
relayState.SetConfig(CreateAsAnnounce, false)
out = capturer.CaptureStdout(func() {
app.Run([]string{"", "show"})
})
for _, row := range strings.Split(out, "\n") {
switch strings.Split(row, ":")[0] {
case "Blocking for service-type actor ":
if !(strings.Split(row, ":")[1] == " false") {
if strings.Split(row, ":")[1] == " true" {
t.Fatalf("Invalid Responce.")
}
case "Manually accept follow-request ":
if !(strings.Split(row, ":")[1] == " false") {
if strings.Split(row, ":")[1] == " true" {
t.Fatalf("Invalid Responce.")
}
case "Announce activity instead of relay create activity ":
if !(strings.Split(row, ":")[1] == " false") {
if strings.Split(row, ":")[1] == " true" {
t.Fatalf("Invalid Responce.")
}
}
}
}
func TestExportConfig(t *testing.T) {
app := buildNewCmd()
buffer := new(bytes.Buffer)
app.SetOutput(buffer)
app.SetArgs([]string{"config", "export"})
app.Execute()
file, err := os.Open("../misc/blankConfig.json")
if err != nil {
t.Fatalf("Test resource fetch error.")
}
jsonData, err := ioutil.ReadAll(file)
output := buffer.String()
if strings.Split(output, "\n")[0] != string(jsonData) {
t.Fatalf("Invalid Responce.")
}
}
func TestImportConfig(t *testing.T) {
app := buildNewCmd()
app.SetArgs([]string{"config", "import", "--json", "../misc/exampleConfig.json"})
app.Execute()
buffer := new(bytes.Buffer)
app.SetOutput(buffer)
app.SetArgs([]string{"config", "export"})
app.Execute()
file, err := os.Open("../misc/exampleConfig.json")
if err != nil {
t.Fatalf("Test resource fetch error.")
}
jsonData, err := ioutil.ReadAll(file)
output := buffer.String()
if strings.Split(output, "\n")[0] != string(jsonData) {
t.Fatalf("Invalid Responce.")
}
relayState.RedisClient.FlushAll().Result()
relayState.Load()
}

View File

@ -3,56 +3,88 @@ package main
import (
"fmt"
"github.com/urfave/cli"
"github.com/spf13/cobra"
)
func listDomains(c *cli.Context) error {
func domainCmdInit() *cobra.Command {
var domain = &cobra.Command{
Use: "domain",
Short: "Manage subscriber domain",
Long: "List all subscriber, set/unset domain as limited or blocked and undo subscribe domain.",
}
var domainList = &cobra.Command{
Use: "list [flags]",
Short: "List domain",
Long: "List domain which filtered given type.",
RunE: listDomains,
}
domainList.Flags().StringP("type", "t", "subscriber", "domain type [subscriber,limited,blocked]")
domain.AddCommand(domainList)
var domainSet = &cobra.Command{
Use: "set [flags]",
Short: "Set or unset domain as limited or blocked",
Long: "Set or unset domain as limited or blocked.",
Args: cobra.MinimumNArgs(1),
RunE: setDomainType,
}
domainSet.Flags().StringP("type", "t", "", "Apply domain type [limited,blocked]")
domainSet.MarkFlagRequired("type")
domainSet.Flags().BoolP("undo", "u", false, "Unset domain as limited or blocked")
domain.AddCommand(domainSet)
return domain
}
func listDomains(cmd *cobra.Command, args []string) error {
var domains []string
switch c.String("type") {
switch cmd.Flag("type").Value.String() {
case "limited":
fmt.Println(" - Limited domain :")
cmd.Println(" - Limited domain :")
domains = relayState.LimitedDomains
case "blocked":
fmt.Println(" - Blocked domain :")
cmd.Println(" - Blocked domain :")
domains = relayState.BlockedDomains
default:
fmt.Println(" - Subscribed domain :")
cmd.Println(" - Subscriber domain :")
temp := relayState.Subscriptions
for _, domain := range temp {
domains = append(domains, domain.Domain)
}
}
for _, domain := range domains {
fmt.Println(domain)
cmd.Println(domain)
}
fmt.Println(fmt.Sprintf("Total : %d", len(domains)))
cmd.Println(fmt.Sprintf("Total : %d", len(domains)))
return nil
}
func setDomainType(c *cli.Context) error {
if c.String("domain") == "" {
fmt.Println("No domain given.")
return nil
}
switch c.String("type") {
func setDomainType(cmd *cobra.Command, args []string) error {
undo := cmd.Flag("undo").Value.String() == "true"
switch cmd.Flag("type").Value.String() {
case "limited":
if c.Bool("undo") {
relayState.SetLimitedDomain(c.String("domain"), false)
fmt.Println("Unset [" + c.String("domain") + "] as Limited domain.")
} else {
relayState.SetLimitedDomain(c.String("domain"), true)
fmt.Println("Set [" + c.String("domain") + "] as Limited domain.")
for _, domain := range args {
relayState.SetLimitedDomain(domain, !undo)
if undo {
cmd.Println("Unset [" + domain + "] as limited domain")
} else {
cmd.Println("Set [" + domain + "] as limited domain")
}
}
case "blocked":
if c.Bool("undo") {
relayState.SetBlockedDomain(c.String("domain"), false)
fmt.Println("Unset [" + c.String("domain") + "] as Blocked domain.")
} else {
relayState.SetBlockedDomain(c.String("domain"), true)
fmt.Println("Set [" + c.String("domain") + "] as Blocked domain.")
for _, domain := range args {
relayState.SetBlockedDomain(domain, !undo)
if undo {
cmd.Println("Unset [" + domain + "] as blocked domain")
} else {
cmd.Println("Set [" + domain + "] as blocked domain")
}
}
default:
fmt.Println("No type given.")
cmd.Println("Invalid type given")
}
return nil
}

194
cli/domain_test.go Normal file
View File

@ -0,0 +1,194 @@
package main
import (
"bytes"
"strings"
"testing"
)
func TestListDomainSubscriber(t *testing.T) {
app := buildNewCmd()
app.SetArgs([]string{"config", "import", "--json", "../misc/exampleConfig.json"})
app.Execute()
buffer := new(bytes.Buffer)
app.SetOutput(buffer)
app.SetArgs([]string{"domain", "list"})
app.Execute()
output := buffer.String()
valid := ` - Subscriber domain :
subscription.example.jp
subscription.example.com
Total : 2
`
if output != valid {
t.Fatalf("Invalid Responce.")
}
relayState.RedisClient.FlushAll().Result()
relayState.Load()
}
func TestListDomainLimited(t *testing.T) {
app := buildNewCmd()
app.SetArgs([]string{"config", "import", "--json", "../misc/exampleConfig.json"})
app.Execute()
buffer := new(bytes.Buffer)
app.SetOutput(buffer)
app.SetArgs([]string{"domain", "list", "-t", "limited"})
app.Execute()
output := buffer.String()
valid := ` - Limited domain :
limitedDomain.example.jp
Total : 1
`
if output != valid {
t.Fatalf("Invalid Responce.")
}
relayState.RedisClient.FlushAll().Result()
relayState.Load()
}
func TestListDomainBlocked(t *testing.T) {
app := buildNewCmd()
app.SetArgs([]string{"config", "import", "--json", "../misc/exampleConfig.json"})
app.Execute()
buffer := new(bytes.Buffer)
app.SetOutput(buffer)
app.SetArgs([]string{"domain", "list", "-t", "blocked"})
app.Execute()
output := buffer.String()
valid := ` - Blocked domain :
blockedDomain.example.jp
Total : 1
`
if output != valid {
t.Fatalf("Invalid Responce.")
}
relayState.RedisClient.FlushAll().Result()
relayState.Load()
}
func TestSetDomainBlocked(t *testing.T) {
app := buildNewCmd()
app.SetArgs([]string{"domain", "set", "-t", "blocked", "testdomain.example.jp"})
app.Execute()
valid := false
for _, domain := range relayState.BlockedDomains {
if domain == "testdomain.example.jp" {
valid = true
}
}
if !valid {
t.Fatalf("Not set blocked domain")
}
relayState.RedisClient.FlushAll().Result()
relayState.Load()
}
func TestSetDomainLimited(t *testing.T) {
app := buildNewCmd()
app.SetArgs([]string{"domain", "set", "-t", "limited", "testdomain.example.jp"})
app.Execute()
valid := false
for _, domain := range relayState.LimitedDomains {
if domain == "testdomain.example.jp" {
valid = true
}
}
if !valid {
t.Fatalf("Not set limited domain")
}
relayState.RedisClient.FlushAll().Result()
relayState.Load()
}
func TestUnsetDomainBlocked(t *testing.T) {
app := buildNewCmd()
app.SetArgs([]string{"config", "import", "--json", "../misc/exampleConfig.json"})
app.Execute()
app.SetArgs([]string{"domain", "set", "-t", "blocked", "-u", "blockedDomain.example.jp"})
app.Execute()
valid := true
for _, domain := range relayState.BlockedDomains {
if domain == "blockedDomain.example.jp" {
valid = false
}
}
if !valid {
t.Fatalf("Not unset blocked domain")
}
relayState.RedisClient.FlushAll().Result()
relayState.Load()
}
func TestUnsetDomainLimited(t *testing.T) {
app := buildNewCmd()
app.SetArgs([]string{"config", "import", "--json", "../misc/exampleConfig.json"})
app.Execute()
app.SetArgs([]string{"domain", "set", "-t", "limited", "-u", "limitedDomain.example.jp"})
app.Execute()
valid := true
for _, domain := range relayState.LimitedDomains {
if domain == "limitedDomain.example.jp" {
valid = false
}
}
if !valid {
t.Fatalf("Not unset blocked domain")
}
relayState.RedisClient.FlushAll().Result()
relayState.Load()
}
func TestSetDomainInvalid(t *testing.T) {
app := buildNewCmd()
app.SetArgs([]string{"config", "import", "--json", "../misc/exampleConfig.json"})
app.Execute()
buffer := new(bytes.Buffer)
app.SetOutput(buffer)
app.SetArgs([]string{"domain", "set", "-t", "hoge", "hoge.example.jp"})
app.Execute()
output := buffer.String()
if strings.Split(output, "\n")[0] != "Invalid type given" {
t.Fatalf("Invalid Responce.")
}
relayState.RedisClient.FlushAll().Result()
relayState.Load()
}

View File

@ -7,10 +7,47 @@ import (
"strings"
"github.com/RichardKnop/machinery/v1/tasks"
"github.com/urfave/cli"
"github.com/spf13/cobra"
"github.com/yukimochi/Activity-Relay/ActivityPub"
"github.com/yukimochi/Activity-Relay/State"
)
func followCmdInit() *cobra.Command {
var follow = &cobra.Command{
Use: "follow",
Short: "Manage follow request for relay",
Long: "List all follow request and accept/reject follow requests.",
}
var followList = &cobra.Command{
Use: "list",
Short: "List follow request",
Long: "List follow request.",
RunE: listFollowsC,
}
follow.AddCommand(followList)
var followAccept = &cobra.Command{
Use: "accept",
Short: "Accept follow request",
Long: "Accept follow request by domain.",
Args: cobra.MinimumNArgs(1),
RunE: acceptFollowC,
}
follow.AddCommand(followAccept)
var followReject = &cobra.Command{
Use: "reject",
Short: "Reject follow request",
Long: "Reject follow request by domain.",
Args: cobra.MinimumNArgs(1),
RunE: rejectFollowC,
}
follow.AddCommand(followReject)
return follow
}
func pushRegistorJob(inboxURL string, body []byte) {
job := &tasks.Signature{
Name: "registor",
@ -34,12 +71,10 @@ func pushRegistorJob(inboxURL string, body []byte) {
}
}
func listFollows(c *cli.Context) error {
var err error
func listFollowsC(cmd *cobra.Command, args []string) error {
var domains []string
fmt.Println(" - Follow request :")
follows, err := redClient.Keys("relay:pending:*").Result()
cmd.Println(" - Follow request :")
follows, err := relayState.RedisClient.Keys("relay:pending:*").Result()
if err != nil {
return err
}
@ -47,80 +82,88 @@ func listFollows(c *cli.Context) error {
domains = append(domains, strings.Replace(follow, "relay:pending:", "", 1))
}
for _, domain := range domains {
fmt.Println(domain)
cmd.Println(domain)
}
cmd.Println(fmt.Sprintf("Total : %d", len(domains)))
return nil
}
func acceptFollow(c *cli.Context) error {
domain := c.String("domain")
if domain != "" {
num, err := redClient.Exists("relay:pending:" + domain).Result()
if err != nil {
return err
}
if num == 0 {
fmt.Println("Given domain not found.")
return nil
}
fmt.Println("Accept Follow request : " + domain)
data, err := redClient.HGetAll("relay:pending:" + domain).Result()
if err != nil {
return err
}
activity := activitypub.Activity{
Context: []string{"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"},
ID: data["activity_id"],
Actor: data["actor"],
Type: data["type"],
Object: data["object"],
}
resp := activity.GenerateResponse(hostname, "Accept")
jsonData, _ := json.Marshal(&resp)
pushRegistorJob(data["inbox_url"], jsonData)
redClient.HSet("relay:subscription:"+domain, "inbox_url", data["inbox_url"])
redClient.Del("relay:pending:" + domain)
} else {
fmt.Println("No domain given.")
func createFollowRequestResponse(domain string, response string) error {
data, err := relayState.RedisClient.HGetAll("relay:pending:" + domain).Result()
if err != nil {
return err
}
activity := activitypub.Activity{
Context: []string{"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"},
ID: data["activity_id"],
Actor: data["actor"],
Type: data["type"],
Object: data["object"],
}
resp := activity.GenerateResponse(hostname, response)
jsonData, _ := json.Marshal(&resp)
pushRegistorJob(data["inbox_url"], jsonData)
relayState.RedisClient.Del("relay:pending:" + domain)
if response == "Accept" {
relayState.AddSubscription(state.Subscription{
Domain: domain,
InboxURL: data["inbox_url"],
ActivityID: data["activity_id"],
ActorID: data["actor"],
})
}
return nil
}
func rejectFollow(c *cli.Context) error {
domain := c.String("domain")
if domain != "" {
num, err := redClient.Exists("relay:pending:" + domain).Result()
if err != nil {
return err
}
if num == 0 {
fmt.Println("Given domain not found.")
return nil
}
fmt.Println("Reject Follow request : " + domain)
data, err := redClient.HGetAll("relay:pending:" + domain).Result()
if err != nil {
return err
}
activity := activitypub.Activity{
Context: []string{"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"},
ID: data["activity_id"],
Actor: data["actor"],
Type: data["type"],
Object: data["object"],
}
resp := activity.GenerateResponse(hostname, "Reject")
jsonData, _ := json.Marshal(&resp)
pushRegistorJob(data["inbox_url"], jsonData)
redClient.Del("relay:pending:" + domain)
} else {
fmt.Println("No domain given.")
func acceptFollowC(cmd *cobra.Command, args []string) error {
var err error
var domains []string
follows, err := relayState.RedisClient.Keys("relay:pending:*").Result()
if err != nil {
return err
}
for _, follow := range follows {
domains = append(domains, strings.Replace(follow, "relay:pending:", "", 1))
}
for _, domain := range args {
for _, request := range domains {
if domain == request {
cmd.Println("Accept [" + domain + "] follow request")
createFollowRequestResponse(domain, "Accept")
break
}
}
cmd.Println("Invalid domain given")
}
return nil
}
func rejectFollowC(cmd *cobra.Command, args []string) error {
var err error
var domains []string
follows, err := redClient.Keys("relay:pending:*").Result()
if err != nil {
return err
}
for _, follow := range follows {
domains = append(domains, strings.Replace(follow, "relay:pending:", "", 1))
}
for _, domain := range args {
for _, request := range domains {
if domain == request {
cmd.Println("Reject [" + domain + "] follow request")
createFollowRequestResponse(domain, "Reject")
break
}
}
cmd.Println("Invalid domain given")
}
return nil
}