Example #1
0
// Handle is the function that handles all commands. It accepts a Commander as
// a parameter, which all commands implement.
func Handle(command Commander) {
	ctx := command.Context()
	ctx.ServiceClientType = command.ServiceClientType()
	ctx.Results = make(chan *Resource)

	resource := &Resource{
		Keys: command.Keys(),
	}

	err := ctx.CheckArgNum(0)
	if err != nil {
		resource.Err = err
		errExit1(command, resource)
	}

	err = ctx.handleGlobalOptions()
	if err != nil {
		resource.Err = err
		errExit1(command, resource)
	}

	client, err := auth.NewClient(ctx.CLIContext, ctx.ServiceClientType, ctx.logger, ctx.GlobalOptions.noCache)
	if err != nil {
		resource.Err = err
		errExit1(command, resource)
	}
	client.HTTPClient.Transport.(*auth.LogRoundTripper).Logger = ctx.logger
	ctx.ServiceClient = client

	err = command.HandleFlags(resource)
	if err != nil {
		resource.Err = err
		errExit1(command, resource)
	}

	go handleExecute(command, resource)

	for resource := range ctx.Results {
		processResult(command, resource)
		printResult(command, resource)
	}

	ctx.storeCredentials()
}
Example #2
0
File: handle.go Project: flazz/rack
// Handle is the function that handles all commands. It accepts a Commander as
// a parameter, which all commands implement.
func Handle(command Commander) {
	ctx := command.Context()
	ctx.ServiceClientType = command.ServiceClientType()
	ctx.WaitGroup = &sync.WaitGroup{}

	ctx.ListenAndReceive()

	resource := &Resource{
		Keys: command.Keys(),
	}

	err := ctx.CheckArgNum(0)
	if err != nil {
		resource.Err = err
		ctx.ErrExit1(resource)
	}

	client, err := auth.NewClient(ctx.CLIContext, ctx.ServiceClientType)
	if err != nil {
		resource.Err = err
		ctx.ErrExit1(resource)
	}
	ctx.ServiceClient = client

	err = command.HandleFlags(resource)
	if err != nil {
		resource.Err = err
		ctx.ErrExit1(resource)
	}

	// can the command accept input on STDIN?
	if pipeableCommand, ok := command.(PipeHandler); ok {
		// should we expect something on STDIN?
		if ctx.CLIContext.IsSet("stdin") {
			stdinField := ctx.CLIContext.String("stdin")
			// if so, does the given field accept pipeable input?
			if stdinField == pipeableCommand.StdinField() {
				// if so, does the given command and field accept streaming input?
				if streamPipeableCommand, ok := pipeableCommand.(StreamPipeHandler); ok {
					ctx.WaitGroup.Add(1)
					go func() {
						err := streamPipeableCommand.HandleStreamPipe(resource)
						if err != nil {
							resource.Err = fmt.Errorf("Error handling streamable, pipeable command: %s\n", err)
							ctx.Results <- resource
						} else {
							streamPipeableCommand.Execute(resource)
							ctx.Results <- resource
						}
					}()
				} else {
					scanner := bufio.NewScanner(os.Stdin)
					for scanner.Scan() {
						item := scanner.Text()
						ctx.WaitGroup.Add(1)
						go func() {
							err := pipeableCommand.HandlePipe(resource, item)
							if err != nil {
								resource.Err = fmt.Errorf("Error handling pipeable command on %s: %s\n", item, err)
								ctx.Results <- resource
							} else {
								pipeableCommand.Execute(resource)
								ctx.Results <- resource
							}
						}()
					}
					if scanner.Err() != nil {
						resource.Err = scanner.Err()
						ctx.ErrExit1(resource)
					}
				}
			} else {
				resource.Err = fmt.Errorf("Unknown STDIN field: %s\n", stdinField)
				ctx.ErrExit1(resource)
			}
		} else {
			ctx.WaitGroup.Add(1)
			go func() {
				err := pipeableCommand.HandleSingle(resource)
				if err != nil {
					resource.Err = err
					ctx.ErrExit1(resource)
				}
				command.Execute(resource)
				ctx.Results <- resource
			}()
		}
	} else {
		ctx.WaitGroup.Add(1)
		go func() {
			command.Execute(resource)
			ctx.Results <- resource
		}()
	}
	ctx.WaitGroup.Wait()
	ctx.StoreCredentials()
}