Created
November 22, 2014 01:17
-
-
Save remh/db82d6b35fe80d87e416 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "os" | |
| "strings" | |
| "io/ioutil" | |
| ) | |
| func check(e error) { | |
| if e != nil { | |
| panic(e) | |
| } | |
| os.Exit(1) | |
| } | |
| func main() { | |
| args := os.Args | |
| if len(args) != 4 { | |
| fmt.Println("Usage: FindReplace.exe path_to_file search_text replace_text\n") | |
| os.Exit(1) | |
| } | |
| filePath := args[1] | |
| searchText := args[2] | |
| replaceText := args[3] | |
| if strings.Trim(replaceText, " ") == "" { | |
| fmt.Println("Repace text can't be empty") | |
| os.Exit(1) | |
| } | |
| parts := strings.Split(replaceText, ":") | |
| if len(parts) == 2 && strings.Trim(parts[1], " ") == "" { | |
| fmt.Println("You can't specify an empty key.") | |
| os.Exit(1) | |
| } | |
| contents, err := ioutil.ReadFile(filePath) | |
| contentsString := string(contents) | |
| newContents := strings.Replace(contentsString, searchText, replaceText, -1) | |
| newContentsByte := []byte(newContents) | |
| err = ioutil.WriteFile(filePath, newContentsByte, 0644) | |
| check(err) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment