Skip to content

Commit

Permalink
Merge pull request #2429 from dgageot/fix-1919
Browse files Browse the repository at this point in the history
Fix script that creates a new version to make it work on osx
  • Loading branch information
balopat authored Jul 8, 2019
2 parents 299c213 + 6335d5c commit 486e4b0
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 41 deletions.
117 changes: 114 additions & 3 deletions hack/new_config_version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,123 @@ limitations under the License.
package main

import (
"fmt"
"bufio"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema"
)

// Before: prev -> current (latest)
// After: prev -> current -> new (latest)
func main() {
fmt.Println(strings.TrimPrefix(latest.Version, "skaffold/"))
new := os.Args[1]
current := strings.TrimPrefix(schema.SchemaVersions[len(schema.SchemaVersions)-1].APIVersion, "skaffold/")
prev := strings.TrimPrefix(schema.SchemaVersions[len(schema.SchemaVersions)-2].APIVersion, "skaffold/")

// Create a package for current version
walk(path("latest"), func(file string, info os.FileInfo) {
if !info.IsDir() {
cp(file, path(current, info.Name()))
sed(path(current, info.Name()), "package latest", "package "+current)
}
})

// Create code to upgrade from current to new
cp(path(prev, "upgrade.go"), path(current, "upgrade.go"))
sed(path(current, "upgrade.go"), current, new)
sed(path(current, "upgrade.go"), prev, current)

// Create a test for the upgrade from current to new
cp(path(prev, "upgrade_test.go"), path(current, "upgrade_test.go"))
sed(path(current, "upgrade_test.go"), current, new)
sed(path(current, "upgrade_test.go"), prev, current)

// Previous version now upgrades to current instead of latest
sed(path(prev, "upgrade.go"), "latest", current)
sed(path(prev, "upgrade_test.go"), "latest", current)

// Latest uses the new version
sed(path("latest", "config.go"), current, new)

// Update skaffold.yaml in integration tests
walk("integration", func(path string, info os.FileInfo) {
if info.Name() == "skaffold.yaml" {
sed(path, current, new)
}
})

// Add the new version to the list of versions
lines := lines(path("versions.go"))
var content string
for _, line := range lines {
content += line + "\n"
if strings.Contains(line, prev) {
content += strings.ReplaceAll(line, prev, current) + "\n"
}
}
write(path("versions.go"), []byte(content))

// Update the docs with the new version
sed("docs/config.toml", current, new)
}

func path(elem ...string) string {
return filepath.Join(append([]string{"pkg", "skaffold", "schema"}, elem...)...)
}

func read(path string) []byte {
buf, err := ioutil.ReadFile(path)
if err != nil {
panic("unable to read " + path)
}
return buf
}

func write(path string, buf []byte) {
if err := ioutil.WriteFile(path, buf, os.ModePerm); err != nil {
panic("unable to write " + path)
}
}

func sed(path string, old, new string) {
buf := read(path)
replaced := regexp.MustCompile(old).ReplaceAll(buf, []byte(new))
write(path, replaced)
}

func cp(path string, dest string) {
buf := read(path)
os.Mkdir(filepath.Dir(dest), os.ModePerm)
write(dest, buf)
}

func lines(path string) []string {
file, err := os.Open(path)
if err != nil {
panic("unable to open " + path)
}
defer file.Close()

var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines
}

func walk(root string, fn func(path string, info os.FileInfo)) {
if err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
fn(path, info)
return nil
}); err != nil {
panic("unable to list files")
}
}
37 changes: 4 additions & 33 deletions hack/new_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.

CURRENT_VERSION=`go run hack/new_config_version/version.go`
echo "Current config version: $CURRENT_VERSION"

echo "Please enter new config version:"
read NEW_VERSION

echo "Please enter previous config version:"
read PREV_VERSION

echo "Generating changes for new config version $NEW_VERSION..."

sed -i docs/config.toml -e "s;$CURRENT_VERSION;$NEW_VERSION;g"

cp -R pkg/skaffold/schema/latest pkg/skaffold/schema/${CURRENT_VERSION}

sed -i pkg/skaffold/schema/${CURRENT_VERSION}/*.go -e "s;package latest;package $CURRENT_VERSION;g"

sed pkg/skaffold/schema/${PREV_VERSION}/upgrade_test.go -e "s;$CURRENT_VERSION;$NEW_VERSION;g" > pkg/skaffold/schema/${CURRENT_VERSION}/upgrade_test.go
sed -i pkg/skaffold/schema/${CURRENT_VERSION}/upgrade_test.go -e "s;$PREV_VERSION;$CURRENT_VERSION;g"

sed pkg/skaffold/schema/${PREV_VERSION}/upgrade.go -e "s;$CURRENT_VERSION;$NEW_VERSION;g" > pkg/skaffold/schema/${CURRENT_VERSION}/upgrade.go
sed -i pkg/skaffold/schema/${CURRENT_VERSION}/upgrade.go -e "s;$PREV_VERSION;$CURRENT_VERSION;g"

sed -i pkg/skaffold/schema/${PREV_VERSION}/upgrade*.go -e "s;latest;$CURRENT_VERSION;g"
goimports -w pkg/skaffold/schema/${PREV_VERSION}/upgrade*.go

sed -i pkg/skaffold/schema/latest/config.go -e "s;$CURRENT_VERSION;$NEW_VERSION;g"

find integration -name "skaffold.yaml" -print0 | xargs -0 -I xx sed -i xx -e "s;$CURRENT_VERSION;$NEW_VERSION;g"

sed pkg/skaffold/schema/versions.go -i -e "s;\(.*\)$PREV_VERSION.Version\(.*\)$PREV_VERSION\(.*\);&\n\1$CURRENT_VERSION.Version\2$CURRENT_VERSION\3;g"
sed pkg/skaffold/schema/versions.go -i -e "s;\(.*\)/$PREV_VERSION\(.*\);&\n\1/$CURRENT_VERSION\2;g"
go run ./hack/new_config_version/version.go ${NEW_VERSION}

goimports -w ./pkg/skaffold/schema
make generate-schemas

git --no-pager diff --minimal

make test

echo
echo "---------------------------------------"
echo
echo "Files generated for $NEW_VERSION. Don't worry about the hack/check-docs change failure, it is expected!"
echo "Other tests should have passed. For the docs change, commit the results and rerun 'make test'."
echo "Files generated for $NEW_VERSION."
echo "All tests should have passed. For the docs change, commit the results and rerun 'make test'."
echo "Please double check manually the generated files as well: the upgrade functionality, and all the examples:"
echo
git status -s
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/schema/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"strings"

yamlpatch "github.com/krishicks/yaml-patch"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
)

type VersionedConfig interface {
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/schema/v1beta10/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

next "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1beta11"
"github.com/GoogleContainerTools/skaffold/testutil"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
)

func TestUpgrade(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/schema/v1beta11/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

next "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/testutil"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
)

func TestUpgrade(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/schema/v1beta9/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

next "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1beta10"
"github.com/GoogleContainerTools/skaffold/testutil"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
)

func TestUpgrade(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/schema/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
misc "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
yaml "gopkg.in/yaml.v2"
)

type APIVersion struct {
Expand Down

0 comments on commit 486e4b0

Please sign in to comment.