github.com/sacloud/iaas-api-go@v1.12.0/internal/tools/functions.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 tools
    16  
    17  import (
    18  	"bytes"
    19  	"go/build"
    20  	"go/format"
    21  	"log"
    22  	"os"
    23  	"path/filepath"
    24  	"text/template"
    25  )
    26  
    27  // TemplateConfig ソース生成を行うためのテンプレート設定
    28  type TemplateConfig struct {
    29  	OutputPath         string
    30  	Template           string
    31  	Parameter          interface{}
    32  	PreventOverwriting bool
    33  }
    34  
    35  // WriteFileWithTemplate 指定の設定に従いファイル出力
    36  func WriteFileWithTemplate(config *TemplateConfig) bool {
    37  	buf := bytes.NewBufferString("")
    38  	t := template.New("t")
    39  	template.Must(t.Parse(config.Template))
    40  	if err := t.Execute(buf, config.Parameter); err != nil {
    41  		log.Fatalf("writing output: %s", err)
    42  	}
    43  
    44  	// create dir
    45  	if _, err := os.Stat(filepath.Dir(config.OutputPath)); err != nil && os.IsNotExist(err) {
    46  		if err := os.MkdirAll(filepath.Dir(config.OutputPath), 0755); err != nil {
    47  			log.Fatal(err)
    48  		}
    49  	}
    50  
    51  	if config.PreventOverwriting {
    52  		if _, err := os.Stat(config.OutputPath); err == nil {
    53  			return false
    54  		}
    55  	}
    56  
    57  	// write to file
    58  	if err := os.WriteFile(config.OutputPath, Sformat(buf.Bytes()), 0644); err != nil { //nolint:gosec
    59  		log.Fatalf("writing output: %s", err)
    60  	}
    61  	return true
    62  }
    63  
    64  // Gopath returns GOPATH
    65  func Gopath() string {
    66  	gopath := build.Default.GOPATH
    67  	gopath = filepath.SplitList(gopath)[0]
    68  	return gopath
    69  }
    70  
    71  // ProjectRootPath プロジェクトルートパス
    72  func ProjectRootPath() string {
    73  	return filepath.Join(Gopath(), "src/github.com/sacloud/iaas-api-go")
    74  }
    75  
    76  // Sformat formats go source codes
    77  func Sformat(buf []byte) []byte {
    78  	src, err := format.Source(buf)
    79  	if err != nil {
    80  		// Should never happen, but can arise when developing this code.
    81  		// The user can compile the output to see the error.
    82  		log.Printf("warning: internal error: invalid Go generated: %s", err)
    83  		log.Printf("warning: compile the package to analyze the error")
    84  		log.Printf("generated: \n%s", string(buf))
    85  		return buf
    86  	}
    87  	return src
    88  }