Exemplo n.º 1
0
func (a *AnalyzerBase) refineServices(services *[]*common.Service) {
	var err error
	for _, service := range *services {
		if service.Command, err = common.ParseEnvironmentVariables(service.Command); err != nil {
			common.PrintlnError("Failed to replace environment variable placeholder due to %s", err.Error())
		}

		if service.Command, err = common.ParseUniqueInt(service.Command); err != nil {
			common.PrintlnError("Failed to replace UNIQUE_INT variable placeholder due to %s", err.Error())
		}
	}
}
func (w *TemplateWriterBase) WriteTemplate(templateName string, filename string, context interface{}) error {
	tmpl, err := template.ParseFiles(filepath.Join(w.TemplateDir, templateName))
	if err != nil {
		return err
	}

	destFullPath := filepath.Join(w.OutputDir, filename)
	if !w.shouldOverwriteExistingFile(destFullPath) {
		newName := filename + ".old"
		err = os.Rename(destFullPath, filepath.Join(w.OutputDir, newName))
		if err != nil {
			return err
		}
		common.PrintlnL2("Renaming %s to %s...", filename, newName)
	}

	destFile, err := os.Create(destFullPath)
	if err != nil {
		return err
	}
	defer func() {
		if err := destFile.Close(); err != nil {
			common.PrintlnError("Cannot close file %s due to: %s", filename, err.Error())
		}
	}()

	common.PrintlnL2("Writing %s...", filename)
	err = tmpl.Execute(destFile, context)
	if err != nil {
		return err
	}

	return w.prependToFile(destFullPath, templateHeader)
}
Exemplo n.º 3
0
func (b *AnalyzerBase) AnalyzeServices(a Analyzer, envVars []*common.EnvVar, gitBranch string, gitURL string, buildRoot string) ([]*common.Service, error) {
	services, err := b.analyzeProcfile()
	if err != nil {
		common.PrintlnError("Failed to parse Procfile due to %s", err.Error())
		return nil, err
	}

	err = a.FillServices(&services)
	if err != nil {
		return nil, err
	}
	b.refineServices(&services)
	b.inheritProjectContext(&services, envVars, gitBranch, gitURL, buildRoot)
	return services, nil
}
Exemplo n.º 4
0
func main() {
	args := os.Args[1:]

	if len(args) > 0 && args[0] == "help" {
		flag.PrintDefaults()
		return
	}

	if len(args) > 0 && (args[0] == "version" || args[0] == "v") {
		fmt.Printf("Cloud 66 Starter (%s)\n", VERSION)
		fmt.Println("Copyright 2015 Cloud66 Inc.")
		return
	}

	if len(args) > 0 && args[0] == "version" {
		common.PrintlnTitle("Starter version: %s (%s)", VERSION, BUILD_DATE)
		return
	}

	flag.Parse()

	common.PrintlnTitle("Cloud 66 Starter ~ (c) 2015 Cloud 66")

	if flagPath == "" {
		pwd, err := os.Getwd()
		if err != nil {
			common.PrintlnError("Unable to detect current directory path due to %s", err.Error())
		}
		flagPath = pwd
	}

	// if templateFolder is specified we're going to use that otherwise download
	if flagTemplates == "" {
		usr, _ := user.Current()
		homeDir := usr.HomeDir

		flagTemplates = filepath.Join(homeDir, ".starter")
		err := getTempaltes(flagTemplates)
		if err != nil {
			common.PrintlnError("Failed to download latest templates due to %s", err.Error())
			os.Exit(1)
		}

		dockerfileTemplateDir = flagTemplates
		serviceYAMLTemplateDir = flagTemplates
	} else {
		common.PrintlnTitle("Using local templates at %s", flagTemplates)
		flagTemplates, err := filepath.Abs(flagTemplates)
		if err != nil {
			common.PrintlnError("Failed to use %s for templates due to %s", flagTemplates, err.Error())
			os.Exit(1)
		}
		dockerfileTemplateDir = flagTemplates
		serviceYAMLTemplateDir = flagTemplates
	}

	common.PrintlnTitle("Detecting framework for the project at %s", flagPath)

	pack, err := Detect(flagPath)
	if err != nil {
		common.PrintlnError("Failed to detect framework due to: %s", err.Error())
		return
	}

	err = pack.Analyze(flagPath, flagEnvironment, !flagNoPrompt)
	if err != nil {
		common.PrintlnError("Failed to analyze the project due to: %s", err.Error())
		return
	}

	err = pack.WriteDockerfile(dockerfileTemplateDir, flagPath, !flagNoPrompt)
	if err != nil {
		common.PrintlnError("Failed to write Dockerfile due to: %s", err.Error())
	}

	err = pack.WriteServiceYAML(serviceYAMLTemplateDir, flagPath, !flagNoPrompt)
	if err != nil {
		common.PrintlnError("Failed to write service.yml due to: %s", err.Error())
	}

	if len(pack.GetMessages()) > 0 {
		common.PrintlnWarning("Warnings:")
		for _, warning := range pack.GetMessages() {
			common.PrintlnWarning(" * " + warning)
		}
	}

	common.PrintlnTitle("Done")
}