MCPcopy Index your code
hub / github.com/riverqueue/river / parseAndUpdateGoModFile

Function parseAndUpdateGoModFile

rivershared/cmd/update-mod-go/main.go:100–155  ·  view source on GitHub ↗
(checkOnly bool, modFilename, workFilename, workGoVersion, workToolchainName string)

Source from the content-addressed store, hash-verified

98}
99
100func parseAndUpdateGoModFile(checkOnly bool, modFilename, workFilename, workGoVersion, workToolchainName string) (bool, error) {
101 modFileData, err := os.ReadFile(modFilename)
102 if err != nil {
103 return false, fmt.Errorf("error reading file %q: %w", modFilename, err)
104 }
105
106 modFile, err := modfile.Parse(modFilename, modFileData, nil)
107 if err != nil {
108 return false, fmt.Errorf("error parsing file %q: %w", modFilename, err)
109 }
110
111 var anyMismatch bool
112
113 fmt.Printf("%s\n", modFilename)
114
115 if workGoVersion != modFile.Go.Version {
116 if checkOnly {
117 return false, fmt.Errorf("go directive of %q (%s) doesn't match %q (%s)", modFilename, modFile.Go.Version, workFilename, workGoVersion)
118 }
119
120 anyMismatch = true
121 if err := modFile.AddGoStmt(workGoVersion); err != nil {
122 return false, fmt.Errorf("error adding go statement: %w", err)
123 }
124 fmt.Printf(" set go to %s for %s\n", workGoVersion, modFilename)
125 }
126
127 if workToolchainName != modFile.Toolchain.Name {
128 if checkOnly {
129 return false, fmt.Errorf("toolchain directive of %q (%s) doesn't match %q (%s)", modFilename, modFile.Toolchain.Name, workFilename, workToolchainName)
130 }
131
132 anyMismatch = true
133 if err := modFile.AddToolchainStmt(workToolchainName); err != nil {
134 return false, fmt.Errorf("error adding toolchain statement: %w", err)
135 }
136 fmt.Printf(" set toolchain to %s for %s\n", workToolchainName, modFilename)
137 }
138
139 if !checkOnly {
140 if anyMismatch {
141 updatedFileData, err := modFile.Format()
142 if err != nil {
143 return false, fmt.Errorf("error formatting file %q after update: %w", modFilename, err)
144 }
145
146 if err := os.WriteFile(modFilename, updatedFileData, 0o600); err != nil { //nolint:gosec // validated by localPath
147 return false, fmt.Errorf("error writing file %q after update: %w", modFilename, err)
148 }
149 } else {
150 fmt.Printf(" no changes\n")
151 }
152 }
153
154 return anyMismatch, nil
155}

Callers 2

runFunction · 0.70

Calls 1

ErrorfMethod · 0.65

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…