Skip to content

Commit

Permalink
Make auto-generated files read-only (#2323)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmlewis committed Apr 3, 2022
1 parent c28ccfd commit e5684e7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
14 changes: 13 additions & 1 deletion github/gen-accessors.go
Expand Up @@ -186,10 +186,18 @@ func (t *templateData) dump() error {
}

logf("Writing %v...", filename)
if err := ioutil.WriteFile(filename, clean, 0644); err != nil {
if err := os.Chmod(filename, 0644); err != nil {
return fmt.Errorf("os.Chmod(%q, 0644): %v", filename, err)
}

if err := ioutil.WriteFile(filename, clean, 0444); err != nil {
return err
}

if err := os.Chmod(filename, 0444); err != nil {
return fmt.Errorf("os.Chmod(%q, 0444): %v", filename, err)
}

return nil
}

Expand Down Expand Up @@ -329,6 +337,8 @@ const source = `// Copyright {{.Year}} The go-github AUTHORS. All rights reserve
// license that can be found in the LICENSE file.
// Code generated by gen-accessors; DO NOT EDIT.
// Instead, please run "go generate ./..." as described here:
// https://github.com/google/go-github/blob/master/CONTRIBUTING.md#submitting-a-patch
package {{.Package}}
{{with .Imports}}
Expand Down Expand Up @@ -373,6 +383,8 @@ const test = `// Copyright {{.Year}} The go-github AUTHORS. All rights reserved.
// license that can be found in the LICENSE file.
// Code generated by gen-accessors; DO NOT EDIT.
// Instead, please run "go generate ./..." as described here:
// https://github.com/google/go-github/blob/master/CONTRIBUTING.md#submitting-a-patch
package {{.Package}}
{{with .Imports}}
Expand Down
16 changes: 15 additions & 1 deletion github/gen-stringify-test.go
Expand Up @@ -352,7 +352,19 @@ func (t *templateData) dump() error {
}

logf("Writing %v...", t.filename)
return ioutil.WriteFile(t.filename, clean, 0644)
if err := os.Chmod(t.filename, 0644); err != nil {
return fmt.Errorf("os.Chmod(%q, 0644): %v", t.filename, err)
}

if err := ioutil.WriteFile(t.filename, clean, 0444); err != nil {
return err
}

if err := os.Chmod(t.filename, 0444); err != nil {
return fmt.Errorf("os.Chmod(%q, 0444): %v", t.filename, err)
}

return nil
}

func newStructField(receiverType, fieldName, fieldType, zeroValue string, namedStruct bool) *structField {
Expand All @@ -379,6 +391,8 @@ const source = `// Copyright {{.Year}} The go-github AUTHORS. All rights reserve
// license that can be found in the LICENSE file.
// Code generated by gen-stringify-tests; DO NOT EDIT.
// Instead, please run "go generate ./..." as described here:
// https://github.com/google/go-github/blob/master/CONTRIBUTING.md#submitting-a-patch
package {{ $package := .Package}}{{$package}}
{{with .Imports}}
Expand Down
2 changes: 2 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions github/github-stringify_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e5684e7

Please sign in to comment.