Beispiel #1
0
func randomStringCommand(_ *command.Args, opts *randomStringOptions) {
	fmt.Println(stringutil.RandomPrintable(opts.Length))
}
Beispiel #2
0
// ExpandInto expands the template into the given directory, creating it. If
// the directory already exists and it's non-empty, and error is returned.
func (t *Template) ExpandInto(dir string, gae bool) error {
	if exists, _ := fileutil.Exists(dir); exists && !isEmptyDir(dir) {
		return fmt.Errorf("%s already exists", dir)
	}
	if err := os.MkdirAll(dir, 0755); err != nil {
		return err
	}
	data, err := t.Data(gae)
	if err != nil {
		return err
	}
	zr, err := gzip.NewReader(bytes.NewReader(data))
	if err != nil {
		return err
	}
	defer zr.Close()
	r := tar.NewReader(zr)

	hdr, err := r.Next()
	if err != nil {
		return err
	}
	// Data to pass to templates
	tmplData := map[string]interface{}{
		"Port":             10000 + rand.Intn(20001), // random port between 10k and 30k
		"AppSecret":        stringutil.RandomPrintable(64),
		"AppEncryptionKey": stringutil.RandomPrintable(32),
		"DevSecret":        stringutil.RandomPrintable(64),
		"DevEncryptionKey": stringutil.RandomPrintable(32),
		"ProjectName":      filepath.Base(dir),
	}
	for hdr != nil {
		p := filepath.Join(dir, filepath.FromSlash(hdr.Name))
		info := hdr.FileInfo()
		if info.IsDir() {
			log.Debugf("creating directory %s", p)
			if err := os.MkdirAll(p, info.Mode()); err != nil {
				panic(err)
			}
		} else if filepath.Base(p) != ".keep" {
			// .keep is used to make git keep the directory
			data, err := ioutil.ReadAll(r)
			if err != nil {
				panic(err)
			}
			if output, ok := isTemplateFile(p); ok {
				p = output
				tmpl, err := template.New(filepath.Base(p)).Parse(string(data))
				if err != nil {
					panic(err)
				}
				var buf bytes.Buffer
				if err := tmpl.Execute(&buf, tmplData); err != nil {
					panic(err)
				}
				data = buf.Bytes()
			}
			log.Debugf("writing file %s", p)
			if err := ioutil.WriteFile(p, data, info.Mode()); err != nil {
				return err
			}
		}
		hdr, err = r.Next()
		if err != nil && err != io.EOF {
			return err
		}
	}
	return nil
}