You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Victorique/vtqe/tools/generate.go

54 lines
1.2 KiB
Go

5 years ago
package tools
import (
"fmt"
4 years ago
"os"
"time"
5 years ago
"b612.me/starainrt"
"github.com/spf13/cobra"
)
var gencmd = &cobra.Command{
Use: "generate",
Short: "生成随机文件",
Long: "生成指定大小的随机文件",
Run: func(this *cobra.Command, args []string) {
sum, _ := this.Flags().GetInt("sum")
num, _ := this.Flags().GetInt("num")
4 years ago
cap, _ := this.Flags().GetInt("cap")
5 years ago
if len(args) != 1 {
this.Help()
return
}
4 years ago
if num <= 0 {
fmt.Println("num不合法不应该小于1")
os.Exit(2)
}
if sum <= 0 {
fmt.Println("sum不合法不应该小于1")
os.Exit(2)
}
if cap <= 0 {
fmt.Println("cap不合法不应该小于1")
os.Exit(2)
}
err := starainrt.FillWithRandom(args[0], num, cap, sum, func(pect float64) {
fmt.Printf("文件已处理:%f%%\r", pect)
5 years ago
})
if err != nil {
fmt.Println("err:" + err.Error())
}
4 years ago
fmt.Println("文件已处理100.0000000%")
time.Sleep(time.Millisecond * 10)
5 years ago
},
}
func init() {
gencmd.Flags().IntP("sum", "s", 3, "随机的种子组数")
gencmd.Flags().IntP("num", "n", 1024, "生成的文件大小")
4 years ago
gencmd.Flags().IntP("cap", "c", 1048576, "bufcap大小")
5 years ago
gencmd.MarkFlagRequired("num")
Maincmd.AddCommand(gencmd)
}