diff --git a/v2alpha/cmd/goupnp2srvgen/main.go b/v2alpha/cmd/goupnp2srvgen/main.go index 7ed7b65..709a0e5 100644 --- a/v2alpha/cmd/goupnp2srvgen/main.go +++ b/v2alpha/cmd/goupnp2srvgen/main.go @@ -24,6 +24,7 @@ import ( ) var ( + outputDir = flag.String("output_dir", "", "Path to directory to write output in.") srvManifests = flag.String("srv_manifests", "", "Path to srvmanifests.toml") srvTemplate = flag.String("srv_template", "", "Path to srv.gotemplate.") upnpresourcesZip = flag.String("upnpresources_zip", "", "Path to upnpresources.zip.") @@ -44,6 +45,13 @@ func run() error { return fmt.Errorf("unused arguments: %s", strings.Join(flag.Args(), " ")) } + if *outputDir == "" { + return errors.New("-output_dir is a required flag.") + } + if err := os.MkdirAll(*outputDir, 0); err != nil { + return fmt.Errorf("creating output_dir %q: %w", *outputDir, err) + } + if *srvManifests == "" { return errors.New("-srv_manifests is a required flag.") } @@ -85,8 +93,8 @@ func run() error { } for _, m := range manifests.DCPS { - if err := processDCP(upnpresources, m, typeMap, tmpl); err != nil { - return fmt.Errorf("processing DCP %s: %w", m.Path, err) + if err := processDCP(upnpresources, m, typeMap, tmpl, *outputDir); err != nil { + return fmt.Errorf("processing DCP %s: %w", m.SpecZipPath, err) } } return nil @@ -97,13 +105,18 @@ func processDCP( manifest *DCPSpecManifest, typeMap typedesc.TypeMap, tmpl *template.Template, + parentOutputDir string, ) error { - dcpSpecData, err := upnpresources.OpenZip(manifest.Path) + outputDir := filepath.Join(parentOutputDir, manifest.OutputDir) + if err := os.MkdirAll(outputDir, os.ModePerm); err != nil { + return fmt.Errorf("creating output directory %q for DCP: %w", outputDir, err) + } + dcpSpecData, err := upnpresources.OpenZip(manifest.SpecZipPath) if err != nil { return err } for _, srvManifest := range manifest.Services { - if err := processService(dcpSpecData, srvManifest, typeMap, tmpl); err != nil { + if err := processService(dcpSpecData, srvManifest, typeMap, tmpl, outputDir); err != nil { return fmt.Errorf("processing service %s: %w", srvManifest.ServiceType, err) } } @@ -115,7 +128,13 @@ func processService( srvManifest *ServiceManifest, typeMap typedesc.TypeMap, tmpl *template.Template, + parentOutputDir string, ) error { + outputDir := filepath.Join(parentOutputDir, srvManifest.Package) + if err := os.MkdirAll(outputDir, os.ModePerm); err != nil { + return fmt.Errorf("creating output directory %q for service: %w", outputDir, err) + } + f, err := dcpSpecData.Open(srvManifest.Path) if err != nil { return err @@ -140,7 +159,13 @@ func processService( return err } - err = tmpl.ExecuteTemplate(os.Stdout, "service", tmplArgs{ + outputPath := filepath.Join(outputDir, srvManifest.Package+".go") + outFile, err := os.Create(outputPath) + if err != nil { + return fmt.Errorf("creating output service file %q: %w", outputPath, err) + } + defer outFile.Close() + err = tmpl.ExecuteTemplate(outFile, "service", tmplArgs{ Manifest: srvManifest, Imps: imps, SCPD: sd, @@ -148,6 +173,9 @@ func processService( if err != nil { return fmt.Errorf("executing srv_template: %w", err) } + if err := outFile.Close(); err != nil { + return fmt.Errorf("closing output service file %q: %w", outputPath, err) + } return nil } @@ -157,8 +185,10 @@ type DCPSpecManifests struct { } type DCPSpecManifest struct { - // Path is the file path within upnpresources.zip to the DCP spec ZIP file. - Path string `toml:"path"` + // SpecZipPath is the file path within upnpresources.zip to the DCP spec ZIP file. + SpecZipPath string `toml:"spec_zip_path"` + // OutputDir is the path relative to --output_dir which the packages are written in. + OutputDir string `toml:"output_dir"` // Services maps from a service name (e.g. "FooBar:1") to a path within the DCP spec ZIP file // (e.g. "xml data files/service/FooBar1.xml"). Services []*ServiceManifest `toml:"service"` diff --git a/v2alpha/srv/srvmanifests.toml b/v2alpha/srv/srvmanifests.toml index 6280b24..925a8ed 100644 --- a/v2alpha/srv/srvmanifests.toml +++ b/v2alpha/srv/srvmanifests.toml @@ -1,10 +1,11 @@ [[dcp]] -path = "standardizeddcps/Internet Gateway_2/UPnP-gw-IGD-TestFiles-20101210.zip" +spec_zip_path = "standardizeddcps/Internet Gateway_2/UPnP-gw-IGD-TestFiles-20101210.zip" +output_dir = "inetgw2" [[dcp.service]] -package = "lanhostconfigmanagement1" +package = "lanhostcfgmng1" type = "urn:schemas-upnp-org:service:LANHostConfigManagement:1" path = "xml data files/service/LANHostConfigManagement1.xml" [[dcp.service]] -package = "wanpppconnection1" +package = "wanpppconn1" type = "urn:schemas-upnp-org:service:WANPPPConnection:1" path = "xml data files/service/WANPPPConnection1.xml"