package main import ( "bufio" "bytes" "fmt" "github.com/BurntSushi/toml" "io" "math/rand" "net" "os" "os/exec" "strings" "time" ) // https://www.tizi365.com/archives/319.html // Config 我们先根据toml配置文件定义struct // 他们的关系都是一一对应,toml配置文件定义的字段是什么类型,struct字段就是什么类型 type Cargo struct { Package struct { Name string `toml:"name"` Version string `toml:"version"` Authors []string `toml:"authors"` Edition string `toml:"edition"` Description string `toml:"description"` License string `toml:"license"` } `toml:"package"` Dependencies struct { } `toml:"dependencies"` } func main() { fi, err := os.Open("/Users/fliter/go/src/shuang/httpp/no_reg_pure.txt") if err != nil { fmt.Printf("Error: %s\n", err) return } defer fi.Close() br := bufio.NewReader(fi) for { a, _, c := br.ReadLine() if c == io.EOF { break } keyword := string(a) //keyword = "adult" deal2(keyword) //生成随机数 min := int32(10) //设置随机数下限 max := int32(100) //设置随机数上限 rand.Seed(time.Now().UnixNano()) num := rand.Int31n(max-min-1) + min + 1 time.Sleep(time.Duration(num) * time.Second) //time.Sleep(30 * time.Second) //break } } func deal2(keyword string) { // 首先初始化struct对象,用于保存配置文件信息 var conf Cargo // 通过toml.DecodeFile将toml配置文件的内容,解析到struct对象 if _, err := toml.DecodeFile("/Users/fliter/rust-lab/hello-rust/Cargo.toml", &conf); err != nil { // handle error } // 可以通过conf读取配置 fmt.Println(conf.Package.Name) conf.Package.Name = strings.TrimSpace(keyword) //TestEncodeRoundTrip() var ( firstBuffer bytes.Buffer ) err := toml.NewEncoder(&firstBuffer).Encode(conf) if err != nil { fmt.Println("err:", err) } fmt.Println(firstBuffer.String()) if errW := WriteToFile("/Users/fliter/rust-lab/hello-rust/Cargo.toml", firstBuffer.String()); errW != nil { fmt.Printf("keyword%s, errW%v", keyword, errW) } //执行命令行操作 command := exec.Command("sh", "./operate.sh") //errC := command.Run() //if errC != nil{ // fmt.Println(errC.Error(),errC) //} var out bytes.Buffer var stderr bytes.Buffer command.Stdout = &out command.Stderr = &stderr errC := command.Run() if errC != nil { fmt.Println(fmt.Sprint(errC) + ": " + stderr.String()) return } fmt.Println("Result: " + out.String()) // ////执行命令行操作 //command := exec.Command("cd", "/Users/fliter/rust-lab/hello-rust") ////errC := command.Run() ////if errC != nil{ //// fmt.Println(errC.Error(),errC) ////} // //var out bytes.Buffer //var stderr bytes.Buffer //command.Stdout = &out //command.Stderr = &stderr //errC := command.Run() //if errC != nil { // fmt.Println(fmt.Sprint(errC) + ": " + stderr.String()) // return //} //fmt.Println("Result: " + out.String()) // //command1 := exec.Command("git", "add", "-A") ////errC1 := command1.Run() ////if errC1 != nil{ //// fmt.Println(errC1.Error()) ////} // //var out1 bytes.Buffer //var stderr1 bytes.Buffer //command1.Stdout = &out1 //command1.Stderr = &stderr1 //errC1 := command1.Run() //if errC1 != nil { // fmt.Println(fmt.Sprint(errC1) + ": " + stderr1.String()) // return //} //fmt.Println("Result: " + out1.String()) // // //command2 := exec.Command("git", "commit", "-m", "add new code") ////errC2 := command2.Run() ////if errC2 != nil { //// fmt.Println(errC2.Error()) ////} // //var out2 bytes.Buffer //var stderr2 bytes.Buffer //command2.Stdout = &out2 //command2.Stderr = &stderr2 //errC2 := command2.Run() //if errC2 != nil { // fmt.Println(fmt.Sprint(errC2) + ": " + stderr2.String()) // return //} //fmt.Println("Result: " + out2.String()) // // //time.Sleep(2e9) // //command3 := exec.Command("git", "push", "-f") ////errC3 := command3.Run() ////if errC3 != nil { //// fmt.Println(errC3.Error()) ////} // //var out3 bytes.Buffer //var stderr3 bytes.Buffer //command3.Stdout = &out3 //command3.Stderr = &stderr3 //errC3 := command3.Run() //if errC3 != nil { // fmt.Println(fmt.Sprint(errC3) + ": " + stderr3.String()) // return //} //fmt.Println("Result: " + out3.String()) // // // //command30 := exec.Command("rm", "Cargo.lock") ////errC3 := command3.Run() ////if errC3 != nil { //// fmt.Println(errC3.Error()) ////} // //var out30 bytes.Buffer //var stderr30 bytes.Buffer //command30.Stdout = &out30 //command30.Stderr = &stderr30 //errC30 := command30.Run() //if errC30 != nil { // fmt.Println(fmt.Sprint(errC30) + ": " + stderr30.String()) // return //} //fmt.Println("Result: " + out30.String()) // // // // // //command4 := exec.Command("cargo", "publish") ////errC4 := command4.Run() ////if errC4 != nil { //// fmt.Println(errC4.Error()) ////} // //var out4 bytes.Buffer //var stderr4 bytes.Buffer //command4.Stdout = &out4 //command4.Stderr = &stderr4 //errC4 := command4.Run() //if errC4 != nil { // fmt.Println(fmt.Sprint(errC4) + ": " + stderr4.String()) // return //} //fmt.Println("Result: " + out4.String()) } func WriteToFile(fileName string, content string) error { f, err := os.OpenFile(fileName, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644) if err != nil { fmt.Println("file create failed. err: " + err.Error()) } else { // offset //os.Truncate(filename, 0) //clear n, _ := f.Seek(0, os.SEEK_END) _, err = f.WriteAt([]byte(content), n) //fmt.Println("write succeed!") defer f.Close() } return err } func TestEncodeRoundTrip() { type Config struct { Age int Cats []string Pi float64 Perfection []int DOB time.Time Ipaddress net.IP } var inputs = Config{ Age: 13, Cats: []string{"one", "two", "three"}, Pi: 3.145, Perfection: []int{11, 2, 3, 4}, DOB: time.Now(), Ipaddress: net.ParseIP("192.168.59.254"), } var ( firstBuffer bytes.Buffer secondBuffer bytes.Buffer outputs Config ) err := toml.NewEncoder(&firstBuffer).Encode(inputs) if err != nil { fmt.Println("err:", err) } _, err = toml.Decode(firstBuffer.String(), &outputs) if err != nil { fmt.Println("err:", err) } err = toml.NewEncoder(&secondBuffer).Encode(outputs) if err != nil { fmt.Println("err:", err) } if firstBuffer.String() != secondBuffer.String() { fmt.Println("err:", err) } fmt.Println(firstBuffer.String()) }