Ejemplo n.º 1
0
func (dg *defaultGenerator) Generate(pathToServices []string) {

	// Open file that we will write all content to
	output, err := os.OpenFile(dg.outputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
	if err != nil {
		log.Fatalf("Could not open output file: %s", err)
	}
	defer func() {
		err := output.Close()
		if err != nil {
			log.Fatal("Could not close file!", err)
		}
	}()

	lookup := map[string]bool{}
	for _, servicePath := range pathToServices {
		if _, ok := lookup[servicePath]; ok {
			continue
		}
		lookup[servicePath] = true
		dg.vm.Services = append(dg.vm.Services, codegen.Service{
			Import: codegen.Import{
				Alias: "",
				Path:  servicePath,
			},
		})
	}

	dg.vm.RoutesNum = len(lookup)

	if err := codegen.Render(output, dg.tpl, dg.vm); err != nil {
		log.Fatalf("Could not render code: %s", err)
	}
}
Ejemplo n.º 2
0
func (dg *defaultGenerator) Generate(pathToServiceDir string) {

	// Open file that we will write all content to
	output, err := os.OpenFile(dg.outputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
	if err != nil {
		log.Fatalf("Could not open output file: %s", err)
	}
	defer func() {
		err := output.Close()
		if err != nil {
			log.Fatal("Could not close file!", err)
		}
	}()

	files, err := ioutil.ReadDir(pathToServiceDir)
	if err != nil {
		log.Fatalf("Could not read dir: %s", pathToServiceDir)
	}

	for _, f := range files {
		err = dg.visit(pathToServiceDir, f)
		if err != nil {
			log.Fatalf("Error while parsing: %s/%s", pathToServiceDir, f.Name())
		}
	}

	if err != nil {
		log.Fatalf("Error while iterating over directory: %s", err)
	}

	if err := codegen.Render(output, dg.tpl, dg.vm); err != nil {
		log.Fatalf("Could not render code: %s", err)
	}
}
Ejemplo n.º 3
0
// GenerateReplicationControllers renders k8s ReplicationControllers
func GenerateReplicationControllers(outputFile string, rcvm RCViewModel) {
	basePath := filepath.Dir(outputFile)
	ok := utils.Exists(basePath)
	if !ok {
		if err := os.Mkdir(basePath, 0777); err != nil {
			log.Fatalf("Could not make dir, %s", err)
		}
	}

	// Open file that we will write all content to
	output, err := os.OpenFile(outputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
	if err != nil {
		log.Fatalf("Could not open output file: %s", err)
	}
	defer func() {
		err := output.Close()
		if err != nil {
			log.Fatal("Could not close file!", err)
		}
	}()

	if err := codegen.Render(output, string(ReplicationControllerTemplate), rcvm); err != nil {
		log.Fatalf("Could not render replication controller: %s", err)
	}
}