代码拉取完成,页面将自动刷新
// 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.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2024-08-20
package gen_annotation
import (
"fmt"
"gitee.com/go-libs/console"
"html/template"
"regexp"
"sort"
"time"
)
type (
// ResultCrontab
// is a component for crontab annotation result.
ResultCrontab struct {
Crontabs []*ResultCrontabItem
Package string
PackageList []*ResultCrontabAlias
Script, Datetime template.HTML
aliasList []string
aliasMapper map[string]string
aliasOffset int
}
// ResultCrontabAlias
// is a component for package imports.
ResultCrontabAlias struct {
Alias string // a0
Pkg string // sketch/app/crontabs
}
// ResultCrontabItem
// is a component for crontab executors.
ResultCrontabItem struct {
Line int // 24
File string // app/crontabs/example_crontab.go
Pkg string // a0
Struct string // ExampleCrontab
Strategy string
NodeUnique, GloballyUnique, RunOnStartup string
}
)
// NewResultCrontab
// creates a crontab result component.
func NewResultCrontab(command *console.Command, pkg string) *ResultCrontab {
return (&ResultCrontab{
Crontabs: make([]*ResultCrontabItem, 0),
Package: pkg,
PackageList: make([]*ResultCrontabAlias, 0),
}).init(command)
}
// AddEntity
// add a crontab entity.
func (o *ResultCrontab) AddEntity(entity *EntityCrontab) {
o.Crontabs = append(o.Crontabs, &ResultCrontabItem{
Line: entity.Line,
File: entity.File,
Pkg: o.AddPackage(entity.Import, true),
Struct: entity.Struct,
Strategy: entity.Strategy.String(),
NodeUnique: fmt.Sprintf(`%v`, entity.NodeUnique),
GloballyUnique: fmt.Sprintf(`%v`, entity.GloballyUnique),
RunOnStartup: fmt.Sprintf(`%v`, entity.RunOnStartup),
})
}
// AddPackage
// add a package import.
func (o *ResultCrontab) AddPackage(pkg string, rename bool) (alias string) {
pkg = regexp.MustCompile(`/+`).ReplaceAllString(pkg, `/`)
// Reuse
// previous alias name.
if str, ok := o.aliasMapper[pkg]; ok {
alias = str
return
}
// Alias name generate.
if rename {
alias = fmt.Sprintf(`a%d`, o.aliasOffset)
o.aliasOffset++
}
// Update alias mapping and list.
o.aliasList = append(o.aliasList, pkg)
o.aliasMapper[pkg] = alias
// Update package info.
return
}
// Ready
// is a hook when result ready.
func (o *ResultCrontab) Ready() *ResultCrontab {
o.initPackages()
return o
}
// +---------------------------------------------------------------------------+
// | Access methods |
// +---------------------------------------------------------------------------+
func (o *ResultCrontab) init(command *console.Command) *ResultCrontab {
// Init
// internal fields.
o.aliasList = make([]string, 0)
o.aliasMapper = make(map[string]string)
// Init
// basic fields.
o.Datetime = template.HTML(time.Now().Format(time.RFC3339))
o.Script = template.HTML(command.ToScript())
// Add
// builtin package.
o.AddPackage("gitee.com/go-libs/crontab", false)
return o
}
func (o *ResultCrontab) initPackages() {
// Sort
// package with alpha number.
sort.Strings(o.aliasList)
// Iterate
// added packages then build package list.
for _, pkg := range o.aliasList {
// Build
// alias component.
item := &ResultCrontabAlias{Pkg: pkg}
// Build
// alias name.
if alias, ok := o.aliasMapper[pkg]; ok {
item.Alias = alias
}
// Add
// to list.
o.PackageList = append(o.PackageList, item)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。