[ Cmii ] [ Octopus ] - 优化项目结构

This commit is contained in:
zeaslity
2024-03-19 16:04:09 +08:00
committed by zeaslity
parent 4ca8d77e74
commit 6b4616690c
80 changed files with 979 additions and 1025 deletions

View File

@@ -0,0 +1,90 @@
// Copyright 2013-2022 Frank Schroeder. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package assert provides helper functions for testing.
package assert
import (
"fmt"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strings"
"testing"
)
// skip defines the default call depth
const skip = 2
// Equal asserts that got and want are equal as defined by
// reflect.DeepEqual. The test fails with msg if they are not equal.
func Equal(t *testing.T, got, want interface{}, msg ...string) {
if x := equal(2, got, want, msg...); x != "" {
fmt.Println(x)
t.Fail()
}
}
func equal(skip int, got, want interface{}, msg ...string) string {
if !reflect.DeepEqual(got, want) {
return fail(skip, "got %v want %v %s", got, want, strings.Join(msg, " "))
}
return ""
}
// Panic asserts that function fn() panics.
// It assumes that recover() either returns a string or
// an error and fails if the message does not match
// the regular expression in 'matches'.
func Panic(t *testing.T, fn func(), matches string) {
if x := doesPanic(2, fn, matches); x != "" {
fmt.Println(x)
t.Fail()
}
}
func doesPanic(skip int, fn func(), expr string) (err string) {
defer func() {
r := recover()
if r == nil {
err = fail(skip, "did not panic")
return
}
var v string
switch r.(type) {
case error:
v = r.(error).Error()
case string:
v = r.(string)
}
err = matches(skip, v, expr)
}()
fn()
return ""
}
// Matches asserts that a value matches a given regular expression.
func Matches(t *testing.T, value, expr string) {
if x := matches(2, value, expr); x != "" {
fmt.Println(x)
t.Fail()
}
}
func matches(skip int, value, expr string) string {
ok, err := regexp.MatchString(expr, value)
if err != nil {
return fail(skip, "invalid pattern %q. %s", expr, err)
}
if !ok {
return fail(skip, "got %s which does not match %s", value, expr)
}
return ""
}
func fail(skip int, format string, args ...interface{}) string {
_, file, line, _ := runtime.Caller(skip)
return fmt.Sprintf("\t%s:%d: %s\n", filepath.Base(file), line, fmt.Sprintf(format, args...))
}

View File

@@ -0,0 +1,55 @@
// Copyright 2013-2022 Frank Schroeder. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package assert
import "testing"
func TestEqualEquals(t *testing.T) {
if got, want := equal(2, "a", "a"), ""; got != want {
t.Fatalf("got %q want %q", got, want)
}
}
func TestEqualFails(t *testing.T) {
if got, want := equal(2, "a", "b"), "\tassert_test.go:16: got a want b \n"; got != want {
t.Fatalf("got %q want %q", got, want)
}
}
func TestPanicPanics(t *testing.T) {
if got, want := doesPanic(2, func() { panic("foo") }, ""), ""; got != want {
t.Fatalf("got %q want %q", got, want)
}
}
func TestPanicPanicsAndMatches(t *testing.T) {
if got, want := doesPanic(2, func() { panic("foo") }, "foo"), ""; got != want {
t.Fatalf("got %q want %q", got, want)
}
}
func TestPanicPanicsAndDoesNotMatch(t *testing.T) {
if got, want := doesPanic(2, func() { panic("foo") }, "bar"), "\tassert.go:62: got foo which does not match bar\n"; got != want {
t.Fatalf("got %q want %q", got, want)
}
}
func TestPanicPanicsAndDoesNotPanic(t *testing.T) {
if got, want := doesPanic(2, func() {}, "bar"), "\tassert.go:65: did not panic\n"; got != want {
t.Fatalf("got %q want %q", got, want)
}
}
func TestMatchesMatches(t *testing.T) {
if got, want := matches(2, "aaa", "a"), ""; got != want {
t.Fatalf("got %q want %q", got, want)
}
}
func TestMatchesDoesNotMatch(t *testing.T) {
if got, want := matches(2, "aaa", "b"), "\tassert_test.go:52: got aaa which does not match b\n"; got != want {
t.Fatalf("got %q want %q", got, want)
}
}

10
agent-common/go.mod Normal file
View File

@@ -0,0 +1,10 @@
module wdd.io/agent-common
go 1.22.1
require (
go.uber.org/zap v1.27.0
gopkg.in/yaml.v3 v3.0.1
)
require go.uber.org/multierr v1.10.0 // indirect

16
agent-common/go.sum Normal file
View File

@@ -0,0 +1,16 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -0,0 +1,85 @@
package logger
import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Logger struct represents a zap-based logger.
type Logger struct {
*zap.Logger
}
var Log, _ = NewLogger()
// NewLogger creates a new Logger instance.
func NewLogger() (*Logger, error) {
config := zap.Config{
Encoding: "json",
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
OutputPaths: []string{"stdout"}, // 输出到控制台
ErrorOutputPaths: []string{"stderr"},
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
TimeKey: "time",
//CallerKey: "caller",
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
//EncodeCaller: zapcore.ShortCallerEncoder,
},
Development: true,
}
logger, err := config.Build()
if err != nil {
return nil, err
}
return &Logger{logger}, nil
}
func (l *Logger) Printf(msg string, args ...interface{}) {
l.Logger.Info(fmt.Sprintf("%s ==> %v", msg, args))
}
// Debug logs a debug message.
func (l *Logger) Debug(msg string, fields ...zap.Field) {
l.Logger.Debug(msg, fields...)
}
func (l *Logger) DebugF(msg string, args ...interface{}) {
l.Logger.Debug(fmt.Sprintf(msg, args...))
}
// Info logs an info message.
func (l *Logger) Info(msg string, fields ...zap.Field) {
l.Logger.Info(msg, fields...)
}
// InfoF logs an info message with format
func (l *Logger) InfoF(msg string, args ...interface{}) {
l.Logger.Info(fmt.Sprintf(msg, args...))
}
// Warn logs a warning message.
func (l *Logger) Warn(msg string, fields ...zap.Field) {
l.Logger.Warn(msg, fields...)
}
func (l *Logger) WarnF(msg string, args ...interface{}) {
l.Logger.Warn(fmt.Sprintf(msg, args...))
}
// Error logs an error message.
func (l *Logger) ErrorF(msg string, args ...interface{}) {
l.Logger.Error(fmt.Sprintf(msg, args...))
}
// Fatal logs a fatal message and exits the program with a non-zero status code.
func (l *Logger) Fatal(msg string, fields ...zap.Field) {
l.Logger.Fatal(msg, fields...)
}

View File

@@ -0,0 +1,165 @@
package message_pusher
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"regexp"
"strings"
"wdd.io/agent-common/logger"
)
var (
topicRegex = regexp.MustCompile(`^[-_A-Za-z0-9]{1,64}$`) // Same as in server/server.go
log = logger.Log
)
const (
maxResponseBytes = 4096
)
type Client struct {
config *Config
}
// Message is a struct that represents a ntfy message
type Message struct { // TODO combine with server.message
ID string
Event string
Time int64
Topic string
Message string
Title string
Priority int
Tags []string
Click string
Icon string
Attachment *Attachment
// Additional fields
TopicURL string
SubscriptionID string
Raw string
}
// Attachment represents a message attachment
type Attachment struct {
Name string `json:"name"`
Type string `json:"type,omitempty"`
Size int64 `json:"size,omitempty"`
Expires int64 `json:"expires,omitempty"`
URL string `json:"url"`
Owner string `json:"-"` // IP address of uploader, used for rate limiting
}
// New creates a new Client using a given Config
func New(config *Config) *Client {
return &Client{
config: config,
}
}
func NewDefaultClient() *Client {
defaultConfig := NewDefaultConfig()
return New(defaultConfig)
}
func (c *Client) PublishDefault(message bytes.Buffer, options []PublishOption) (*Message, error) {
if c.config.DefaultTopic == "" {
return nil, errors.New("[PublishDefault] - topic empty")
}
// parse default
options = c.parseConfigToOption(options)
return c.PublishReader(c.config.DefaultTopic, bytes.NewReader(message.Bytes()), options)
}
// Publish sends a message to a specific topic, optionally using options.
// See PublishReader for details.
func (c *Client) Publish(topic, message string, options []PublishOption) (*Message, error) {
return c.PublishReader(topic, strings.NewReader(message), options)
}
// PublishReader sends a message to a specific topic, optionally using options.
//
// A topic can be either a full URL (e.g. https://myhost.lan/mytopic), a short URL which is then prepended https://
// (e.g. myhost.lan -> https://myhost.lan), or a short name which is expanded using the default host in the
// config (e.g. mytopic -> https://ntfy.sh/mytopic).
//
// To pass title, priority and tags, check out WithTitle, WithPriority, WithTagsList, WithDelay, WithNoCache,
// WithNoFirebase, and the generic WithHeader.
func (c *Client) PublishReader(topic string, body io.Reader, options []PublishOption) (*Message, error) {
topicURL, err := c.expandTopicURL(topic)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", topicURL, body)
if err != nil {
return nil, err
}
for _, option := range options {
if err := option(req); err != nil {
return nil, err
}
}
log.DebugF("%s Publishing message with headers %s", topicURL, req.Header)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := io.ReadAll(io.LimitReader(resp.Body, maxResponseBytes))
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New(strings.TrimSpace(string(b)))
}
m, err := toMessage(string(b), topicURL, "")
if err != nil {
return nil, err
}
return m, nil
}
func (c *Client) expandTopicURL(topic string) (string, error) {
if strings.HasPrefix(topic, "http://") || strings.HasPrefix(topic, "https://") {
return topic, nil
} else if strings.Contains(topic, "/") {
return fmt.Sprintf("https://%s", topic), nil
}
if !topicRegex.MatchString(topic) {
return "", fmt.Errorf("invalid topic name: %s", topic)
}
return fmt.Sprintf("%s/%s", c.config.DefaultHost, topic), nil
}
func (c *Client) parseConfigToOption(options []PublishOption) []PublishOption {
config := c.config
if config.DefaultToken != "" {
options = append(options, WithBearerAuth(config.DefaultToken))
} else if config.DefaultUser != "" {
if *config.DefaultPassword != "" {
options = append(options, WithBasicAuth(config.DefaultUser, *config.DefaultPassword))
} else {
log.ErrorF("[parseConfigToOption] - default password is empty!")
}
}
return options
}
func toMessage(s, topicURL, subscriptionID string) (*Message, error) {
var m *Message
if err := json.NewDecoder(strings.NewReader(s)).Decode(&m); err != nil {
return nil, err
}
m.TopicURL = topicURL
m.SubscriptionID = subscriptionID
m.Raw = s
return m, nil
}

View File

@@ -0,0 +1,34 @@
package message_pusher
import (
"testing"
"wdd.io/agent-common/utils"
)
func TestClient_Publish(t *testing.T) {
client := NewDefaultClient()
optionList := []PublishOption{
WithTitle("测试内容"),
WithPriority("5"),
WithMarkdown(),
}
deployPush := DeployPush{
Namespace: "uavcloud-dev",
AppName: "cmii-uav-platform",
Replicas: "1",
DeployStatus: false,
}
deployPush.ParseCmiiDeployTemplate()
message, err := client.PublishDefault(deployPush.ParseCmiiDeployTemplate(), optionList)
if err != nil {
return
}
utils.BeautifulPrint(message)
}

View File

@@ -0,0 +1,62 @@
package message_pusher
import (
"gopkg.in/yaml.v3"
"os"
)
const (
// DefaultBaseURL is the base URL used to expand short topic names
DefaultBaseURL = "https://push.107421.xyz"
DefaultBaseToken = "tk_zvdb67fwj1hrjivkq3ga9z7u63av5"
DefaultTopic = "cmii"
)
// Config is the config struct for a Client
type Config struct {
DefaultHost string `yaml:"default-host"`
DefaultUser string `yaml:"default-user"`
DefaultPassword *string `yaml:"default-password"`
DefaultToken string `yaml:"default-token"`
DefaultCommand string `yaml:"default-command"`
DefaultTopic string `yaml:"default-topic"`
Subscribe []Subscribe `yaml:"subscribe"`
}
// Subscribe is the struct for a Subscription within Config
type Subscribe struct {
Topic string `yaml:"topic"`
User *string `yaml:"user"`
Password *string `yaml:"password"`
Token *string `yaml:"token"`
Command string `yaml:"command"`
If map[string]string `yaml:"if"`
}
// NewDefaultConfig creates a new Config struct for a Client
func NewDefaultConfig() *Config {
return &Config{
DefaultHost: DefaultBaseURL,
DefaultUser: "",
DefaultPassword: nil,
DefaultToken: DefaultBaseToken,
DefaultTopic: DefaultTopic,
DefaultCommand: "",
Subscribe: nil,
}
}
// LoadConfig loads the Client config from a yaml file
func LoadConfig(filename string) (*Config, error) {
b, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
c := NewDefaultConfig()
if err := yaml.Unmarshal(b, c); err != nil {
return nil, err
}
return c, nil
}

View File

@@ -0,0 +1,204 @@
package message_pusher
import (
"encoding/base64"
"fmt"
"net/http"
"strings"
"time"
)
// RequestOption is a generic request option that can be added to Client calls
type RequestOption = func(r *http.Request) error
// PublishOption is an option that can be passed to the Client.Publish call
type PublishOption = RequestOption
// SubscribeOption is an option that can be passed to a Client.Subscribe or Client.Poll call
type SubscribeOption = RequestOption
// WithMessage sets the notification message. This is an alternative way to passing the message body.
func WithMessage(message string) PublishOption {
return WithHeader("X-Message", message)
}
// WithTitle adds a title to a message
func WithTitle(title string) PublishOption {
return WithHeader("X-Title", title)
}
// WithPriority adds a priority to a message. The priority can be either a number (1=min, 5=max),
// or the corresponding names (see util.ParsePriority).
func WithPriority(priority string) PublishOption {
return WithHeader("X-Priority", priority)
}
// WithTagsList adds a list of tags to a message. The tags parameter must be a comma-separated list
// of tags. To use a slice, use WithTags instead
func WithTagsList(tags string) PublishOption {
return WithHeader("X-Tags", tags)
}
// WithTags adds a list of a tags to a message
func WithTags(tags []string) PublishOption {
return WithTagsList(strings.Join(tags, ","))
}
// WithDelay instructs the server to send the message at a later date. The delay parameter can be a
// Unix timestamp, a duration string or a natural langage string. See https://ntfy.sh/docs/publish/#scheduled-delivery
// for details.
func WithDelay(delay string) PublishOption {
return WithHeader("X-Delay", delay)
}
// WithClick makes the notification action open the given URL as opposed to entering the detail view
func WithClick(url string) PublishOption {
return WithHeader("X-Click", url)
}
// WithIcon makes the notification use the given URL as its icon
func WithIcon(icon string) PublishOption {
return WithHeader("X-Icon", icon)
}
// WithActions adds custom user actions to the notification. The value can be either a JSON array or the
// simple format definition. See https://ntfy.sh/docs/publish/#action-buttons for details.
func WithActions(value string) PublishOption {
return WithHeader("X-Actions", value)
}
// WithAttach sets a URL that will be used by the client to download an attachment
func WithAttach(attach string) PublishOption {
return WithHeader("X-Attach", attach)
}
// WithMarkdown instructs the server to interpret the message body as Markdown
func WithMarkdown() PublishOption {
return WithHeader("X-Markdown", "yes")
}
// WithFilename sets a filename for the attachment, and/or forces the HTTP body to interpreted as an attachment
func WithFilename(filename string) PublishOption {
return WithHeader("X-Filename", filename)
}
// WithEmail instructs the server to also send the message to the given e-mail address
func WithEmail(email string) PublishOption {
return WithHeader("X-Email", email)
}
// WithBasicAuth adds the Authorization header for basic auth to the request
func WithBasicAuth(user, pass string) PublishOption {
return WithHeader("Authorization", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", user, pass)))))
}
// WithBearerAuth adds the Authorization header for Bearer auth to the request
func WithBearerAuth(token string) PublishOption {
return WithHeader("Authorization", fmt.Sprintf("Bearer %s", token))
}
// WithEmptyAuth clears the Authorization header
func WithEmptyAuth() PublishOption {
return RemoveHeader("Authorization")
}
// WithNoCache instructs the server not to cache the message server-side
func WithNoCache() PublishOption {
return WithHeader("X-Cache", "no")
}
// WithNoFirebase instructs the server not to forward the message to Firebase
func WithNoFirebase() PublishOption {
return WithHeader("X-Firebase", "no")
}
// WithSince limits the number of messages returned from the server. The parameter since can be a Unix
// timestamp (see WithSinceUnixTime), a duration (WithSinceDuration) the word "all" (see WithSinceAll).
func WithSince(since string) SubscribeOption {
return WithQueryParam("since", since)
}
// WithSinceAll instructs the server to return all messages for the given topic from the server
func WithSinceAll() SubscribeOption {
return WithSince("all")
}
// WithSinceDuration instructs the server to return all messages since the given duration ago
func WithSinceDuration(since time.Duration) SubscribeOption {
return WithSinceUnixTime(time.Now().Add(-1 * since).Unix())
}
// WithSinceUnixTime instructs the server to return only messages newer or equal to the given timestamp
func WithSinceUnixTime(since int64) SubscribeOption {
return WithSince(fmt.Sprintf("%d", since))
}
// WithPoll instructs the server to close the connection after messages have been returned. Don't use this option
// directly. Use Client.Poll instead.
func WithPoll() SubscribeOption {
return WithQueryParam("poll", "1")
}
// WithScheduled instructs the server to also return messages that have not been sent yet, i.e. delayed/scheduled
// messages (see WithDelay). The messages will have a future date.
func WithScheduled() SubscribeOption {
return WithQueryParam("scheduled", "1")
}
// WithFilter is a generic subscribe option meant to be used to filter for certain messages only
func WithFilter(param, value string) SubscribeOption {
return WithQueryParam(param, value)
}
// WithMessageFilter instructs the server to only return messages that match the exact message
func WithMessageFilter(message string) SubscribeOption {
return WithQueryParam("message", message)
}
// WithTitleFilter instructs the server to only return messages with a title that match the exact string
func WithTitleFilter(title string) SubscribeOption {
return WithQueryParam("title", title)
}
// WithPriorityFilter instructs the server to only return messages with the matching priority. Not that messages
// without priority also implicitly match priority 3.
func WithPriorityFilter(priority int) SubscribeOption {
return WithQueryParam("priority", fmt.Sprintf("%d", priority))
}
// WithTagsFilter instructs the server to only return messages that contain all of the given tags
func WithTagsFilter(tags []string) SubscribeOption {
return WithQueryParam("tags", strings.Join(tags, ","))
}
// WithHeader is a generic option to add headers to a request
func WithHeader(header, value string) RequestOption {
return func(r *http.Request) error {
if value != "" {
r.Header.Set(header, value)
}
return nil
}
}
// WithQueryParam is a generic option to add query parameters to a request
func WithQueryParam(param, value string) RequestOption {
return func(r *http.Request) error {
if value != "" {
q := r.URL.Query()
q.Add(param, value)
r.URL.RawQuery = q.Encode()
}
return nil
}
}
// RemoveHeader is a generic option to remove a header from a request
func RemoveHeader(header string) RequestOption {
return func(r *http.Request) error {
if header != "" {
delete(r.Header, header)
}
return nil
}
}

View File

@@ -0,0 +1,27 @@
package message_pusher
var DefaultClientOp = NewDefaultClient()
var CmiiUpdatePushOptions = []PublishOption{
WithTitle("更新应用"),
WithPriority("3"),
}
func PushCmiiUpdateMessage(cmiiEnv, appName, newTag string, updateStatus bool) {
deployPush := DeployPush{
Namespace: cmiiEnv,
AppName: appName,
DeployStatus: updateStatus,
ToTag: newTag,
}
deployPush.ParseCmiiDeployTemplate()
_, err := DefaultClientOp.PublishDefault(deployPush.ParseCmiiDeployTemplate(), CmiiUpdatePushOptions)
if err != nil {
log.ErrorF("[PushCmiiUpdateMessage] - message push error %s", err.Error())
return
}
}

View File

@@ -0,0 +1,43 @@
package message_pusher
import (
"bytes"
"text/template"
)
const cmiiDeployTemplate = `
{{if .DeployStatus}}
部署状态: 成功😍
{{- else }}
部署状态: 失败👿👿👿
{{- end}}
命名空间: {{.Namespace}}
应用名称: {{.AppName}}
副本数量: {{.Replicas}}
目标版本: {{.ToTag}}
`
type DeployPush struct {
Namespace string
AppName string
Replicas string
DeployStatus bool
ToTag string
}
func (d DeployPush) ParseCmiiDeployTemplate() bytes.Buffer {
// 解析模板
tmpl, err := template.New("cmiiDeployTemplate").Parse(cmiiDeployTemplate)
if err != nil {
panic(err)
}
// 应用数据并打印结果
var result bytes.Buffer
err = tmpl.Execute(&result, d)
if err != nil {
panic(err)
}
return result
}

View File

@@ -0,0 +1,20 @@
package message_pusher
import (
"fmt"
"testing"
)
func TestDeployPush_ParseCmiiDeployTemplate(t *testing.T) {
deployPush := DeployPush{
Namespace: "casc",
AppName: "sdasdas",
Replicas: "dasdasd",
DeployStatus: false,
ToTag: "5.4.0",
}
template := deployPush.ParseCmiiDeployTemplate()
fmt.Println(template.String())
}

View File

@@ -0,0 +1,83 @@
package utils
import (
"io"
"os"
)
// AppendSourceToFile 将源文件的内容添加到目标文件使用golang标准库完成跨平台、安全性更强
func AppendSourceToFile(sourceFile, targetFile string) bool {
// 打开源文件
source, err := os.Open(sourceFile)
if err != nil {
log.ErrorF("[BasicAppendSourceToFile] - error open source file => %s, error is %s", sourceFile, err.Error())
return false
}
defer source.Close()
// 打开目标文件,如果不存在则创建,如果存在则在末尾追加
target, err := os.OpenFile(targetFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.ErrorF("[BasicAppendSourceToFile] - error open target file => %s, error is %s", sourceFile, err.Error())
return false
}
defer target.Close()
// 将源文件内容复制到目标文件
_, err = io.Copy(target, source)
if err != nil {
log.ErrorF("[BasicAppendSourceToFile] - Error appending to target file: %s", err.Error())
return false
}
return true
}
// AppendContentToFile 向目标文件中追加写入一些内容
func AppendContentToFile(content string, targetFile string) bool {
// 打开文件用于追加。如果文件不存在,将会创建一个新文件。
file, err := os.OpenFile(targetFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.ErrorF("[BasicAppendContentToFile] - Error opening file: %s , error is %s", targetFile, err.Error())
return false
}
defer file.Close() // 确保文件最终被关闭
// 写入内容到文件
if _, err := file.WriteString(content); err != nil {
log.ErrorF("[BasicAppendContentToFile] - Error writing to file: %s , error is %s", targetFile, err.Error())
return false
}
return true
}
// AppendNullToFile 清空一个文件
func AppendNullToFile(targetFile string) bool {
// 使用os.O_TRUNC清空文件内容
file, err := os.OpenFile(targetFile, os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
log.ErrorF("[AppendNullToFile] - Error opening file: %s, error is %s", targetFile, err.Error())
return false
}
defer file.Close() // 确保在函数退出前关闭文件
return true
}
func WordSpaceCompletion(source string, totalLength int) string {
sourceLength := len(source)
if sourceLength >= totalLength {
return source
}
for i := 0; i < totalLength-sourceLength; i++ {
source += " "
}
return source
}

View File

@@ -0,0 +1,22 @@
package utils
func MinInt(x, y int) int {
if x < y {
return x
}
return y
}
func MaxInt32(x, y int32) int32 {
if x > y {
return x
}
return y
}
func MaxInt(x, y int) int {
if x > y {
return x
}
return y
}

View File

@@ -0,0 +1,49 @@
package utils
import (
"encoding/json"
"fmt"
"wdd.io/agent-common/logger"
)
var log = logger.Log
func BeautifulPrint(object interface{}) {
bytes, err := json.MarshalIndent(object, "", " ")
if err != nil {
log.ErrorF("[BeautifulPrint] - json marshal error ! => %v", object)
}
fmt.Println()
fmt.Println(string(bytes))
fmt.Println()
}
func BeautifulPrintToString(object interface{}) string {
bytes, err := json.MarshalIndent(object, "", " ")
if err != nil {
log.ErrorF("[BeautifulPrint] - json marshal error ! => %v", object)
}
return string(bytes)
}
func BeautifulPrintListWithTitle(contend []string, title string) {
fmt.Println()
fmt.Println(fmt.Sprintf("content tile is => %s", title))
for _, line := range contend {
bytes, _ := json.MarshalIndent(line, "", " ")
fmt.Println(string(bytes))
}
fmt.Println("---------- end -----------")
}
func SplitLinePrint() {
fmt.Println()
fmt.Println()
fmt.Println()
}

View File

@@ -0,0 +1,29 @@
package utils
import (
"time"
)
// ParseDateTimeTime 输出系统时间的格式为"2006-01-02 15:04:05"形式的时间字符串
func ParseDateTimeTime() string {
now := time.Now()
/*loc := time.FixedZone("UTC+8", 8*60*60) // 创建东八区时区对象
localTime := now.In(loc) // 转换为东八区时间*/
return now.Format(time.DateTime)
}
// ParseISOLocalDateTime 时间格式为2023-08-11T10:48:15+08:00
func ParseISOLocalDateTime() string {
now := time.Now()
return now.Format(time.RFC3339)
}
func TimeSplitFormatString() string {
now := time.Now()
formattedTime := now.Format("2006-01-02-15-04-05")
return formattedTime
}