-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Copy static files (assets) (#29)
- Loading branch information
1 parent
65727a4
commit ab72553
Showing
5 changed files
with
143 additions
and
2 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
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" |
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,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 | ||
} |
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
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