-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding release comment management to all config.go
- Loading branch information
Showing
6 changed files
with
335 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
Copyright 2019 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"path" | ||
|
||
"github.com/GoogleContainerTools/skaffold/hack/versions/pkg/schema" | ||
) | ||
|
||
func main() { | ||
latest := path.Join("pkg", "skaffold", "schema", "latest", "config.go") | ||
schema.UpdateVersionComment(latest, true) | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
Copyright 2019 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/GoogleContainerTools/skaffold/hack/versions/pkg/version" | ||
|
||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/GoogleContainerTools/skaffold/hack/versions/pkg/schema" | ||
) | ||
|
||
func main() { | ||
schemaDir := path.Join("pkg", "skaffold", "schema") | ||
_, isReleased := version.GetLatestVersion() | ||
if err := filepath.Walk(schemaDir, func(path string, info os.FileInfo, err error) error { | ||
if info.Name() == "config.go" { | ||
released := !strings.Contains(path, "latest") || isReleased | ||
return schema.UpdateVersionComment(path, released) | ||
} | ||
return nil | ||
}); err != nil { | ||
logrus.Fatalf("%s", err) | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
Copyright 2019 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package schema | ||
|
||
import ( | ||
"bytes" | ||
"go/ast" | ||
"go/parser" | ||
"go/printer" | ||
"go/token" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
const releasedComment = `// !!! WARNING !!! This config version is already released, please DO NOT MODIFY the structs in this file.` | ||
const unreleasedComment = `// This config version is not yet released, it is SAFE TO MODIFY the structs in this file.` | ||
|
||
// historicalVersionComments is used to recognize whether an existing comment is a "release comment" or not. | ||
// If you want to change a (say releasedComment) comment historically on all files, then: | ||
// 1.) add the current version to `historicalVersionComments` | ||
// 2.) change the text of releasedComment (or unreleasedComment) | ||
// 3.) run `go run hack/versions/cmd/update_comments/main.go` | ||
// 4.) remove the updated version from `historicalVersionComments` | ||
var historicalVersionComments = []string{ | ||
releasedComment, | ||
unreleasedComment, | ||
} | ||
|
||
func UpdateVersionComment(origFile string, released bool) error { | ||
info, err := os.Stat(origFile) | ||
if err != nil { | ||
return err | ||
} | ||
content, err := updateVersionComment(origFile, released) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := ioutil.WriteFile(origFile, content, info.Mode()); err != nil { | ||
return err | ||
} | ||
|
||
cmd := exec.Command("go", "fmt", origFile) | ||
return cmd.Run() | ||
} | ||
|
||
func updateVersionComment(origFile string, released bool) ([]byte, error) { | ||
fset := token.NewFileSet() | ||
|
||
var commentString string | ||
if released { | ||
commentString = releasedComment | ||
} else { | ||
commentString = unreleasedComment | ||
} | ||
|
||
astA, err := parser.ParseFile(fset, origFile, nil, parser.ParseComments) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
firstComment := astA.Comments[1].List[0] | ||
|
||
if firstComment.Text == commentString { | ||
return printAst(fset, astA) | ||
} | ||
|
||
if isVersionComment(firstComment) { | ||
firstComment.Text = commentString | ||
return printAst(fset, astA) | ||
} | ||
|
||
addFirstCommentOnVersion(astA, commentString) | ||
|
||
return printAst(fset, astA) | ||
} | ||
|
||
func isVersionComment(firstComment *ast.Comment) bool { | ||
for _, comment := range historicalVersionComments { | ||
if comment == firstComment.Text { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func printAst(fset *token.FileSet, ast *ast.File) ([]byte, error) { | ||
var buf bytes.Buffer | ||
|
||
if err := printer.Fprint(&buf, fset, ast); err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
} | ||
|
||
func addFirstCommentOnVersion(astA *ast.File, commentString string) { | ||
ast.Inspect(astA, func(node ast.Node) bool { | ||
|
||
if decl, ok := node.(*ast.GenDecl); ok && | ||
decl.Tok.String() == "const" && | ||
len(decl.Specs) == 1 { | ||
sp := decl.Specs[0] | ||
if t, ok := sp.(*ast.ValueSpec); ok { | ||
if len(t.Names) == 1 && t.Names[0].Name == "Version" { | ||
comment := ast.Comment{ | ||
Slash: decl.TokPos - 1, | ||
Text: commentString + "\n", | ||
} | ||
comments := []*ast.Comment{ | ||
&comment, | ||
} | ||
|
||
cg := &ast.CommentGroup{List: comments} | ||
astA.Comments = append([]*ast.CommentGroup{astA.Comments[0], cg}, astA.Comments[1:]...) | ||
decl.Doc = cg | ||
return false | ||
} | ||
} | ||
} | ||
return true | ||
}) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
Copyright 2019 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package schema | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/GoogleContainerTools/skaffold/testutil" | ||
) | ||
|
||
const configFileTemplate = `/* | ||
Copyright 2019 The Skaffold Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package v1beta12 | ||
import ( | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/util" | ||
) | ||
%sconst Version string = "skaffold/v1beta12" | ||
// NewSkaffoldConfig creates a SkaffoldConfig | ||
func NewSkaffoldConfig() util.VersionedConfig { | ||
return new(SkaffoldConfig) | ||
} | ||
` | ||
|
||
var configWithNoComment = fmt.Sprintf(configFileTemplate, "") | ||
var configWithReleasedComment = fmt.Sprintf(configFileTemplate, releasedComment+"\n") | ||
var configWithUnreleasedComment = fmt.Sprintf(configFileTemplate, unreleasedComment+"\n") | ||
|
||
func TestAddComment(t *testing.T) { | ||
|
||
tcs := []struct { | ||
name string | ||
orig string | ||
expected string | ||
released bool | ||
}{ | ||
{ | ||
name: "unreleased comment added on file", | ||
released: true, | ||
orig: configWithNoComment, | ||
expected: configWithReleasedComment, | ||
}, | ||
{ | ||
name: "released comment added on file", | ||
released: false, | ||
orig: configWithNoComment, | ||
expected: configWithUnreleasedComment, | ||
}, | ||
{ | ||
name: "released -> released", | ||
released: true, | ||
orig: configWithReleasedComment, | ||
expected: configWithReleasedComment, | ||
}, | ||
{ | ||
name: "unreleased -> unreleased", | ||
released: false, | ||
orig: configWithUnreleasedComment, | ||
expected: configWithUnreleasedComment, | ||
}, | ||
{ | ||
name: "released -> unreleased", | ||
released: false, | ||
orig: configWithReleasedComment, | ||
expected: configWithUnreleasedComment, | ||
}, | ||
{ | ||
name: "unreleased -> released", | ||
released: true, | ||
orig: configWithUnreleasedComment, | ||
expected: configWithReleasedComment, | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
testutil.Run(t, tc.name, func(t *testutil.T) { | ||
|
||
dir := t.NewTempDir() | ||
aFile := dir.Path("a.go") | ||
t.CheckNoError(ioutil.WriteFile(aFile, []byte(tc.orig), 0666)) | ||
modified, err := updateVersionComment(aFile, tc.released) | ||
t.CheckErrorAndDeepEqual(false, err, tc.expected, string(modified)) | ||
}) | ||
} | ||
|
||
} |