From ab725538b656a9fd50a81230504f25e2f20948e6 Mon Sep 17 00:00:00 2001 From: Sebastian Czech Date: Fri, 8 Mar 2024 10:59:10 +0100 Subject: [PATCH] feat: Copy static files (assets) (#29) --- cmd/mktp/config.yaml | 21 +++++++- pkg/generate/assets.go | 91 +++++++++++++++++++++++++++++++++++ pkg/mktp/cmd.go | 5 ++ pkg/properties/config.go | 14 +++++- pkg/properties/config_test.go | 14 ++++++ 5 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 pkg/generate/assets.go diff --git a/cmd/mktp/config.yaml b/cmd/mktp/config.yaml index 0332d3a8..ff60b01c 100644 --- a/cmd/mktp/config.yaml +++ b/cmd/mktp/config.yaml @@ -1,3 +1,22 @@ output: go_sdk: "../generated/pango" - terraform_provider: "../generated/terraform-provider-panos" \ No newline at end of file + terraform_provider: "../generated/terraform-provider-panos" +assets: +# util_package: +# source: "assets/util" +# target: +# go_sdk: true +# terraform_provider: false +# destination: "util" +# errors_package: +# source: "assets/errors" +# target: +# go_sdk: true +# terraform_provider: false +# destination: "errors" +# version_package: +# source: "assets/version" +# target: +# go_sdk: true +# terraform_provider: false +# destination: "version" diff --git a/pkg/generate/assets.go b/pkg/generate/assets.go new file mode 100644 index 00000000..2b0c9904 --- /dev/null +++ b/pkg/generate/assets.go @@ -0,0 +1,91 @@ +package generate + +import ( + "bytes" + "fmt" + "github.com/paloaltonetworks/pan-os-codegen/pkg/properties" + "io" + "io/fs" + "os" + "path/filepath" +) + +func CopyAssets(config *properties.Config) error { + for _, asset := range config.Assets { + files, err := listAssets(asset) + if err != nil { + return err + } + + if asset.Target.GoSdk { + if err = copyAsset(config.Output.GoSdk, asset, files); err != nil { + return err + } + } + if asset.Target.TerraformProvider { + if err = copyAsset(config.Output.TerraformProvider, asset, files); err != nil { + return err + } + } + } + + return nil +} + +func listAssets(asset *properties.Asset) ([]string, error) { + var files []string + + // Walk through directory and get list of all files + err := filepath.WalkDir(asset.Source, func(path string, entry fs.DirEntry, err error) error { + if err != nil { + return err + } + if !entry.IsDir() { + files = append(files, path) + } + return nil + }) + if err != nil { + return nil, err + } + + return files, nil +} + +func copyAsset(target string, asset *properties.Asset, files []string) error { + // Prepare destination path + destinationDir := target + "/" + asset.Destination + + // Create the destination directory if it doesn't exist + if err := os.MkdirAll(destinationDir, os.ModePerm); err != nil { + return err + } + + for _, sourceFilePath := range files { + // Prepare destination path + destinationFilePath := filepath.Join(destinationDir, filepath.Base(sourceFilePath)) + fmt.Printf("Copy file from %s to %s\n", sourceFilePath, destinationFilePath) + + // Read the contents of the source file + data, err := os.ReadFile(sourceFilePath) + if err != nil { + return err + } + + // Create the destination file + destinationFile, err := os.Create(destinationFilePath) + if err != nil { + return err + } + defer func(destinationFile *os.File) { + _ = destinationFile.Close() + }(destinationFile) + + // Write the contents into the destination file + _, err = io.Copy(destinationFile, bytes.NewReader(data)) + if err != nil { + return err + } + } + return nil +} diff --git a/pkg/mktp/cmd.go b/pkg/mktp/cmd.go index c87012bc..66d3960e 100644 --- a/pkg/mktp/cmd.go +++ b/pkg/mktp/cmd.go @@ -109,6 +109,11 @@ func (c *Cmd) Execute() error { // Output as Terraform code. } + // Copy assets (static files) + if err = generate.CopyAssets(config); err != nil { + return fmt.Errorf("error copying assets %s", err) + } + // Finalize pango code: // * make fmt diff --git a/pkg/properties/config.go b/pkg/properties/config.go index 7846d1cf..1e5e3648 100644 --- a/pkg/properties/config.go +++ b/pkg/properties/config.go @@ -3,7 +3,8 @@ package properties import "github.com/paloaltonetworks/pan-os-codegen/pkg/content" type Config struct { - Output OutputPaths `json:"output" yaml:"output"` + Output OutputPaths `json:"output" yaml:"output"` + Assets map[string]*Asset `json:"assets" yaml:"assets"` } type OutputPaths struct { @@ -11,6 +12,17 @@ type OutputPaths struct { TerraformProvider string `json:"terraform_provider" yaml:"terraform_provider"` } +type Asset struct { + Source string `json:"source" yaml:"source"` + Target *Target `json:"target" yaml:"target"` + Destination string `json:"destination" yaml:"destination"` +} + +type Target struct { + GoSdk bool `json:"go_sdk" yaml:"go_sdk"` + TerraformProvider bool `json:"terraform_provider" yaml:"terraform_provider"` +} + func ParseConfig(input []byte) (*Config, error) { var ans Config err := content.Unmarshal(input, &ans) diff --git a/pkg/properties/config_test.go b/pkg/properties/config_test.go index 51f7a1fd..f08e45fe 100644 --- a/pkg/properties/config_test.go +++ b/pkg/properties/config_test.go @@ -10,6 +10,13 @@ func TestConfig(t *testing.T) { const content = `output: go_sdk: "../generated/pango" terraform_provider: "../generated/terraform-provider-panos" +assets: + util_package: + source: "assets/util" + target: + go_sdk: true + terraform_provider: false + destination: "util" ` // when @@ -20,4 +27,11 @@ func TestConfig(t *testing.T) { assert.NotEmptyf(t, config.Output, "Config output cannot be empty") assert.NotEmptyf(t, config.Output.GoSdk, "Config Go SDK path cannot be empty") assert.NotEmptyf(t, config.Output.TerraformProvider, "Config Terraform provider path cannot be empty") + assert.NotEmpty(t, config.Assets) + assert.Equal(t, 1, len(config.Assets)) + assert.Equal(t, 1, len(config.Assets)) + assert.Equal(t, "assets/util", config.Assets["util_package"].Source) + assert.True(t, config.Assets["util_package"].Target.GoSdk) + assert.False(t, config.Assets["util_package"].Target.TerraformProvider) + assert.Equal(t, "util", config.Assets["util_package"].Destination) }