Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from AH-dark/refactor/migrate
Browse files Browse the repository at this point in the history
refactor(actions/migrate): use json file instead of variable config
  • Loading branch information
AH-dark authored Sep 10, 2023
2 parents 5c31389 + d5b3190 commit 72ffc04
Show file tree
Hide file tree
Showing 16 changed files with 657 additions and 503 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
Expand Down
6 changes: 0 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,3 @@ jobs:
GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
DOCKER_REGISTRY: ghcr.io/ah-dark
DOCKER_IMAGE_NAME: epay-cli

- name: Upload assets
uses: actions/upload-artifact@v3
with:
name: migrator
path: dist/*
8 changes: 8 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ archives:
format_overrides:
- goos: windows
format: zip
files:
- src: data/*
dst: data
info:
owner: root
group: root
mtime: "{{ .CommitDate }}"
mode: 0644

release:
github:
Expand Down
8 changes: 8 additions & 0 deletions actions/factory/action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package factory

import "github.com/urfave/cli/v2"

type ActionService interface {
Command() *cli.Command
Do(c *cli.Context) error
}
73 changes: 25 additions & 48 deletions actions/submit.go → actions/implements/mapi/command.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package actions
package mapi

import (
"fmt"
"net/url"

"github.com/AH-dark/epay-cli/pkg/epay"
"github.com/AH-dark/epay-cli/pkg/utils"
"github.com/urfave/cli/v2"
)

func SubmitCommand() *cli.Command {
func (svc *service) Command() *cli.Command {
return &cli.Command{
Name: "submit",
Usage: "get submit.php url and args",
Action: SubmitAction,
Name: "mapi",
Usage: "mapi submit",
Action: svc.Do,
Flags: []cli.Flag{
&cli.IntFlag{
Name: "pid",
Expand Down Expand Up @@ -73,45 +69,26 @@ func SubmitCommand() *cli.Command {
Required: false,
EnvVars: []string{"EPAY_RETURN_URL"},
},
&cli.StringFlag{
Name: "client-ip",
Usage: "client ip",
Value: "127.0.0.1",
Required: false,
EnvVars: []string{"EPAY_CLIENT_IP"},
},
&cli.StringFlag{
Name: "device",
Usage: "device",
Value: "pc",
Required: false,
EnvVars: []string{"EPAY_DEVICE"},
},
&cli.StringFlag{
Name: "param",
Usage: "param",
Required: false,
EnvVars: []string{"EPAY_PARAM"},
},
},
}
}

func SubmitAction(c *cli.Context) error {
client, err := epay.NewClient(&epay.Config{
PartnerID: c.Int("pid"),
AppSecret: c.String("secret"),
Endpoint: c.String("endpoint"),
})
if err != nil {
return err
}

u, args, err := client.Submit(c.Context, &epay.SubmitArgs{
Type: epay.PaymentType(c.String("type")),
ServiceTradeNo: c.String("trade-no"),
Name: c.String("name"),
Money: c.String("money"),
NotifyUrl: c.String("notify-url"),
ReturnUrl: c.String("return-url"),
})
if err != nil {
return err
}

values := url.Values{}
for k, v := range args {
values.Add(k, v)
}

urlObj, err := url.Parse(u)
if err != nil {
return err
}

urlObj.RawQuery = values.Encode()

fmt.Println(urlObj.String())

return nil
}
72 changes: 72 additions & 0 deletions actions/implements/mapi/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package mapi

import (
"fmt"
"strconv"

"github.com/urfave/cli/v2"

"github.com/AH-dark/epay-cli/actions/factory"
"github.com/AH-dark/epay-cli/pkg/epay"
"github.com/AH-dark/epay-cli/pkg/utils"
)

type service struct {
}

func NewService() factory.ActionService {
return &service{}
}

func (svc *service) getSign(c *cli.Context) string {
sign := utils.CalculateEPaySign(map[string]string{
"pid": strconv.Itoa(c.Int("pid")),
"type": c.String("type"),
"out_trade_no": c.String("trade-no"),
"notify_url": c.String("notify-url"),
"return_url": c.String("return-url"),
"name": c.String("name"),
"money": c.String("money"),
"clientip": c.String("client-ip"),
"device": c.String("device"),
"param": c.String("param"),
}, c.String("secret"))

return sign
}

func (svc *service) Do(c *cli.Context) error {
client, err := epay.NewClient(&epay.Config{
PartnerID: c.Int("pid"),
AppSecret: c.String("secret"),
Endpoint: c.String("endpoint"),
})
if err != nil {
return err
}

sign := svc.getSign(c)
fmt.Println("Sign:", sign)

url, args, err := client.MApiSubmit(c.Context, &epay.MApiSubmitArgs{
Type: epay.PaymentType(c.String("type")),
OutTradeNo: c.String("trade-no"),
Name: c.String("name"),
Money: c.String("money"),
NotifyUrl: c.String("notify-url"),
ReturnUrl: utils.EmptyPtr(c.String("return-url")),
ClientIP: c.String("client-ip"),
Device: utils.EmptyPtr(epay.DeviceType(c.String("device"))),
Param: utils.EmptyPtr(c.String("param")),
Sign: sign,
SignType: "MD5",
})
if err != nil {
return err
}

fmt.Println("URL:", url)
fmt.Println("Args:", args)

return nil
}
97 changes: 97 additions & 0 deletions actions/implements/migrate/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package migrate

import (
"strconv"

"github.com/urfave/cli/v2"

"github.com/AH-dark/epay-cli/pkg/utils"
)

func (svc *service) Command() *cli.Command {
return &cli.Command{
Name: "migrate",
Usage: "generate sql schema",
Action: svc.Do,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "database.driver",
Usage: "database driver",
EnvVars: []string{"DATABASE_DRIVER", "DB_DRIVER"},
Value: "mysql",
},
&cli.StringFlag{
Name: "database.host",
Usage: "database host",
EnvVars: []string{"DATABASE_HOST", "DB_HOST"},
Value: "localhost",
},
&cli.IntFlag{
Name: "database.port",
Usage: "database port",
EnvVars: []string{"DATABASE_PORT", "DB_PORT"},
Value: 3306,
},
&cli.StringFlag{
Name: "database.name",
Usage: "database name",
EnvVars: []string{"DATABASE_NAME", "DB_NAME"},
Value: "epay",
},
&cli.StringFlag{
Name: "database.user",
Usage: "database user",
EnvVars: []string{"DATABASE_USER", "DB_USER"},
Value: "root",
},
&cli.StringFlag{
Name: "database.password",
Usage: "database password",
EnvVars: []string{"DATABASE_PASSWORD", "DB_PASSWORD"},
Value: "",
},
&cli.StringFlag{
Name: "database.prefix",
Usage: "database prefix",
EnvVars: []string{"DATABASE_PREFIX", "DB_PREFIX"},
Value: "pay_",
},
&cli.StringFlag{
Name: "database.sslmode",
Usage: "database sslmode",
EnvVars: []string{"DATABASE_SSL_MODE", "DB_SSL_MODE"},
Value: "disable",
},
&cli.StringFlag{
Name: "database.charset",
Usage: "database charset",
EnvVars: []string{"DATABASE_CHARSET", "DB_CHARSET"},
Value: "utf8mb4",
},
&cli.StringFlag{
Name: "app.syskey",
Usage: "app syskey",
EnvVars: []string{"APP_SYSKEY", "APP_SYS_KEY"},
Value: utils.RandString(32),
},
&cli.StringFlag{
Name: "app.cronkey",
Usage: "app cronkey",
EnvVars: []string{"APP_CRONKEY", "APP_CRON_KEY"},
Value: strconv.Itoa(utils.RandInt(100000, 999999)),
},
&cli.StringFlag{
Name: "migrate.default_config",
Usage: "migrate default config",
EnvVars: []string{"MIGRATE_DEFAULT_CONFIG", "MIGRATION_DEFAULT_CONFIG"},
Value: "./data/default_config.json",
},
&cli.StringFlag{
Name: "migrate.default_payment_types",
Usage: "migrate default payment types",
EnvVars: []string{"MIGRATE_DEFAULT_PAYMENT_TYPES", "MIGRATION_DEFAULT_PAYMENT_TYPES"},
Value: "./data/default_payment_types.json",
},
},
}
}
Loading

0 comments on commit 72ffc04

Please sign in to comment.