Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cronjob #196

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cmd/static/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/cloudwego/cwgo/pkg/api_list"
"github.com/cloudwego/cwgo/pkg/client"
"github.com/cloudwego/cwgo/pkg/consts"
"github.com/cloudwego/cwgo/pkg/cronjob"
"github.com/cloudwego/cwgo/pkg/curd/doc"
"github.com/cloudwego/cwgo/pkg/fallback"
"github.com/cloudwego/cwgo/pkg/job"
Expand Down Expand Up @@ -107,6 +108,17 @@ func Init() *cli.App {
return job.Job(globalArgs.JobArgument)
},
},
{
Name: CronJobName,
Usage: CronJobUsage,
Flags: cronjobFlags(),
Action: func(c *cli.Context) error {
if err := globalArgs.CronJobArgument.ParseCli(c); err != nil {
return err
}
return cronjob.Cronjob(globalArgs.CronJobArgument)
},
},
{
Name: ApiListName,
Usage: ApiUsage,
Expand Down Expand Up @@ -214,6 +226,12 @@ Examples:

Examples:
cwgo job --job_name jobOne --job_name jobTwo --module my_job
`
CronJobName = "cronjob"
CronJobUsage = `generate cronjob code

Examples:
cwgo cronjob --job_name jobOne --job_name jobTwo --module my_cronjob
`
FallbackName = "fallback"
FallbackUsage = "fallback to hz or kitex"
Expand Down
31 changes: 31 additions & 0 deletions cmd/static/cronjob_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package static

import (
"github.com/cloudwego/cwgo/pkg/consts"
"github.com/urfave/cli/v2"
)

func cronjobFlags() []cli.Flag {
return []cli.Flag{
&cli.StringSliceFlag{Name: consts.JobName, Usage: "Specify the cronjob name."},
&cli.StringFlag{Name: consts.Module, Aliases: []string{"mod"}, Usage: "Specify the Go module name to generate go.mod."},
&cli.StringFlag{Name: consts.OutDir, Usage: "Specify output directory, default is current dir."},
&cli.StringFlag{Name: consts.JobFile, Usage: "Specify cronjob output file, default is job.go.", Value: "job.go"},
}
}
2 changes: 2 additions & 0 deletions config/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Argument struct {
*ModelArgument
*DocArgument
*JobArgument
*CronJobArgument
*ApiArgument
*FallbackArgument
}
Expand All @@ -50,6 +51,7 @@ func NewArgument() *Argument {
ModelArgument: NewModelArgument(),
DocArgument: NewDocArgument(),
JobArgument: NewJobArgument(),
CronJobArgument: NewCronJobArgument(),
ApiArgument: NewApiArgument(),
FallbackArgument: NewFallbackArgument(),
}
Expand Down
42 changes: 42 additions & 0 deletions config/cronjob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package config

import (
"github.com/cloudwego/cwgo/pkg/consts"
"github.com/urfave/cli/v2"
)

type CronJobArgument struct {
GoMod string
PackagePrefix string
JobName []string
OutDir string
JobFile string
}

func NewCronJobArgument() *CronJobArgument {
return &CronJobArgument{}
}

func (cj *CronJobArgument) ParseCli(ctx *cli.Context) error {
cj.JobName = ctx.StringSlice(consts.JobName)
cj.GoMod = ctx.String(consts.Module)
cj.OutDir = ctx.String(consts.OutDir)
cj.JobFile = ctx.String(consts.JobFile)
return nil
}
30 changes: 30 additions & 0 deletions pkg/common/utils/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package utils

import "unicode"

func CapitalizeFirstLetter(s string) string {
if len(s) == 0 {
return s
}
runes := []rune(s)
if !unicode.IsUpper(runes[0]) {
runes[0] = unicode.ToUpper(runes[0])
}
return string(runes)
}
1 change: 1 addition & 0 deletions pkg/consts/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ const (

const (
JobName = "job_name"
JobFile = "job_file"
)

const (
Expand Down
Loading
Loading