本文整理匯總了Golang中io.Writer.Flush方法的典型用法代碼示例。如果您正苦於以下問題:Golang Writer.Flush方法的具體用法?Golang Writer.Flush怎麽用?Golang Writer.Flush使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類io.Writer
的用法示例。
在下文中一共展示了Writer.Flush方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: printHostList
func printHostList(hostList []photon.Host, w io.Writer, c *cli.Context) error {
if c.GlobalIsSet("non-interactive") {
for _, host := range hostList {
tag := strings.Trim(fmt.Sprint(host.Tags), "[]")
scriptTag := strings.Replace(tag, " ", ",", -1)
fmt.Printf("%s\t%s\t%s\t%s\n", host.ID, host.State, host.Address, scriptTag)
}
} else if c.GlobalString("output") != "" {
utils.FormatObjects(hostList, w, c)
} else {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintf(w, "ID\tState\tIP\tTags\n")
for _, host := range hostList {
fmt.Fprintf(w, "%s\t%v\t%s\t%s\n", host.ID, host.State, host.Address, strings.Trim(fmt.Sprint(host.Tags), "[]"))
}
err := w.Flush()
if err != nil {
return err
}
fmt.Printf("\nTotal: %d\n", len(hostList))
}
return nil
}
示例2: printClusterList
func printClusterList(clusterList []photon.Cluster, w io.Writer, c *cli.Context, summaryView bool) error {
stateCount := make(map[string]int)
for _, cluster := range clusterList {
stateCount[cluster.State]++
}
if c.GlobalIsSet("non-interactive") {
if !summaryView {
for _, cluster := range clusterList {
fmt.Printf("%s\t%s\t%s\t%s\t%d\n", cluster.ID, cluster.Name, cluster.Type, cluster.State, cluster.WorkerCount)
}
}
} else if c.GlobalString("output") != "" {
utils.FormatObjects(clusterList, w, c)
} else {
if !summaryView {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintf(w, "ID\tName\tType\tState\tWorker Count\n")
for _, cluster := range clusterList {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\n", cluster.ID, cluster.Name, cluster.Type, cluster.State, cluster.WorkerCount)
}
err := w.Flush()
if err != nil {
return err
}
}
fmt.Printf("\nTotal: %d\n", len(clusterList))
for key, value := range stateCount {
fmt.Printf("%s: %d\n", key, value)
}
}
return nil
}
示例3: listProjects
// Retrieves a list of projects, returns an error if one occurred
func listProjects(c *cli.Context, w io.Writer) error {
err := checkArgCount(c, 0)
if err != nil {
return err
}
tenantName := c.String("tenant")
client.Esxclient, err = client.GetClient(c)
if err != nil {
return err
}
tenant, err := verifyTenant(tenantName)
if err != nil {
return err
}
projects, err := client.Esxclient.Tenants.GetProjects(tenant.ID, nil)
if err != nil {
return err
}
if c.GlobalIsSet("non-interactive") {
for _, t := range projects.Items {
limits := quotaLineItemListToString(t.ResourceTicket.Limits)
usage := quotaLineItemListToString(t.ResourceTicket.Usage)
fmt.Printf("%s\t%s\t%s\t%s\n", t.ID, t.Name, limits, usage)
}
} else if utils.NeedsFormatting(c) {
utils.FormatObjects(projects.Items, w, c)
} else {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintf(w, "ID\tName\tLimit\tUsage\n")
for _, t := range projects.Items {
rt := t.ResourceTicket
for i := 0; i < len(rt.Limits); i++ {
if i == 0 {
fmt.Fprintf(w, "%s\t%s\t%s %g %s\t%s %g %s\n", t.ID, t.Name,
rt.Limits[i].Key, rt.Limits[i].Value, rt.Limits[i].Unit,
rt.Usage[i].Key, rt.Usage[i].Value, rt.Usage[i].Unit)
} else {
fmt.Fprintf(w, "\t\t%s %g %s\t%s %g %s\n",
rt.Limits[i].Key, rt.Limits[i].Value, rt.Limits[i].Unit,
rt.Usage[i].Key, rt.Usage[i].Value, rt.Usage[i].Unit)
}
}
for i := len(rt.Limits); i < len(rt.Usage); i++ {
fmt.Fprintf(w, "\t\t\t%s %g %s\n", rt.Usage[i].Key, rt.Usage[i].Value, rt.Usage[i].Unit)
}
}
err := w.Flush()
if err != nil {
return err
}
fmt.Printf("\nTotal projects: %d\n", len(projects.Items))
}
return nil
}
示例4: showProject
// Show project info with the specified project id, returns an error if one occurred
func showProject(c *cli.Context, w io.Writer) error {
err := checkArgCount(c, 1)
if err != nil {
return err
}
id := c.Args().First()
client.Esxclient, err = client.GetClient(c)
if err != nil {
return err
}
project, err := client.Esxclient.Projects.Get(id)
if err != nil {
return err
}
if c.GlobalIsSet("non-interactive") {
securityGroups := []string{}
for _, s := range project.SecurityGroups {
securityGroups = append(securityGroups, fmt.Sprintf("%s:%t", s.Name, s.Inherited))
}
scriptSecurityGroups := strings.Join(securityGroups, ",")
limits := quotaLineItemListToString(project.ResourceTicket.Limits)
usages := quotaLineItemListToString(project.ResourceTicket.Usage)
fmt.Printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n", project.ID, project.Name, project.ResourceTicket.TenantTicketID,
project.ResourceTicket.TenantTicketName, limits, usages, scriptSecurityGroups)
} else if utils.NeedsFormatting(c) {
utils.FormatObject(project, w, c)
} else {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintf(w, "Project ID: %s\n", project.ID)
fmt.Fprintf(w, " Name: %s\n", project.Name)
fmt.Fprintf(w, " TenantTicketID: %s\n", project.ResourceTicket.TenantTicketID)
fmt.Fprintf(w, " TenantTicketName: %s\n", project.ResourceTicket.TenantTicketName)
fmt.Fprintf(w, " Limits:\n")
for _, l := range project.ResourceTicket.Limits {
fmt.Fprintf(w, " %s\t%g\t%s\n", l.Key, l.Value, l.Unit)
}
fmt.Fprintf(w, " Usage:\n")
for _, u := range project.ResourceTicket.Usage {
fmt.Fprintf(w, " %s\t%g\t%s\n", u.Key, u.Value, u.Unit)
}
if len(project.SecurityGroups) != 0 {
fmt.Fprintf(w, " SecurityGroups:\n")
for _, s := range project.SecurityGroups {
fmt.Fprintf(w, " %s\t%t\n", s.Name, s.Inherited)
}
}
err = w.Flush()
if err != nil {
return err
}
}
return nil
}
示例5: writeColumns
func (c *CommandLine) writeColumns(response *client.Response, w io.Writer) {
for _, result := range response.Results {
// Create a tabbed writer for each result a they won't always line up
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 1, '\t', 0)
csv := c.formatResults(result, "\t")
for _, r := range csv {
fmt.Fprintln(w, r)
}
w.Flush()
}
}
示例6: showResourceTicket
// Sends a show resource-ticket task to client based on the cli.Context
// Returns an error if one occurred
func showResourceTicket(c *cli.Context, w io.Writer) error {
err := checkArgNum(c.Args(), 1, "resource-ticket show <name> [<options>]")
if err != nil {
return err
}
name := c.Args().First()
tenantName := c.String("tenant")
client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
if err != nil {
return err
}
tenant, err := verifyTenant(tenantName)
if err != nil {
return err
}
rt, err := findResourceTicket(tenant.ID, name)
if err != nil {
return err
}
if c.GlobalIsSet("non-interactive") {
usage := quotaLineItemListToString(rt.Usage)
limits := quotaLineItemListToString(rt.Limits)
fmt.Printf("%s\t%s\t%s\t%s\n", rt.Name, rt.ID, limits, usage)
} else if utils.NeedsFormatting(c) {
utils.FormatObject(rt, w, c)
} else {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintf(w, "ID\tName\tLimit\tUsage\n")
for i := 0; i < len(rt.Limits); i++ {
if i == 0 {
fmt.Fprintf(w, "%s\t%s\t%s %g %s\t%s %g %s\n", rt.ID, rt.Name,
rt.Limits[i].Key, rt.Limits[i].Value, rt.Limits[i].Unit,
rt.Usage[i].Key, rt.Usage[i].Value, rt.Usage[i].Unit)
} else {
fmt.Fprintf(w, "\t\t%s %g %s\t%s %g %s\n",
rt.Limits[i].Key, rt.Limits[i].Value, rt.Limits[i].Unit,
rt.Usage[i].Key, rt.Usage[i].Value, rt.Usage[i].Unit)
}
}
err := w.Flush()
if err != nil {
return err
}
}
return nil
}
示例7: printClusterVMs
func printClusterVMs(vms []photon.VM, w io.Writer, c *cli.Context) (err error) {
clusterVMs := []ClusterVM{}
for _, vm := range vms {
ipAddr := "-"
networks, err := getVMNetworks(vm.ID, c)
if err != nil {
continue
}
for _, nt := range networks {
network := nt.(map[string]interface{})
if val, ok := network["network"]; !ok || val == nil {
continue
}
if val, ok := network["ipAddress"]; ok && val != nil {
ipAddr = val.(string)
break
}
}
clusterVM := ClusterVM{
vm,
ipAddr,
}
clusterVMs = append(clusterVMs, clusterVM)
}
if c.GlobalIsSet("non-interactive") {
fmt.Printf("%d\n", len(vms))
for _, clusterVM := range clusterVMs {
fmt.Printf("%s\t%s\t%s\n", clusterVM.ClusterVM.ID,
clusterVM.ClusterVM.Name, clusterVM.IPAddress)
}
} else if utils.NeedsFormatting(c) {
utils.FormatObjects(clusterVMs, w, c)
} else {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintf(w, "VM ID\tVM Name\tVM IP\n")
for _, clusterVM := range clusterVMs {
fmt.Fprintf(w, "%s\t%s\t%s\n", clusterVM.ClusterVM.ID,
clusterVM.ClusterVM.Name, clusterVM.IPAddress)
}
err := w.Flush()
if err != nil {
return err
}
}
return nil
}
示例8: writeColumns
func (c *CommandLine) writeColumns(response *client.Response, w io.Writer) {
for _, result := range response.Results {
// Print out all messages first
for _, m := range result.Messages {
fmt.Fprintf(w, "%s: %s.\n", m.Level, m.Text)
}
// Create a tabbed writer for each result as they won't always line up
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 1, '\t', 0)
csv := c.formatResults(result, "\t")
for _, r := range csv {
fmt.Fprintln(w, r)
}
w.Flush()
}
}
示例9: listFlavors
// Retrieves a list of flavors
func listFlavors(c *cli.Context, w io.Writer) error {
err := checkArgNum(c.Args(), 0, "flavor list [<options>]")
if err != nil {
return err
}
client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
if err != nil {
return err
}
name := c.String("name")
kind := c.String("kind")
options := &photon.FlavorGetOptions{
Name: name,
Kind: kind,
}
flavors, err := client.Esxclient.Flavors.GetAll(options)
if err != nil {
return err
}
if c.GlobalIsSet("non-interactive") {
for _, flavor := range flavors.Items {
costs := quotaLineItemListToString(flavor.Cost)
fmt.Printf("%s\t%s\t%s\t%s\n", flavor.ID, flavor.Name, flavor.Kind, costs)
}
} else if utils.NeedsFormatting(c) {
utils.FormatObjects(flavors.Items, w, c)
} else {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintf(w, "ID\tName\tKind\tCost\n")
for _, flavor := range flavors.Items {
printQuotaList(w, flavor.Cost, flavor.ID, flavor.Name, flavor.Kind)
}
err = w.Flush()
if err != nil {
return err
}
fmt.Printf("Total: %d\n", len(flavors.Items))
}
return nil
}
示例10: listResourceTickets
// Retrieves a list of resource tickets, returns an error if one occurred
func listResourceTickets(c *cli.Context, w io.Writer) error {
err := checkArgNum(c.Args(), 0, "resource-ticket list [<options>]")
if err != nil {
return err
}
tenantName := c.String("tenant")
client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
if err != nil {
return err
}
tenant, err := verifyTenant(tenantName)
if err != nil {
return err
}
tickets, err := client.Esxclient.Tenants.GetResourceTickets(tenant.ID, nil)
if err != nil {
return err
}
if c.GlobalIsSet("non-interactive") {
for _, t := range tickets.Items {
limits := quotaLineItemListToString(t.Limits)
fmt.Printf("%s\t%s\t%s\n", t.ID, t.Name, limits)
}
} else if utils.NeedsFormatting(c) {
utils.FormatObjects(tickets.Items, w, c)
} else {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintf(w, "ID\tName\tLimit\n")
for _, t := range tickets.Items {
printQuotaList(w, t.Limits, t.ID, t.Name)
}
err := w.Flush()
if err != nil {
return err
}
fmt.Printf("\nTotal resource tickets: %d\n", len(tickets.Items))
}
return nil
}
示例11: listImages
// Lists all images
func listImages(c *cli.Context, w io.Writer) error {
err := checkArgCount(c, 0)
if err != nil {
return err
}
client.Esxclient, err = client.GetClient(c)
if err != nil {
return err
}
name := c.String("name")
options := &photon.ImageGetOptions{
Name: name,
}
images, err := client.Esxclient.Images.GetAll(options)
if err != nil {
return err
}
if c.GlobalIsSet("non-interactive") {
for _, image := range images.Items {
fmt.Printf("%s\t%s\t%s\t%d\t%s\t%s\t%s\n", image.ID, image.Name, image.State, image.Size,
image.ReplicationType, image.ReplicationProgress, image.SeedingProgress)
}
} else if utils.NeedsFormatting(c) {
utils.FormatObjects(images.Items, w, c)
} else {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintf(w, "ID\tName\tState\tSize(Byte)\tReplication_type\tReplicationProgress\tSeedingProgress\n")
for _, image := range images.Items {
fmt.Fprintf(w, "%s\t%s\t%s\t%d\t%s\t%s\t%s\n", image.ID, image.Name, image.State, image.Size,
image.ReplicationType, image.ReplicationProgress, image.SeedingProgress)
}
err = w.Flush()
if err != nil {
return err
}
fmt.Printf("\nTotal: %d\n", len(images.Items))
}
return nil
}
示例12: listPhysicalNetworks
func listPhysicalNetworks(c *cli.Context, w io.Writer) error {
err := checkArgCount(c, 0)
if err != nil {
return err
}
client.Esxclient, err = client.GetClient(c)
if err != nil {
return err
}
name := c.String("name")
options := &photon.SubnetGetOptions{
Name: name,
}
networks, err := client.Esxclient.Subnets.GetAll(options)
if err != nil {
return err
}
if c.GlobalIsSet("non-interactive") {
for _, network := range networks.Items {
fmt.Printf("%s\t%s\t%s\t%s\t%s\n", network.ID, network.Name, network.State, network.PortGroups, network.Description)
}
} else if utils.NeedsFormatting(c) {
utils.FormatObjects(networks.Items, w, c)
} else {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintf(w, "ID\tName\tState\tPortGroups\tDescription\tIsDefault\n")
for _, network := range networks.Items {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%t\n", network.ID, network.Name, network.State, network.PortGroups,
network.Description, network.IsDefault)
}
err = w.Flush()
if err != nil {
return err
}
fmt.Printf("Total: %d\n", len(networks.Items))
}
return nil
}
示例13: listDeployments
// Retrieves a list of deployments
func listDeployments(c *cli.Context, w io.Writer) error {
err := checkArgCount(c, 0)
if err != nil {
return err
}
client.Esxclient, err = client.GetClient(c)
if err != nil {
return err
}
deployments, err := client.Esxclient.Deployments.GetAll()
if err != nil {
return err
}
if utils.NeedsFormatting(c) {
utils.FormatObjects(deployments, w, c)
} else if c.GlobalIsSet("non-interactive") {
for _, deployment := range deployments.Items {
fmt.Printf("%s\n", deployment.ID)
}
} else {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintf(w, "ID\n")
for _, deployment := range deployments.Items {
fmt.Fprintf(w, "%s\n", deployment.ID)
}
err = w.Flush()
if err != nil {
return err
}
fmt.Printf("\nTotal: %d\n", len(deployments.Items))
}
return nil
}
示例14: listTenants
// Retrieves a list of tenants, returns an error if one occurred
func listTenants(c *cli.Context, w io.Writer) error {
err := checkArgNum(c.Args(), 0, "tenant list")
if err != nil {
return err
}
client.Esxclient, err = client.GetClient(utils.IsNonInteractive(c))
if err != nil {
return err
}
tenants, err := client.Esxclient.Tenants.GetAll()
if err != nil {
return err
}
if c.GlobalIsSet("non-interactive") {
for _, tenant := range tenants.Items {
fmt.Printf("%s\t%s\n", tenant.ID, tenant.Name)
}
} else if utils.NeedsFormatting(c) {
utils.FormatObjects(tenants.Items, w, c)
} else {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintf(w, "ID\tName\n")
for _, tenant := range tenants.Items {
fmt.Fprintf(w, "%s\t%s\n", tenant.ID, tenant.Name)
}
err = w.Flush()
if err != nil {
return err
}
fmt.Printf("\nTotal: %d\n", len(tenants.Items))
}
return nil
}
示例15: listAvailabilityZones
// Retrieves a list of availability zones, returns an error if one occurred
func listAvailabilityZones(c *cli.Context, w io.Writer) error {
err := checkArgCount(c, 0)
if err != nil {
return err
}
client.Esxclient, err = client.GetClient(c)
if err != nil {
return err
}
zones, err := client.Esxclient.AvailabilityZones.GetAll()
if err != nil {
return err
}
if c.GlobalIsSet("non-interactive") {
for _, zone := range zones.Items {
fmt.Printf("%s\t%s\n", zone.ID, zone.Name)
}
} else if utils.NeedsFormatting(c) {
utils.FormatObjects(zones.Items, w, c)
} else {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintf(w, "ID\tName\n")
for _, zone := range zones.Items {
fmt.Fprintf(w, "%s\t%s\n", zone.ID, zone.Name)
}
err = w.Flush()
if err != nil {
return err
}
fmt.Printf("\nTotal: %d\n", len(zones.Items))
}
return nil
}