Ejemplo n.º 1
0
func (e *ExecEnv) templateContext(out io.Writer, tag string) (int, error) {
	tag, defValue, hasDefault := splitDefault(tag)

	write := func(val string, err error) (int, error) {
		if err != nil {
			return 0, err
		}
		if val == "" {
			if !hasDefault {
				return 0, fmt.Errorf("a value is required for variable %q", tag)
			}
			val = defValue
		}
		return out.Write(bytes.NewBufferString(val).Bytes())
	}

	prefix, suffix := splitPrefix(tag)
	switch prefix {
	case "env":
		return write(os.Getenv(suffix), nil)
	case "git":
		return valueFromGit(out, suffix, defValue)
	case "time":
		return write(fmtdate.Format(suffix, e.startTime), nil)
	case "fs":
		val, err := valueFromFilesystem(suffix, e.workingDir)
		return write(val, err)
	case "user":
		val, err := valueFromUser(suffix)
		return write(val, err)
	}

	switch tag {
	case "unique":
		return write(e.Unique(), nil)
	case "project":
		return write(e.Project, nil)
	case "exec-id":
		return write(e.ExecID, nil)
	default:
		return 0, fmt.Errorf("unknown variable %q", tag)
	}
}
Ejemplo n.º 2
0
func decodeCokJSON(body string) {
	items, err := simplejson.NewJson([]byte(body))
	if err != nil {
		panic("json format error")
	}

	index := 0

	fmt.Printf("\r%9s\t%2s\t%10s\t%10s\t%12s\n", "x", "y", "name", "time", "sid")
	fmt.Println("-------------------------------------------------------------")

	for {
		item := items.GetIndex(index)

		xPosition := item.Get("x").MustInt()
		yPosition := item.Get("y").MustInt()
		name := item.Get("name").MustString()
		date := item.Get("lasttime").MustInt64()
		sid := item.Get("sid").MustInt()

		if name == "" && xPosition == 0 && yPosition == 0 {
			return
		}

		tm := time.Unix(date, 0)

		fmt.Printf("%2v - %5d\t%3d\t%8s\t%10s\t%5d\n",
			index+1,
			xPosition,
			yPosition,
			name,
			fmtdate.Format("YYYY-MM-DD hh:mm:ss", tm),
			sid)

		index++
	}
}
Ejemplo n.º 3
0
func now() string {
	return fmtdate.Format("YYYY-MM-DD-hh-mm-ss", time.Now())
}
Ejemplo n.º 4
0
func GetDate() string {
	date := fmtdate.Format("DD.MM.YYYY", time.Now())
	return date
}
Ejemplo n.º 5
0
func (c *Config) usageOptions(addGeneral bool, skipped map[string]bool, relaxed map[string]bool) string {
	var optBf bytes.Buffer

	for optName, opt := range c.spec {
		if _, has := skipped[optName]; has {
			continue
		}
		optBf.WriteString("\n")

		var left bytes.Buffer
		if _, has := relaxed[optName]; has || !opt.Required {
			left.WriteString("[")
		}

		if opt.Shortflag != "" {
			left.WriteString("-" + opt.Shortflag + ", ")
		}
		left.WriteString("--" + optName)

		if opt.Default != nil {

			switch opt.Type {
			case "string":
				left.WriteString(fmt.Sprintf("='%s'", opt.Default))
			case "bool":
				if opt.Default.(bool) {
					left.WriteString("=true")
				} else {
					left.WriteString("=false")
				}
			case "json":
				left.WriteString(fmt.Sprintf("='%s'", opt.Default))
			case "time":
				left.WriteString(fmt.Sprintf("='%s'", fmtdate.Format("hh:mm:ss", opt.Default.(time.Time))))
			case "date":
				left.WriteString(fmt.Sprintf("='%s'", fmtdate.Format("YYYY-MM-DD", opt.Default.(time.Time))))
			case "datetime":
				left.WriteString(fmt.Sprintf("='%s'", fmtdate.Format("YYYY-MM-DD hh:mm:ss", opt.Default.(time.Time))))
			default:
				left.WriteString(fmt.Sprintf("=%v", opt.Default))

			}

		} else {
			if opt.Type != "bool" {
				left.WriteString(fmt.Sprintf("=%s", convertOpttype(opt.Type)))
			}
		}

		/*
			if opt.Required {
				left.WriteString(" (required)")
			}
		*/
		if _, has := relaxed[optName]; has || !opt.Required {
			left.WriteString("]")
		}

		optBf.WriteString(pad("  "+left.String(), opt.Help))
		//optBf.WriteString("\t\t" + strings.Join(strings.Split(opt.Help, "\n"), "\n\t\t"))
	}

	if !c.isCommand() && addGeneral {
		generalOptions := map[string]string{
			"version":          "prints the current version of the program",
			"help":             "prints the help",
			"config-spec":      "prints the specification of the configurable options",
			"config-env":       "prints the environmental variables of the configurable options",
			"config-locations": "prints the locations of current configuration",
			"config-files":     "prints the locations of the config files",
		}

		for optname, opthelp := range generalOptions {
			optBf.WriteString("\n" + pad("  --"+optname, opthelp))
		}
	}

	return optBf.String()
}