Skip to content

Commit

Permalink
feat: Copy static files (assets) (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech authored Mar 8, 2024
1 parent 65727a4 commit ab72553
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 2 deletions.
21 changes: 20 additions & 1 deletion cmd/mktp/config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
output:
go_sdk: "../generated/pango"
terraform_provider: "../generated/terraform-provider-panos"
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"
91 changes: 91 additions & 0 deletions pkg/generate/assets.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions pkg/mktp/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 13 additions & 1 deletion pkg/properties/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,26 @@ 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 {
GoSdk string `json:"go_sdk" yaml:"go_sdk"`
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)
Expand Down
14 changes: 14 additions & 0 deletions pkg/properties/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

0 comments on commit ab72553

Please sign in to comment.