Skip to content

Commit

Permalink
feat: Parse normalised specifications (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech authored Feb 29, 2024
1 parent f19e57b commit 813a4d9
Show file tree
Hide file tree
Showing 23 changed files with 1,022 additions and 481 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ We are maintaining a [public roadmap](https://github.com/orgs/PaloAltoNetworks/p
## Getting Help

Open an [issue](https://github.com/PaloAltoNetworks/pan-os-codegen/issues) on Github.

## Usage

```bash
go run cmd/mktp/main.go cmd/mktp/config.yaml
```
3 changes: 3 additions & 0 deletions cmd/mktp/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output:
go_sdk: "../generated/pango"
terraform_provider: "../generated/terraform-provider-panos"
30 changes: 15 additions & 15 deletions cmd/mktp/main.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package main

import (
"context"
"fmt"
"os"
"context"
"fmt"
"os"

"github.com/paloaltonetworks/pan-os-codegen/pkg/mktp"
"github.com/paloaltonetworks/pan-os-codegen/pkg/mktp"
)

func main() {
var err error
ctx := context.Background()
var err error
ctx := context.Background()

cmd := mktp.Command(ctx, os.Args[1:]...)
err = cmd.Setup()
if err == nil {
err = cmd.Execute()
}
cmd := mktp.Command(ctx, os.Args[1:]...)
err = cmd.Setup()
if err == nil {
err = cmd.Execute()
}

if err != nil {
fmt.Fprintf(os.Stderr, err.Error() + "\n")
os.Exit(1)
}
if err != nil {
fmt.Fprintf(os.Stderr, err.Error()+"\n")
os.Exit(1)
}
}
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
module github.com/paloaltonetworks/pan-os-codegen

require gopkg.in/yaml.v3 v3.0.1
require (
github.com/stretchr/testify v1.8.4
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
)

go 1.21.4
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
183 changes: 91 additions & 92 deletions pkg/imports/manager.go
Original file line number Diff line number Diff line change
@@ -1,81 +1,80 @@
package imports

import (
"fmt"
"sort"
"strings"
"text/template"
"fmt"
"sort"
"strings"
"text/template"
)


func NewManager() *Manager {
return &Manager{
Standard: make(map[string] string),
Sdk: make(map[string] string),
Hashicorp: make(map[string] string),
Other: make(map[string] string),
}
return &Manager{
Standard: make(map[string]string),
Sdk: make(map[string]string),
Hashicorp: make(map[string]string),
Other: make(map[string]string),
}
}

type Manager struct {
Standard map[string] string
Sdk map[string] string
Hashicorp map[string] string
Other map[string] string
Standard map[string]string
Sdk map[string]string
Hashicorp map[string]string
Other map[string]string
}

func (o *Manager) AddStandardImport(path, shortName string) { o.Standard[path] = shortName }
func (o *Manager) AddSdkImport(path, shortName string) { o.Sdk[path] = shortName }
func (o *Manager) AddStandardImport(path, shortName string) { o.Standard[path] = shortName }
func (o *Manager) AddSdkImport(path, shortName string) { o.Sdk[path] = shortName }
func (o *Manager) AddHashicorpImport(path, shortName string) { o.Hashicorp[path] = shortName }
func (o *Manager) AddOtherImport(path, shortName string) { o.Other[path] = shortName }
func (o *Manager) AddOtherImport(path, shortName string) { o.Other[path] = shortName }

func (o *Manager) Merge(v *Manager) {
if v == nil {
return
}
if v == nil {
return
}

for key, value := range v.Standard {
o.Standard[key] = value
}
for key, value := range v.Standard {
o.Standard[key] = value
}

for key, value := range v.Sdk {
o.Sdk[key] = value
}
for key, value := range v.Sdk {
o.Sdk[key] = value
}

for key, value := range v.Hashicorp {
o.Hashicorp[key] = value
}
for key, value := range v.Hashicorp {
o.Hashicorp[key] = value
}

for key, value := range v.Other {
o.Other[key] = value
}
for key, value := range v.Other {
o.Other[key] = value
}
}

func (o *Manager) RenderImports() (string, error) {
needSpacing := false

fm := template.FuncMap{
"SortLibs": func(v map[string] string) []string { return sortImports(v) },
"Render": func(v map[string] string) (string, error) {
list := sortImports(v)
ans, err := renderBlock(list)
if ans != "" {
if needSpacing {
ans = fmt.Sprintf("\n%s", ans)
} else {
needSpacing = true
}
}
return ans, err
},
}

t := template.Must(
template.New(
"import-render",
).Funcs(
fm,
).Parse(`
needSpacing := false

fm := template.FuncMap{
"SortLibs": func(v map[string]string) []string { return sortImports(v) },
"Render": func(v map[string]string) (string, error) {
list := sortImports(v)
ans, err := renderBlock(list)
if ans != "" {
if needSpacing {
ans = fmt.Sprintf("\n%s", ans)
} else {
needSpacing = true
}
}
return ans, err
},
}

t := template.Must(
template.New(
"import-render",
).Funcs(
fm,
).Parse(`
{{- /* Begin */ -}}
{{- $obj := . }}
import (
Expand All @@ -85,53 +84,53 @@ import (
{{- Render $obj.Other }}
)
{{- /* Done */ -}}`,
),
)
),
)

var b strings.Builder
err := t.Execute(&b, o)
var b strings.Builder
err := t.Execute(&b, o)

return b.String(), err
return b.String(), err
}

func renderBlock(libs []string) (string, error) {
if len(libs) == 0 {
return "", nil
}

t := template.Must(
template.New(
"render-import-block",
).Parse(`
if len(libs) == 0 {
return "", nil
}

t := template.Must(
template.New(
"render-import-block",
).Parse(`
{{- /* Begin */ -}}
{{- range $lib := . }}
{{ $lib }}
{{- end }}
{{- /* Done */ -}}`,
),
)
),
)

var b strings.Builder
err := t.Execute(&b, libs)
var b strings.Builder
err := t.Execute(&b, libs)

return b.String(), err
return b.String(), err
}

func sortImports(v map[string] string) []string {
libs := make([]string, 0, len(v))
for key := range v {
libs = append(libs, key)
}
sort.Strings(libs)

ans := make([]string, 0, len(libs))
for _, path := range libs {
if v[path] == "" {
ans = append(ans, fmt.Sprintf("%q", path))
} else {
ans = append(ans, fmt.Sprintf("%s %q", v[path], path))
}
}

return ans
func sortImports(v map[string]string) []string {
libs := make([]string, 0, len(v))
for key := range v {
libs = append(libs, key)
}
sort.Strings(libs)

ans := make([]string, 0, len(libs))
for _, path := range libs {
if v[path] == "" {
ans = append(ans, fmt.Sprintf("%q", path))
} else {
ans = append(ans, fmt.Sprintf("%s %q", v[path], path))
}
}

return ans
}
41 changes: 24 additions & 17 deletions pkg/load/file.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package load

import (
"encoding/json"
"fmt"
"strings"

"gopkg.in/yaml.v3"
"encoding/json"
"fmt"
"gopkg.in/yaml.v3"
"os"
"strings"
)

func Unmarshal(v []byte, o interface{}) error {
var err error

func File(v []byte, o interface{}) error {
var err error
runes := []byte(strings.TrimSpace(string(v)))
if len(runes) == 0 {
return fmt.Errorf("no data in file")
}

runes := []byte(strings.TrimSpace(string(v)))
if len(runes) == 0 {
return fmt.Errorf("no data in file")
}
if runes[0] == '{' && runes[len(runes)-1] == '}' {
err = json.Unmarshal(v, o)
} else {
err = yaml.Unmarshal(v, o)
}

if runes[0] == '{' && runes[len(runes)-1] == '}' {
err = json.Unmarshal(v, o)
} else {
err = yaml.Unmarshal(v, o)
}
return err
}

return err
func File(path string) ([]byte, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return content, err
}
Loading

0 comments on commit 813a4d9

Please sign in to comment.