utils

package module
v0.2.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2022 License: MulanPSL-2.0 Imports: 24 Imported by: 4

README

utils

Go utilities

script

git clone https://codehub-y.huawei.com/dcs/container/utils.git

cd utils
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${HOME}/script_test cmd/script_test/main.go

chmod +x ${HOME}/script_test

${HOME}/script_test -path "${HOME}/test.sh" -args "-m hello" -env "EXIT_CODE_OK=0 ERROR_CODE_INVALID=1" -out "${HOME}/test.out" -dir "${HOME}"

Documentation

Overview

Copyright (c) 2022 Yaohui Wang (yaohuiwang@outlook.com) utils is licensed under Mulan PubL v2. You can use this software according to the terms and conditions of the Mulan PubL v2. You may obtain a copy of Mulan PubL v2 at:

http://license.coscl.org.cn/MulanPubL-2.0

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PubL v2 for more details.

Copyright (c) 2022 Yaohui Wang (yaohuiwang@outlook.com) utils is licensed under Mulan PubL v2. You can use this software according to the terms and conditions of the Mulan PubL v2. You may obtain a copy of Mulan PubL v2 at:

http://license.coscl.org.cn/MulanPubL-2.0

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PubL v2 for more details.

Index

Constants

View Source
const (
	LOG_DEBUG int = iota
	LOG_INFO
	LOG_WARN
	LOG_ERROR
)
View Source
const (
	MODE_PERM_RW fs.FileMode = 0600
	MODE_PERM_RO fs.FileMode = 0400
)
View Source
const (
	STATUS_UP      string = "up"
	STATUS_DOWN    string = "down"
	STATUS_PENDING string = "pending"
)
View Source
const (
	ScriptStatusWaiting = "waiting"
	ScriptStatusRunning = "running"
	ScriptStatusExited  = "exited"
	ScriptStatusKilled  = "killed"
	ScriptStatusFailed  = "failed_start"
)
View Source
const (
	DefaultThreadQueueSize int = 64
	DefaultThreadPoolSize  int = 8
)

Variables

View Source
var (
	Logger   *log.Logger = log.Default()
	LogLevel int         = LOG_DEBUG
	LogLabel []string    = []string{"DEBUG", "INFO", "WARN", "ERROR"}
)

Functions

func AllocIPFromCidr

func AllocIPFromCidr(cidr string, exist []string) (*string, error)

allocate ip from cidr

func AllocIPFromRange

func AllocIPFromRange(start string, end string, exist []string) (*string, error)

allocate ip from range

func CreateFile

func CreateFile(target string, data ...string) error

func DelFileIfExist

func DelFileIfExist(f string) error

func Equal added in v0.2.0

func Equal(a interface{}, b interface{}) bool

func ExecCommand

func ExecCommand(cmd string) (int, error)

func FileExist

func FileExist(f string) bool

func GetIPStatus

func GetIPStatus(ipaddress string, prefix *uint8, dev *string) string

func GetInterfaceOfIP

func GetInterfaceOfIP(ipaddress string) (*string, *int, error)

func GetInterfaceStatus

func GetInterfaceStatus(dev *string) string

func GetProcStatus

func GetProcStatus(pidFile string) string

func IpInNetwork

func IpInNetwork(ipstr string, cidr string) bool

cidr contai

func Ipv4ToUint32 added in v0.1.9

func Ipv4ToUint32(ipv4 string) (uint32, error)

convert ipv4 to unit32

func ListContains

func ListContains(slice []string, obj string) bool

func ListDelete

func ListDelete(slice []string, obj string) (result []string)

func LogPrintf

func LogPrintf(level int, module string, format string, v ...interface{})

func MergeFiles

func MergeFiles(target string, files ...string) error

func MkdirIfNotExist

func MkdirIfNotExist(path string) error

func RandNum

func RandNum(max int) int

func RandomString

func RandomString(len int) string

func Uint32ToIpv4 added in v0.1.10

func Uint32ToIpv4(ipv4Uint32 uint32) string

convert unit32 to ipv4

func WriteFile

func WriteFile(target string, flag int, mode os.FileMode, data ...string) error

Types

type Bitmap added in v0.2.6

type Bitmap struct {
	// contains filtered or unexported fields
}

func FromBytes added in v0.2.6

func FromBytes(buffer []byte, cap uint32) *Bitmap

FromBytes reads a bitmap from a byte buffer without copying the buffer.

func FromString added in v0.2.6

func FromString(hexString string, cap uint32) (*Bitmap, error)

FromString decodes the received string and loads it to bitmap object

func NewBitmap added in v0.2.6

func NewBitmap(size uint32) *Bitmap

func (*Bitmap) And added in v0.2.6

func (a *Bitmap) And(other Bitmap, extra ...Bitmap)

And computes the intersection between two bitmaps and stores the result in the current bitmap

func (*Bitmap) AndNot added in v0.2.6

func (a *Bitmap) AndNot(other Bitmap, extra ...Bitmap)

AndNot computes the difference between two bitmaps and stores the result in the current bitmap. Operation works as set subtract: a - b

func (*Bitmap) Clear added in v0.2.6

func (b *Bitmap) Clear()

Clear clears the bitmap and resizes it to zero.

func (*Bitmap) Contains added in v0.2.6

func (b *Bitmap) Contains(x uint32) bool

Contains checks whether a value is contained in the bitmap or not.

func (*Bitmap) Count added in v0.2.6

func (b *Bitmap) Count() int

Count returns the number of elements in this bitmap

func (*Bitmap) CountTo added in v0.2.6

func (b *Bitmap) CountTo(until uint32) int

CountTo counts the number of elements in the bitmap up until the specified index. If until is math.MaxUint32, it will return the count. The count is non-inclusive of the index.

func (*Bitmap) Grow added in v0.2.6

func (b *Bitmap) Grow(desiredBit uint32)

Grow grows the bitmap size until we reach the desired bit.

func (*Bitmap) MarshalJSON added in v0.2.6

func (b *Bitmap) MarshalJSON() ([]byte, error)

func (*Bitmap) Max added in v0.2.6

func (b *Bitmap) Max() (uint32, bool)

Max get the largest value stored in this bitmap, assuming the bitmap is not empty.

func (*Bitmap) MaxZero added in v0.2.6

func (b *Bitmap) MaxZero() (uint32, bool)

MaxZero get the last zero bit and return its index, assuming bitmap is not empty

func (*Bitmap) Min added in v0.2.6

func (b *Bitmap) Min() (uint32, bool)

Min get the smallest value stored in this bitmap, assuming the bitmap is not empty.

func (*Bitmap) MinZero added in v0.2.6

func (b *Bitmap) MinZero() (uint32, bool)

MinZero finds the first zero bit and returns its index, assuming the bitmap is not empty.

func (*Bitmap) Ones added in v0.2.6

func (b *Bitmap) Ones()

Ones sets the entire bitmap to one.

func (*Bitmap) Or added in v0.2.6

func (a *Bitmap) Or(other Bitmap, extra ...Bitmap)

Or computes the union between two bitmaps and stores the result in the current bitmap

func (*Bitmap) Remove added in v0.2.6

func (b *Bitmap) Remove(x uint32)

Remove removes the bit x from the bitmap, but does not shrink it.

func (*Bitmap) Set added in v0.2.6

func (b *Bitmap) Set(x uint32)

Set sets the bit x in the bitmap and grows it if necessary.

func (*Bitmap) ToBytes added in v0.2.6

func (b *Bitmap) ToBytes() (out []byte)

ToBytes converts the bitmap to binary representation without copying the underlying data. The output buffer should not be modified, since it would also change the bitmap.

func (*Bitmap) ToHexBitmap added in v0.2.6

func (b *Bitmap) ToHexBitmap() *HexBitmap

func (*Bitmap) ToString added in v0.2.6

func (b *Bitmap) ToString() string

ToString returns encoded string representation for the bitmap

func (*Bitmap) UnmarshalJSON added in v0.2.6

func (b *Bitmap) UnmarshalJSON(data []byte) (err error)

func (*Bitmap) Xor added in v0.2.6

func (a *Bitmap) Xor(other Bitmap, extra ...Bitmap)

Xor computes the symmetric difference between two bitmaps and stores the result in the current bitmap

type Gossip

type Gossip struct {
	Name                    string
	BindAddr                string
	BindPort                int
	SecretKey               []byte
	ProbeInterval           int
	SyncInterval            int
	RetransmitMult          int
	Ready                   bool
	NotifyJoinHandler       func(*memberlist.Node)
	NotifyLeaveHandler      func(*memberlist.Node)
	NotifyUpdateHandler     func(*memberlist.Node)
	InvalidatesHandler      func(memberlist.Broadcast) bool
	NodeMetaHandler         func() []byte
	NotifyMsgHandler        func([]byte)
	LocalStateHandler       func() []byte
	MergeRemoteStateHandler func([]byte)
	// contains filtered or unexported fields
}

func DefaultGossip

func DefaultGossip() *Gossip

Generage a default broadcast, need to set handlers:

  • NotifyMsgHandler
  • LocalStateHandler
  • MergeRemoteStateHandler

func GossipWith

func GossipWith(name string, bindAddr string, bindPort int) *Gossip

func (*Gossip) Broadcast

func (g *Gossip) Broadcast(msg []byte)

Broadcast message

func (*Gossip) Start

func (g *Gossip) Start(members *string) error

Start gossip

type HexBitmap added in v0.2.1

type HexBitmap struct {
	Size uint32 `json:"size,omitempty"`
	Bits string `json:"bits,omitempty"`
}

func (*HexBitmap) Grow added in v0.2.1

func (h *HexBitmap) Grow(x uint32) error

func (*HexBitmap) Remove added in v0.2.1

func (h *HexBitmap) Remove(x uint32) error

func (*HexBitmap) Set added in v0.2.1

func (h *HexBitmap) Set(x uint32) error

func (*HexBitmap) ToBitmap added in v0.2.1

func (h *HexBitmap) ToBitmap() (*Bitmap, error)

type Runnable

type Runnable interface {
	Run()
}

type Script

type Script struct {
	Spec   *ScriptSpec
	Cmd    *exec.Cmd
	Status string

	Runnable
	// contains filtered or unexported fields
}

func NewScript

func NewScript(s *ScriptSpec, callback func()) (*Script, error)

func (*Script) Expect

func (s *Script) Expect(regex string, timeout time.Duration) bool

func (*Script) Input

func (s *Script) Input(line string) error

func (*Script) Run

func (s *Script) Run()

func (*Script) Wait

func (s *Script) Wait()

type ScriptSpec

type ScriptSpec struct {
	Path        string
	Dir         string
	Args        []string
	Env         []string
	SysProcAttr *syscall.SysProcAttr // &syscall.SysProcAttr{Setpgid: true}
	Out         string
}

type ThreadPool

type ThreadPool struct {
	QueueSize int
	PoolSize  int
	Queue     chan Runnable
	// contains filtered or unexported fields
}

func DefaultThreadPool

func DefaultThreadPool() *ThreadPool

func NewThreadPool

func NewThreadPool(QueueSize int, PoolSize int) *ThreadPool

func (*ThreadPool) Destroy

func (p *ThreadPool) Destroy()

func (*ThreadPool) Init

func (p *ThreadPool) Init()

func (*ThreadPool) Put

func (p *ThreadPool) Put(r Runnable) error

func (*ThreadPool) Wait

func (p *ThreadPool) Wait()

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL