vitess.io/vitess@v0.16.2/go/tools/asthelpergen/main/main.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"log"
    21  	"os"
    22  
    23  	"github.com/spf13/pflag"
    24  
    25  	"vitess.io/vitess/go/tools/asthelpergen"
    26  
    27  	"vitess.io/vitess/go/tools/goimports"
    28  )
    29  
    30  func main() {
    31  	var options asthelpergen.Options
    32  	var verify bool
    33  
    34  	pflag.StringSliceVar(&options.Packages, "in", nil, "Go packages to load the generator")
    35  	pflag.StringVar(&options.RootInterface, "iface", "", "Root interface generate rewriter for")
    36  	pflag.StringSliceVar(&options.Clone.Exclude, "clone_exclude", nil, "don't deep clone these types")
    37  	pflag.StringSliceVar(&options.Equals.AllowCustom, "equals_custom", nil, "generate custom comparators for these types")
    38  	pflag.BoolVar(&verify, "verify", false, "ensure that the generated files are correct")
    39  	pflag.Parse()
    40  
    41  	result, err := asthelpergen.GenerateASTHelpers(&options)
    42  	if err != nil {
    43  		log.Fatal(err)
    44  	}
    45  
    46  	if verify {
    47  		for _, err := range asthelpergen.VerifyFilesOnDisk(result) {
    48  			log.Fatal(err)
    49  		}
    50  		log.Printf("%d files OK", len(result))
    51  	} else {
    52  		for fullPath, file := range result {
    53  			content, err := goimports.FormatJenFile(file)
    54  			if err != nil {
    55  				log.Fatalf("failed to apply goimport to '%s': %v", fullPath, err)
    56  			}
    57  			err = os.WriteFile(fullPath, content, 0664)
    58  			if err != nil {
    59  				log.Fatalf("failed to save file to '%s': %v", fullPath, err)
    60  			}
    61  			log.Printf("saved '%s'", fullPath)
    62  		}
    63  	}
    64  }