From cd3706d7c5e03af13e8fb396b483d3522f8ac91c Mon Sep 17 00:00:00 2001 From: Amine Hilaly Date: Fri, 25 Jun 2021 18:58:45 +0200 Subject: [PATCH] Copy `generator.yaml` to apis/version directory Adding some logic to create copies of the `generator.yaml` within the apis versions directories. These generator.yaml files we be reused by the conversion webhook generator to understand the difference between each two different API versions. This patch Also adds a new utility function to copy files from a source to a destination path. --- cmd/ack-generate/command/apis.go | 15 ++++++++++++++- pkg/util/file.go | 32 +++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/cmd/ack-generate/command/apis.go b/cmd/ack-generate/command/apis.go index 435a7955..0c73f55b 100644 --- a/cmd/ack-generate/command/apis.go +++ b/cmd/ack-generate/command/apis.go @@ -24,6 +24,7 @@ import ( generate "github.com/aws-controllers-k8s/code-generator/pkg/generate" ackgenerate "github.com/aws-controllers-k8s/code-generator/pkg/generate/ack" "github.com/aws-controllers-k8s/code-generator/pkg/model" + "github.com/aws-controllers-k8s/code-generator/pkg/util" ) type contentType int @@ -65,7 +66,19 @@ func saveGeneratedMetadata(cmd *cobra.Command, args []string) error { optAWSSDKGoVersion, optGeneratorConfigPath, ) - return err + if err != nil { + return fmt.Errorf("cannot create generation metadata file: %v", err) + } + + copyDest := filepath.Join( + optOutputPath, "apis", optGenVersion, "generator.yaml", + ) + err = util.CopyFile(optGeneratorConfigPath, copyDest) + if err != nil { + return fmt.Errorf("cannot copy generator configuration file: %v", err) + } + + return nil } // generateAPIs generates the Go files for each resource in the AWS service diff --git a/pkg/util/file.go b/pkg/util/file.go index aaa4ae41..f3567cf8 100644 --- a/pkg/util/file.go +++ b/pkg/util/file.go @@ -13,10 +13,40 @@ package util -import "os" +import ( + "io" + "os" +) // FileExists returns True if the supplied file path exists, false otherwise func FileExists(path string) bool { _, err := os.Stat(path) return !os.IsNotExist(err) } + +// CopyFile copies a file from a source path to a destination path. +func CopyFile(src, dest string) error { + srcFile, err := os.Open(src) + if err != nil { + return err + } + defer srcFile.Close() + + destFile, err := os.Create(dest) + if err != nil { + return err + } + defer destFile.Close() + + _, err = io.Copy(destFile, srcFile) + if err != nil { + return err + } + + err = destFile.Sync() + if err != nil { + return err + } + + return nil +}