"mkdir": Mkdir(),
"mv": Mv(),
"rm": Rm(),
+ "replace": Replace(),
"sleep": Sleep(),
"stderr": Stderr(),
"stdout": Stdout(),
})
}
+// Replace replaces all occurrences of a string in a file with another string.
+func Replace() Cmd {
+ return Command(
+ CmdUsage{
+ Summary: "replace strings in a file",
+ Args: "[old new]... file",
+ Detail: []string{
+ "The 'old' and 'new' arguments are unquoted as if in quoted Go strings.",
+ },
+ },
+ func(s *State, args ...string) (WaitFunc, error) {
+ if len(args)%2 != 1 {
+ return nil, ErrUsage
+ }
+
+ oldNew := make([]string, 0, len(args)-1)
+ for _, arg := range args[:len(args)-1] {
+ s, err := strconv.Unquote(`"` + arg + `"`)
+ if err != nil {
+ return nil, err
+ }
+ oldNew = append(oldNew, s)
+ }
+
+ r := strings.NewReplacer(oldNew...)
+ file := s.Path(args[len(args)-1])
+
+ data, err := os.ReadFile(file)
+ if err != nil {
+ return nil, err
+ }
+ replaced := r.Replace(string(data))
+
+ return nil, os.WriteFile(file, []byte(replaced), 0666)
+ })
+}
+
// Rm removes a file or directory.
//
// If a directory, Rm also recursively removes that directory's
package main_test
import (
- "bytes"
"cmd/go/internal/script"
"cmd/go/internal/script/scripttest"
"cmd/go/internal/work"
cmds[name] = cmd
}
- add("addcrlf", scriptAddCRLF())
add("cc", scriptCC(cmdExec))
cmdGo := scriptGo(interrupt, gracePeriod)
add("go", cmdGo)
return cmds
}
-// scriptAddCRLF adds CRLF line endings to the named files.
-func scriptAddCRLF() script.Cmd {
- return script.Command(
- script.CmdUsage{
- Summary: "convert line endings to CRLF",
- Args: "file...",
- },
- func(s *script.State, args ...string) (script.WaitFunc, error) {
- if len(args) == 0 {
- return nil, script.ErrUsage
- }
-
- for _, file := range args {
- file = s.Path(file)
- data, err := os.ReadFile(file)
- if err != nil {
- return nil, err
- }
- err = os.WriteFile(file, bytes.ReplaceAll(data, []byte("\n"), []byte("\r\n")), 0666)
- if err != nil {
- return nil, err
- }
- }
-
- return nil, nil
- })
-}
-
// scriptCC runs the C compiler along with platform specific options.
func scriptCC(cmdExec script.Cmd) script.Cmd {
return script.Command(
! stdout '"(Dir|Info|GoMod|Zip)"'
# reuse attempt with stale hash should reinvoke git, not report reuse
+cp tagtestsv022.json tagtestsv022badhash.json
+replace '57952' '56952XXX' tagtestsv022badhash.json
go mod download -reuse=tagtestsv022badhash.json -x -json vcs-test.golang.org/git/tagtests.git@v0.2.2
stderr 'git fetch'
! stdout '"Reuse": true'
stdout '"Zip"'
# reuse with stale repo URL
+cp tagtestsv022.json tagtestsv022badurl.json
+replace 'git/tagtests\"' 'git/tagtestsXXX\"' tagtestsv022badurl.json
go mod download -reuse=tagtestsv022badurl.json -x -json vcs-test.golang.org/git/tagtests.git@v0.2.2
! stdout '"Reuse": true'
stdout '"URL": "https://vcs-test.golang.org/git/tagtests"'
stdout '"Zip"'
# reuse with stale VCS
+cp tagtestsv022.json tagtestsv022badvcs.json
+replace '\"git\"' '\"gitXXX\"' tagtestsv022badvcs.json
go mod download -reuse=tagtestsv022badvcs.json -x -json vcs-test.golang.org/git/tagtests.git@v0.2.2
! stdout '"Reuse": true'
stdout '"URL": "https://vcs-test.golang.org/git/tagtests"'
# reuse with stale Dir
+cp tagtestsv022.json tagtestsv022baddir.json
+replace '\t\t\"Ref\":' '\t\t\"Subdir\": \"subdir\",\n\t\t\"Ref\":' tagtestsv022baddir.json
go mod download -reuse=tagtestsv022baddir.json -x -json vcs-test.golang.org/git/tagtests.git@v0.2.2
! stdout '"Reuse": true'
stdout '"URL": "https://vcs-test.golang.org/git/tagtests"'
# reuse with stale TagSum
+cp tagtests.json tagtestsbadtagsum.json
+replace 'sMEOGo=' 'sMEoGo=XXX' tagtestsbadtagsum.json
go mod download -reuse=tagtestsbadtagsum.json -x -json vcs-test.golang.org/git/tagtests.git@latest
! stdout '"Reuse": true'
stdout '"TagSum": "t1:Dp7yRKDuE8WjG0429PN9hYWjqhy2te7P9Oki/sMEOGo="'
-
--- tagtestsv022badhash.json --
-{
- "Path": "vcs-test.golang.org/git/tagtests.git",
- "Version": "v0.2.2",
- "Origin": {
- "VCS": "git",
- "URL": "https://vcs-test.golang.org/git/tagtests",
- "Ref": "refs/tags/v0.2.2",
- "Hash": "59356c8cd18c5fe9a598167d98a6843e52d57952XXX"
- }
-}
-
--- tagtestsbadtagsum.json --
-{
- "Path": "vcs-test.golang.org/git/tagtests.git",
- "Version": "v0.2.2",
- "Query": "latest",
- "Origin": {
- "VCS": "git",
- "URL": "https://vcs-test.golang.org/git/tagtests",
- "TagSum": "t1:Dp7yRKDuE8WjG0429PN9hYWjqhy2te7P9Oki/sMEOGo=XXX",
- "Ref": "refs/tags/v0.2.2",
- "Hash": "59356c8cd18c5fe9a598167d98a6843e52d57952"
- },
- "Reuse": true
-}
-
--- tagtestsv022badvcs.json --
-{
- "Path": "vcs-test.golang.org/git/tagtests.git",
- "Version": "v0.2.2",
- "Origin": {
- "VCS": "gitXXX",
- "URL": "https://vcs-test.golang.org/git/tagtests",
- "Ref": "refs/tags/v0.2.2",
- "Hash": "59356c8cd18c5fe9a598167d98a6843e52d57952"
- }
-}
-
--- tagtestsv022baddir.json --
-{
- "Path": "vcs-test.golang.org/git/tagtests.git",
- "Version": "v0.2.2",
- "Origin": {
- "VCS": "git",
- "URL": "https://vcs-test.golang.org/git/tagtests",
- "Subdir": "subdir",
- "Ref": "refs/tags/v0.2.2",
- "Hash": "59356c8cd18c5fe9a598167d98a6843e52d57952"
- }
-}
-
--- tagtestsv022badurl.json --
-{
- "Path": "vcs-test.golang.org/git/tagtests.git",
- "Version": "v0.2.2",
- "Origin": {
- "VCS": "git",
- "URL": "https://vcs-test.golang.org/git/tagtestsXXX",
- "Ref": "refs/tags/v0.2.2",
- "Hash": "59356c8cd18c5fe9a598167d98a6843e52d57952"
- }
-}