Exemple #1
0
func printResult(command Commander, resource *Resource) {
	ctx := command.Context()
	w := ctx.CLIContext.App.Writer
	keys := resource.Keys
	noHeader := false
	if ctx.GlobalOptions.noHeader {
		noHeader = true
	}
	switch resource.Result.(type) {
	case map[string]interface{}:
		m := resource.Result.(map[string]interface{})
		m = onlyNonNil(m)
		switch ctx.GlobalOptions.output {
		case "json":
			output.MetadataJSON(w, m, keys)
		case "csv":
			output.MetadataCSV(w, m, keys, noHeader)
		default:
			output.MetadataTable(w, m, keys)
		}
	case []map[string]interface{}:
		ms := resource.Result.([]map[string]interface{})
		for i, m := range ms {
			ms[i] = onlyNonNil(m)
		}
		switch ctx.GlobalOptions.output {
		case "json":
			output.ListJSON(w, ms, keys)
		case "csv":
			output.ListCSV(w, ms, keys, noHeader)
		default:
			output.ListTable(w, ms, keys, noHeader)
		}
	case io.Reader:
		if _, ok := resource.Result.(io.ReadCloser); ok {
			defer resource.Result.(io.ReadCloser).Close()
		}
		_, err := io.Copy(w, resource.Result.(io.Reader))
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error copying (io.Reader) result: %s\n", err)
		}
	default:
		switch ctx.GlobalOptions.output {
		case "json":
			output.DefaultJSON(w, resource.Result)
		default:
			fmt.Fprintf(w, "%v\n", resource.Result)
		}
	}
}
Exemple #2
0
// Print returns the output to the user
func (ctx *Context) Print(resource *Resource) {
	defer ctx.WaitGroup.Done()

	// limit the returned fields if any were given in the `fields` flag
	keys := ctx.limitFields(resource)
	w := ctx.CLIContext.App.Writer

	switch resource.Result.(type) {
	case map[string]interface{}:
		m := resource.Result.(map[string]interface{})
		m = onlyNonNil(m)
		switch ctx.OutputFormat {
		case "json":
			output.MetadataJSON(w, m, keys)
		case "csv":
			output.MetadataCSV(w, m, keys)
		default:
			output.MetadataTable(w, m, keys)
		}
	case []map[string]interface{}:
		ms := resource.Result.([]map[string]interface{})
		for i, m := range ms {
			ms[i] = onlyNonNil(m)
		}
		switch ctx.OutputFormat {
		case "json":
			output.ListJSON(w, ms, keys)
		case "csv":
			output.ListCSV(w, ms, keys)
		default:
			output.ListTable(w, ms, keys)
		}
	case io.Reader:
		if _, ok := resource.Result.(io.ReadCloser); ok {
			defer resource.Result.(io.ReadCloser).Close()
		}
		_, err := io.Copy(w, resource.Result.(io.Reader))
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error copying (io.Reader) result: %s\n", err)
		}
	default:
		switch ctx.OutputFormat {
		case "json":
			output.DefaultJSON(w, resource.Result)
		default:
			fmt.Fprintf(w, "%v", resource.Result)
		}
	}
}
Exemple #3
0
// Print returns the output to the user
func (ctx *Context) Print(resource *Resource) {
	defer ctx.WaitGroup.Done()

	// limit the returned fields if any were given in the `fields` flag
	keys := ctx.limitFields(resource)
	w := ctx.CLIContext.App.Writer
	if ctx.CLIContext.GlobalIsSet("json") || ctx.CLIContext.IsSet("json") {
		switch resource.Result.(type) {
		case map[string]interface{}:
			m := resource.Result.(map[string]interface{})
			output.MetadataJSON(w, m, keys)
		case []map[string]interface{}:
			m := resource.Result.([]map[string]interface{})
			output.ListJSON(w, m, keys)
		case io.Reader:
			if _, ok := resource.Result.(io.ReadCloser); ok {
				defer resource.Result.(io.ReadCloser).Close()
			}
			_, err := io.Copy(w, resource.Result.(io.Reader))
			if err != nil {
				fmt.Fprintf(os.Stderr, "Error copying (io.Reader) result: %s\n", err)
			}
		default:
			output.DefaultJSON(w, resource.Result)
		}
	} else if ctx.CLIContext.GlobalIsSet("csv") || ctx.CLIContext.IsSet("csv") {
		switch resource.Result.(type) {
		case map[string]interface{}:
			m := resource.Result.(map[string]interface{})
			output.MetadataCSV(w, m, keys)
		case []map[string]interface{}:
			m := resource.Result.([]map[string]interface{})
			output.ListCSV(w, m, keys)
		case io.Reader:
			if _, ok := resource.Result.(io.ReadCloser); ok {
				defer resource.Result.(io.ReadCloser).Close()
			}
			_, err := io.Copy(w, resource.Result.(io.Reader))
			if err != nil {
				fmt.Fprintf(os.Stderr, "Error copying (io.Reader) result: %s\n", err)
			}
		default:
			fmt.Fprintf(w, "%v", resource.Result)
		}
	} else {
		switch resource.Result.(type) {
		case map[string]interface{}:
			m := resource.Result.(map[string]interface{})
			output.MetadataTable(w, m, keys)
		case []map[string]interface{}:
			m := resource.Result.([]map[string]interface{})
			output.ListTable(w, m, keys)
		case io.Reader:
			if _, ok := resource.Result.(io.ReadCloser); ok {
				defer resource.Result.(io.ReadCloser).Close()
			}
			_, err := io.Copy(w, resource.Result.(io.Reader))
			if err != nil {
				fmt.Fprintf(os.Stderr, "Error copying (io.Reader) result: %s\n", err)
			}
		default:
			fmt.Fprintf(w, "%v", resource.Result)
		}
	}
}