github.com/mailru/activerecord@v1.12.2/cmd/argen/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"fmt"
     7  	"log"
     8  	"os"
     9  	"path/filepath"
    10  
    11  	argen "github.com/mailru/activerecord/internal/app"
    12  	"github.com/mailru/activerecord/internal/pkg/ds"
    13  	"golang.org/x/mod/modfile"
    14  )
    15  
    16  // ldflags
    17  var (
    18  	Version     string
    19  	BuildTime   string
    20  	BuildOS     string
    21  	BuildCommit string
    22  )
    23  
    24  func getAppInfo() *ds.AppInfo {
    25  	return ds.NewAppInfo().
    26  		WithVersion(Version).
    27  		WithBuildTime(BuildTime).
    28  		WithBuildOS(BuildOS).
    29  		WithBuildCommit(BuildCommit)
    30  }
    31  
    32  func main() {
    33  	ctx := context.Background()
    34  	path := flag.String("path", "./repository", "Path to repository dir")
    35  	fixturePath := flag.String("fixture_path", "", "Path to stores of tested fixtures")
    36  	declarationDir := flag.String("declaration", "declaration", "declaration subdir")
    37  	destinationDir := flag.String("destination", "generated", "generation subdir")
    38  	moduleName := flag.String("module", "", "module name from go.mod")
    39  	version := flag.Bool("version", false, "print version")
    40  	flag.Parse()
    41  
    42  	if *version {
    43  		fmt.Printf("Version %s; BuildCommit: %s\n", Version, BuildCommit)
    44  		os.Exit(0)
    45  	}
    46  
    47  	srcDir := filepath.Join(*path, *declarationDir)
    48  	dstDir := filepath.Join(*path, *destinationDir)
    49  
    50  	if *moduleName == "" {
    51  		goModBytes, err := os.ReadFile("go.mod")
    52  		if err != nil {
    53  			log.Fatalf("error get mod.go")
    54  		}
    55  
    56  		*moduleName = modfile.ModulePath(goModBytes)
    57  		if *moduleName == "" {
    58  			log.Fatalf("can't determine module name")
    59  		}
    60  	}
    61  
    62  	gen, err := argen.Init(ctx, getAppInfo(), srcDir, dstDir, *fixturePath, *moduleName)
    63  	if err != nil {
    64  		log.Fatalf("error initialization: %s", err)
    65  	}
    66  
    67  	if err := gen.Run(); err != nil {
    68  		log.Fatalf("error generate repository: %s", err)
    69  	}
    70  }