Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] govc host.info is always writing to os.stdout instead of the writer passed to Write #2995

Closed
markrexwinkel opened this issue Nov 16, 2022 · 1 comment · Fixed by #2996

Comments

@markrexwinkel
Copy link

Describe the bug

I am using the govc commands in my own cli. I basically repeating the steps done by cli.Run().
Now as I understand it, each implemented command has a Write() function that should write the output to the passed in writer.

govc host.info is always writing to os.stdout instead of the writer passed to Write().

I know it would be better to use govmomi directly but the govc commands where a quick way to implement the functionality I needed.

To Reproduce
Steps to reproduce the behavior:

main.go:

package main

import (
	"bytes"
	"context"
	"flag"
	"fmt"
	"io"
	"os"

	"github.com/vmware/govmomi/govc/cli"
	"github.com/vmware/govmomi/govc/flags"
	_ "github.com/vmware/govmomi/govc/host"
	"github.com/vmware/govmomi/vim25/types"
)

func main() {
	name := "host.info"
	cmd, ok := cli.Commands()[name]
	if !ok {
		panic(fmt.Sprintf("command not found %q", name))
	}

	fs := flag.NewFlagSet("", flag.ContinueOnError)
	fs.SetOutput(io.Discard)

	ctx := context.Background()

	if id := os.Getenv("GOVC_OPERATION_ID"); id != "" {
		ctx = context.WithValue(ctx, types.ID{}, id)
	}

	var buf bytes.Buffer

	outFlag, ctx := flags.NewOutputFlag(ctx)
	outFlag.Out = &buf

	cmd.Register(ctx, fs)

	defer func() {
		_ = clientLogout(ctx, cmd)
	}()

	if err := fs.Parse(os.Args[1:]); err != nil {
		panic(err)
	}

	if err := cmd.Process(ctx); err != nil {
		panic(err)
	}

	if err := cmd.Run(ctx, fs); err != nil {
		panic(err)
	}

	if buf.Len() == 0 {
		panic("Expected buffer length > 0")
	}
}

func clientLogout(ctx context.Context, cmd cli.Command) error {
	type logout interface {
		Logout(context.Context) error
	}

	if l, ok := cmd.(logout); ok {
		return l.Logout(ctx)
	}

	return nil
}

Make sure you set the GOVC_URL, GOVC_USERNAME and GOVC_PASSWORD environment variables.

Expected behavior
Output of the host.info command should be written to the writer passed in to the OutputFlag.

Affected version
govc 0.29.0

Screenshots/Debug Output

Output of the main.go described above is:

Name:              esxi.lab.internal
  Path:            /lab.internal/host/esxi.lab.internal/esxi.lab.internal
  Manufacturer:    Micro-Star International Co., Ltd.
  Logical CPUs:    8 CPUs @ 2904MHz
  Processor type:  Intel(R) Core(TM) i7-10700F CPU @ 2.90GHz
  CPU usage:       684 MHz (2.9%)
  Memory:          65453MB
  Memory usage:    21644 MB (33.1%)
  Boot time:       2022-11-02 14:00:30.47428 +0000 UTC
  State:           connected
panic: Expected buffer length > 0
@github-actions
Copy link
Contributor

Howdy 🖐   markrexwinkel ! Thank you for your interest in this project. We value your feedback and will respond soon.

If you want to contribute to this project, please make yourself familiar with the CONTRIBUTION guidelines.

markrexwinkel added a commit to markrexwinkel/govmomi that referenced this issue Nov 16, 2022
markrexwinkel added a commit to markrexwinkel/govmomi that referenced this issue Nov 16, 2022
@markrexwinkel markrexwinkel mentioned this issue Nov 16, 2022
11 tasks
priyanka19-98 pushed a commit to priyanka19-98/govmomi that referenced this issue Jan 17, 2024
priyanka19-98 pushed a commit to priyanka19-98/govmomi that referenced this issue Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant