github.com/sacloud/iaas-api-go@v1.12.0/internal/tools/gen-api-interfaces/main.go (about)

     1  // Copyright 2022-2023 The sacloud/iaas-api-go Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"log"
    19  	"path/filepath"
    20  
    21  	"github.com/sacloud/iaas-api-go/internal/define"
    22  	"github.com/sacloud/iaas-api-go/internal/tools"
    23  )
    24  
    25  const destination = "zz_apis.go"
    26  
    27  func init() {
    28  	log.SetFlags(0)
    29  	log.SetPrefix("gen-api-interfaces: ")
    30  }
    31  
    32  func main() {
    33  	tools.WriteFileWithTemplate(&tools.TemplateConfig{
    34  		OutputPath: filepath.Join(tools.ProjectRootPath(), destination),
    35  		Template:   tmpl,
    36  		Parameter:  define.APIs,
    37  	})
    38  	log.Printf("generated: %s\n", filepath.Join(tools.ProjectRootPath(), destination))
    39  }
    40  
    41  const tmpl = `// generated by 'github.com/sacloud/iaas-api-go/internal/tools/gen-api-interfaces'; DO NOT EDIT
    42  
    43  package iaas
    44  
    45  import (
    46  	"context"
    47  	"github.com/sacloud/iaas-api-go/types"
    48  )
    49  
    50  {{ range . }} {{ $typeName := .TypeName }} {{ $resource := . }}
    51  
    52  /************************************************* 
    53  * {{ $typeName }}API
    54  *************************************************/
    55  
    56  // {{ $typeName }}API is interface for operate {{ $typeName }} resource
    57  type {{ $typeName }}API interface {
    58  {{ range .Operations }}
    59  	{{ .MethodName }}(ctx context.Context{{if not $resource.IsGlobal}}, zone string{{end}}{{ range .Arguments }}, {{ .ArgName }} {{ .TypeName }}{{ end }}) {{.ResultsStatement}} 
    60  {{- end -}}
    61  }
    62  {{ end }}
    63  `