예제 #1
0
파일: serve.go 프로젝트: kulikov/serve
func main() {
	manifestFile := kingpin.Flag("manifest", "Path to manifest.yml file.").Default("manifest.yml").String()
	plugin := kingpin.Arg("plugin", "Plugin name for run.").String()
	vars := *kingpin.Flag("var", "key=value pairs with manifest vars.").StringMap()
	dryRun := kingpin.Flag("dry-run", "Show manifest section only").Bool()
	noColor := kingpin.Flag("no-color", "Disable colored output").Bool()
	pluginData := kingpin.Flag("plugin-data", "Data for plugin").String()

	kingpin.Version(version)
	kingpin.Parse()

	var plugins []manifest.PluginData
	var err error
	var manifestData *manifest.Manifest

	color.NoColor = *noColor

	if *pluginData != "" {
		manifestData = manifest.LoadJSON(*pluginData)
	} else {
		manifestData = manifest.Load(*manifestFile, vars)
	}

	if *plugin == "" && *dryRun {
		fmt.Printf("%s\n%s\n%s\n",
			color.GreenString(">>> manifest:"),
			manifestData.String(),
			color.GreenString("<<< manifest: OK\n"))
		return
	}

	if *pluginData != "" {
		plugins = []manifest.PluginData{manifestData.GetPluginWithData(*plugin)}
	} else {
		plugins, err = manifestData.FindPlugins(*plugin)
	}

	if err != nil {
		log.Fatalln(color.RedString("Error find plugins for '%s': %v", *plugin, err))
	}

	for _, pair := range plugins {
		log.Printf("%s\n%s\n\n", color.GreenString(">>> %s:", pair.PluginName), color.CyanString("%s", pair.Data))

		if !*dryRun {
			if err := pair.Plugin.Run(pair.Data); err != nil {
				fmt.Println("")
				log.Fatalln(color.RedString("Error on run plugin `%s`: %v", pair.PluginName, err))
			} else {
				log.Println(color.GreenString("<<< %s: OK", pair.PluginName))
			}
		}
	}
}