예제 #1
0
func loadTemplate(args []string) *common.Template {
	var template *common.Template
	var err error
	if len(args) < 2 {
		fmt.Fprintln(os.Stderr, "No template name or configuration(s) supplied")
		usage()
	}

	if *stdin {
		if len(args) < 2 {
			usage()
		}

		input, err := ioutil.ReadAll(os.Stdin)
		if err != nil {
			panic(err)
		}

		r := bytes.NewReader(input)
		template, err = expander.NewTemplateFromArchive(args[1], r, args[2:])
		if err != nil {
			if err != tar.ErrHeader {
				panic(err)
			}

			r := bytes.NewReader(input)
			template, err = expander.NewTemplateFromReader(args[1], r, args[2:])
			if err != nil {
				panic(fmt.Errorf("cannot create configuration from supplied arguments: %s\n", err))
			}
		}
	} else {
		// See if the first argument is a local file. It could either be a type, or it could be a configuration. If
		// it's a local file, it's configuration.
		if _, err := os.Stat(args[1]); err == nil {
			if len(args) > 2 {
				template, err = expander.NewTemplateFromFileNames(args[1], args[2:])
			} else {
				template, err = expander.NewTemplateFromRootTemplate(args[1])
			}
		} else {
			template = buildTemplateFromType(args[1])
		}

		if err != nil {
			panic(fmt.Errorf("cannot create configuration from supplied arguments: %s\n", err))
		}
	}

	// Override name if set from flags.
	if *deployment_name != "" {
		template.Name = *deployment_name
	}

	return template
}
예제 #2
0
func loadTemplate(args []string) *common.Template {
	var template *common.Template
	var err error
	if len(args) < 2 {
		fmt.Fprintln(os.Stderr, "No template name or configuration(s) supplied")
		usage()
	}

	if *stdin {
		if len(args) < 2 {
			usage()
		}

		input, err := ioutil.ReadAll(os.Stdin)
		if err != nil {
			panic(err)
		}

		r := bytes.NewReader(input)
		template, err = expander.NewTemplateFromArchive(args[1], r, args[2:])
		if err != nil {
			if err != tar.ErrHeader {
				panic(err)
			}

			r := bytes.NewReader(input)
			template, err = expander.NewTemplateFromReader(args[1], r, args[2:])
			if err != nil {
				panic(fmt.Errorf("cannot create configuration from supplied arguments: %s\n", err))
			}
		}
	} else {
		if len(args) < 3 {
			if t := getRegistryType(args[1]); t != nil {
				template = buildTemplateFromType(*t)
			} else {
				template, err = expander.NewTemplateFromRootTemplate(args[1])
			}
		} else {
			template, err = expander.NewTemplateFromFileNames(args[1], args[2:])
		}

		if err != nil {
			panic(fmt.Errorf("cannot create configuration from supplied arguments: %s\n", err))
		}
	}

	// Override name if set from flags.
	if *deployment_name != "" {
		template.Name = *deployment_name
	}

	return template
}