Skip to content

Instantly share code, notes, and snippets.

View Prnyself's full-sized avatar
🐱
Meow

Lance Prnyself

🐱
Meow
View GitHub Profile
@Prnyself
Prnyself / copy.go
Last active May 7, 2020 07:58
copy with storage
package main
import (
"fmt"
"os"
"time"
"github.com/Xuanwo/storage"
"github.com/Xuanwo/storage/pkg/credential"
"github.com/Xuanwo/storage/pkg/endpoint"
@Prnyself
Prnyself / cat_test.go
Created March 27, 2020 09:18
test action by monkey
func TestCatRun(t *testing.T) {
tmpErr := errors.New("temp err")
cases := []struct {
name string
input string
parseErr error
runErr error
}{
{
name: "normal",
@Prnyself
Prnyself / storager.go
Created February 27, 2020 10:14
handle work dir
func (s *Storage) getAbsPath(path string) string {
return strings.TrimPrefix(strings.TrimSuffix(s.workDir, "/")+"/"+path, "/")
}
func (s *Storage) getRelPath(path string) string {
return strings.TrimPrefix(path, strings.TrimSuffix(s.workDir, "/")+"/")
}
package utils
import (
"path/filepath"
"strings"
)
var separator = "/"
// ParseWd get a path as input, split the work dir and file by following rules
@Prnyself
Prnyself / storage.go
Created November 6, 2019 08:54
handle cp base and path in different conditions
func HandleStorageBaseAndPath(t *taskutils.BetweenStorageTask) error {
// In operation cp, we set source storage to dir of the source path.
srcPath, err := filepath.Abs(t.GetSourcePath())
if err != nil {
return err
}
if err = t.GetSourceStorage().Init(types.WithBase(filepath.Dir(srcPath))); err != nil {
return err
}
t.SetSourcePath(filepath.Base(srcPath))
@Prnyself
Prnyself / ModifyStruct.go
Last active September 3, 2019 06:35
ModifyStruct uses reflect to modify a struct
// ModifyStruct uses reflect to modify a struct.
// v is a pointer to a struct,
// m supports the map to modify the struct,
// myTag is used to determine which field to modify by its tag,
// if myTag == "", modify by its name.
func ModifyStruct(v interface{}, m StringMapper, myTag string) error {
if reflect.TypeOf(v).Kind() != reflect.Ptr || // v should be a pointer
reflect.TypeOf(v).Elem().Kind() != reflect.Struct { // to a struct
return InvalidStructErr
}
@Prnyself
Prnyself / cp.go
Created August 8, 2019 10:17
set params from cmd
package main
import (
"github.com/spf13/cobra"
"github.com/yunify/qsctl/v2/action"
"github.com/yunify/qsctl/v2/utils"
)
// CpCommand will handle copy command.
@Prnyself
Prnyself / copy.go
Created August 8, 2019 10:17
extend base flag handler
package action
import (
"bufio"
"bytes"
"crypto/md5"
"io"
"os"
"runtime/pprof"
"sync"
@Prnyself
Prnyself / flags.go
Created August 8, 2019 10:16
base flag handler
package action
// FlagHandler contains all flags from cmd
type FlagHandler struct {
Bench bool `json:"bench"`
ExpectSize int64 `json:"expect_size"`
Format string `json:"format"`
HumanReadable bool `json:"human_readable"`
LongFormat bool `json:"long_format"`
MaximumMemoryContent int64 `json:"maximum_memory_content"`
package action
import (
"io"
"io/ioutil"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"