Skip to content

Commit

Permalink
rearrange yomo tags (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
wujunzhuo committed May 15, 2024
1 parent 30afc74 commit 658a98f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 47 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@

# Go workspace file
go.work

bin
yc.yml
22 changes: 11 additions & 11 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var (
func addUploadCmd(rootCmd *cobra.Command) {
msg := &pkg.ReqMsgUpload{}
var src string
cmd := subCmd(rootCmd, pkg.TAG_UPLOAD, msg,
cmd := subCmd(rootCmd, pkg.TAG_REQUEST_UPLOAD, "upload", msg,
func() error {
info, err := os.Stat(src)
if err != nil {
Expand Down Expand Up @@ -110,7 +110,7 @@ func addUploadCmd(rootCmd *cobra.Command) {

func addCreateCmd(rootCmd *cobra.Command) {
var envs []string
cmd := subCmd(rootCmd, pkg.TAG_CREATE, &pkg.ReqMsgCreate{
cmd := subCmd(rootCmd, pkg.TAG_REQUEST_CREATE, "create", &pkg.ReqMsgCreate{
Envs: &envs,
}, nil)
cmd.Flags().StringArrayVar(&envs, "envs", nil, "app environment variables")
Expand All @@ -119,37 +119,37 @@ func addCreateCmd(rootCmd *cobra.Command) {

func addStopCmd(rootCmd *cobra.Command) {
var timeout int
cmd := subCmd(rootCmd, pkg.TAG_STOP, &pkg.ReqMsgStop{
cmd := subCmd(rootCmd, pkg.TAG_REQUEST_STOP, "stop", &pkg.ReqMsgStop{
Timeout: &timeout,
}, nil)
cmd.Flags().IntVar(&timeout, "timeout", 10, "timeout")
bindFlags(cmd.Flags())
}

func addStartCmd(rootCmd *cobra.Command) {
subCmd(rootCmd, pkg.TAG_START, &pkg.ReqMsgStart{}, nil)
subCmd(rootCmd, pkg.TAG_REQUEST_START, "start", &pkg.ReqMsgStart{}, nil)
}

func addRemoveCmd(rootCmd *cobra.Command) {
subCmd(rootCmd, pkg.TAG_REMOVE, &pkg.ReqMsgRemove{}, nil)
subCmd(rootCmd, pkg.TAG_REQUEST_REMOVE, "remove", &pkg.ReqMsgRemove{}, nil)
}

func addStatusCmd(rootCmd *cobra.Command) {
subCmd(rootCmd, pkg.TAG_STATUS, &pkg.ReqMsgStatus{}, nil)
subCmd(rootCmd, pkg.TAG_REQUEST_STATUS, "status", &pkg.ReqMsgStatus{}, nil)
}

func addLogsCmd(rootCmd *cobra.Command) {
var tail int
cmd := subCmd(rootCmd, pkg.TAG_LOGS, &pkg.ReqMsgLogs{
cmd := subCmd(rootCmd, pkg.TAG_REQUEST_LOGS, "logs", &pkg.ReqMsgLogs{
Tail: &tail,
}, nil)
cmd.Flags().IntVar(&tail, "tail", 20, "tail")
bindFlags(cmd.Flags())
}

func subCmd[T any](rootCmd *cobra.Command, tag uint32, msg *T, f func() error) *cobra.Command {
func subCmd[T any](rootCmd *cobra.Command, tag uint32, use string, msg *T, f func() error) *cobra.Command {
cmd := &cobra.Command{
Use: pkg.TagName(tag),
Use: use,
Run: func(cmd *cobra.Command, args []string) {
credential := "app-key-secret:" + appKey + "|" + appSecret

Expand Down Expand Up @@ -190,15 +190,15 @@ func subCmd[T any](rootCmd *cobra.Command, tag uint32, msg *T, f func() error) *

var ctx context.Context
switch tag {
case pkg.TAG_LOGS:
case pkg.TAG_REQUEST_LOGS:
ctx, cancel = context.WithCancel(context.Background())
go func() {
for {
source.Write(tag, buf)
time.Sleep(time.Second * 15)
}
}()
case pkg.TAG_UPLOAD:
case pkg.TAG_REQUEST_UPLOAD:
ctx, cancel = context.WithCancel(context.Background())
source.Write(tag, buf)
default:
Expand Down
51 changes: 15 additions & 36 deletions pkg/data_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,43 +55,22 @@ type ResMsgLogs = struct {
}

const (
TAG_UPLOAD uint32 = 0xFF01
TAG_CREATE uint32 = 0xFF02
TAG_STOP uint32 = 0xFF03
TAG_START uint32 = 0xFF04
TAG_REMOVE uint32 = 0xFF05
TAG_STATUS uint32 = 0xFF06
TAG_LOGS uint32 = 0xFF07
TAG_RESPONSE_UPLOAD uint32 = 0xFF21
TAG_RESPONSE_CREATE uint32 = 0xFF22
TAG_RESPONSE_STOP uint32 = 0xFF23
TAG_RESPONSE_START uint32 = 0xFF24
TAG_RESPONSE_REMOVE uint32 = 0xFF25
TAG_RESPONSE_STATUS uint32 = 0xFF26
TAG_RESPONSE_LOGS uint32 = 0xFF27
TAG_REQUEST_UPLOAD uint32 = 0xE201
TAG_REQUEST_CREATE uint32 = 0xE202
TAG_REQUEST_STOP uint32 = 0xE203
TAG_REQUEST_START uint32 = 0xE204
TAG_REQUEST_REMOVE uint32 = 0xE205
TAG_REQUEST_STATUS uint32 = 0xE206
TAG_REQUEST_LOGS uint32 = 0xE207
TAG_RESPONSE_UPLOAD uint32 = 0xF201
TAG_RESPONSE_CREATE uint32 = 0xF202
TAG_RESPONSE_STOP uint32 = 0xF203
TAG_RESPONSE_START uint32 = 0xF204
TAG_RESPONSE_REMOVE uint32 = 0xF205
TAG_RESPONSE_STATUS uint32 = 0xF206
TAG_RESPONSE_LOGS uint32 = 0xF207
)

func TagName(tag uint32) string {
switch tag {
case TAG_UPLOAD:
return "upload"
case TAG_CREATE:
return "create"
case TAG_START:
return "start"
case TAG_STOP:
return "stop"
case TAG_REMOVE:
return "remove"
case TAG_STATUS:
return "status"
case TAG_LOGS:
return "logs"
default:
return ""
}
}

func ResponseTag(tag uint32) uint32 {
return tag + 0x20
return tag + 0x1000
}

0 comments on commit 658a98f

Please sign in to comment.