Example #1
0
func Packages(debug bool) (string, error) {

	distro := DetermineDistro()

	out, err := []byte{}, error(nil)

	switch distro {
	default:
		out, err = []byte{0}, errors.New("unknown")
	case "debian":
		out, err = exec.Command("dpkg-query", "-W").Output()
	case "redhat":
		out, err = exec.Command("rpm", "-qa", "--queryformat", "\"%{NAME}\t%{VERSION}\n\"").Output()
	}

	if err != nil {
		return "", err
	}

	reader := bytes.NewBuffer(out)
	scanner := bufio.NewScanner(reader)
	jsonMsg := "{\n"

	for scanner.Scan() {
		s := strings.Split(scanner.Text(), "\t")
		strutils.QuoteString(s[0])
		strutils.QuoteString(s[1])
		jsonMsg += "  " + strutils.QuoteString(s[0]) + " : " + strutils.QuoteString(s[1]) + ",\n"

	}

	jsonMsg = strutils.TrimSuffix(jsonMsg, ",\n")
	jsonMsg += "\n}"
	return jsonMsg, nil

}
Example #2
0
func JsonBuild(input interface{}, debug bool) string {
	m := input.(map[string]interface{})

	json := "{\n"

	for k, v := range m {
		switch vv := v.(type) {
		case string:
			if debug == true {
				fmt.Println("k:", k, "v:", v, "vv:", vv, "type:", reflect.TypeOf(v))
			}
			json += JsonJoin([]string{"  ", strutils.QuoteString(k), strutils.QuoteString(vv)})
		case int64:
			if debug == true {
				fmt.Println("k:", k, "v:", v, "vv:", vv, "type:", reflect.TypeOf(v))
			}
			json += JsonJoin([]string{"  ", strutils.QuoteString(k), strconv.FormatInt(vv, 1)})
		case float64:
			if debug == true {
				fmt.Println("k:", k, "v:", v, "vv:", vv, "type:", reflect.TypeOf(v))
			}
			json += JsonJoin([]string{"  ", strutils.QuoteString(k), strconv.FormatFloat(vv, 'f', -1, 64)})
		case bool:
			if debug == true {
				fmt.Println("k:", k, "v:", v, "vv:", vv, "type:", reflect.TypeOf(v))
			}
			json += JsonJoin([]string{"  ", strutils.QuoteString(k), strconv.FormatBool(vv)})
		default:
			if debug == true {
				fmt.Println("k:", k, "v:", v, "vv:", vv, "type:", reflect.TypeOf(v))
			}
			json += JsonJoin([]string{"  ", strutils.QuoteString(k), strutils.QuoteString("***UNKNOWN***")})
		}
	}

	json = strutils.TrimSuffix(json, ",\n")
	json += "\n}"

	return json
}