Пример #1
0
func newCommand(args *command.Args, opts *newOptions) error {
	var projectTmplDir string
	if envDir := os.Getenv("GONDOLA_PROJECT_TEMPLATES"); envDir != "" {
		projectTmplDir = envDir
	} else {
		// Check for updated templates
		usr, err := user.Current()
		if err != nil {
			return err
		}
		cache := filepath.Join(usr.HomeDir, ".gondola", "project-templates")
		projectTmplDir = filepath.Join(cache, "templates")
		etagFile := filepath.Join(cache, "etag")
		if err := updateTemplates(projectTmplDir, etagFile); err != nil {
			// Check if the directory exists
			if st, _ := os.Stat(projectTmplDir); st == nil || !st.IsDir() {
				return err
			}
		}
	}
	tmpls, err := LoadTemplates(projectTmplDir)
	if err != nil {
		return err
	}
	if opts.List {
		w := tabwriter.NewWriter(os.Stdout, 8, 4, 2, ' ', 0)
		for _, v := range tmpls {
			fmt.Fprintf(w, "%s:\t%s\n", v.Name, v.Description)
		}
		return w.Flush()
	}
	if len(args.Args()) == 0 {
		return errors.New("missing directory name")
	}
	var projectTmpl *Template
	for _, v := range tmpls {
		if v.Name == opts.Template {
			projectTmpl = v
			break
		}
	}
	dir := args.Args()[0]
	if projectTmpl != nil {
		return projectTmpl.ExpandInto(dir, opts.Gae)
	}
	available := generic.Map(tmpls, func(t *Template) string { return t.Name }).([]string)
	return fmt.Errorf("template %s not found, availble ones are: %s", opts.Template, strings.Join(available, ", "))
}
Пример #2
0
func rmGenCommand(args *command.Args) error {
	dir := "."
	if len(args.Args()) > 0 {
		dir = args.Args()[0]
	}
	re := regexp.MustCompile("(?i).+\\.gen\\..+")
	return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
		if info != nil && !info.IsDir() && re.MatchString(path) {
			log.Infof("Removing %s", path)
			if err := os.Remove(path); err != nil {
				return err
			}
			dir := filepath.Dir(path)
			if infos, err := ioutil.ReadDir(dir); err == nil && len(infos) == 0 {
				log.Infof("Removing empty dir %s", dir)
				if err := os.Remove(dir); err != nil {
					return err
				}
			}
		}
		return nil
	})
}
Пример #3
0
func profileCommand(args *command.Args, opts *profileOptions) error {
	if len(args.Args()) == 0 {
		return errors.New("url can't be empty")
	}
	u := args.Args()[0]
	var values url.Values
	if opts.Data != "" {
		vals, err := url.ParseQuery(opts.Data)
		if err != nil {
			return fmt.Errorf("error parsing data %q: %s", opts.Data, err)
		}
		values = vals
	}
	parsed, err := url.Parse(u)
	if err != nil {
		return fmt.Errorf("invalid url %q: %s", u, err)
	}
	host := parsed.Host
	var secret string
	var info *profileInfo
	for {
		info, err = requestProfile(u, opts.Method, values, secret)
		if err == nil {
			break
		}
		if err == errAuthRequired {
			fmt.Printf("Enter secret for %s: ", host)
			secret = getPasswd()
			fmt.Println("")
			continue
		}
		if err == errAuthFailed {
			fmt.Printf("Incorrect secret\nEnter secret for %s: ", host)
			secret = getPasswd()
			fmt.Println("")
			continue
		}
		return err
	}
	width := 80
	fmt.Printf("total %s\n%s\n\n", info.Elapsed, strings.Repeat("=", width))
	other := info.Elapsed
	for _, v := range info.Timings {
		other -= v.Total()
		fmt.Printf("%s - %d events - %s\n%s\n", v.Name, v.Count(), v.Total(), strings.Repeat("-", width))
		maxLength := 0
		for _, ev := range v.Events {
			if length := len(fmt.Sprintf("%s", ev.Elapsed())); length > maxLength {
				maxLength = length
			}
		}
		for ii, ev := range v.Events {
			notesWidth := width - maxLength - 6
			notes := formatNotes(ev.Notes, notesWidth)
			fmt.Printf("| %s | %s |\n", pad(fmt.Sprintf("%s", ev.Elapsed()), maxLength), pad(notes[0], notesWidth))
			for _, n := range notes[1:] {
				fmt.Printf("| %s | %s |\n", pad("", maxLength), pad(n, notesWidth))
			}
			if ii < len(v.Events)-1 {
				fmt.Println(strings.Repeat("-", width))
			}
		}
		fmt.Printf("%s\n\n", strings.Repeat("=", width))
	}
	fmt.Printf("others - %s\n", other)
	return nil
}