func setupClient(clientPemFile string) (*client.Client, error) { pvtKey, err := shared.ReadPemKey(clientPemFile) if err != nil { return nil, err } return client.New(pvtKey), nil }
func execute(fireAndForget bool, onFeedback func(fb string), server, executor, clientPemPath, exe string, args ...string) error { pvtKey, err := shared.ReadPemKey(clientPemPath) if err != nil { return fmt.Errorf("Cannot read client pem file, error: %s", err.Error()) } c := client.New(pvtKey) session, err := c.RequestNewSession(server) if err != nil { return fmt.Errorf("Unable to create new session, error: %s", err.Error()) } onFeedback(fmt.Sprintf("Using session id: %d\n", session.SessionId())) workingDir := "" builder := getExecutorExecRequestBuilder(session, executor) resp, err := builder.Exe(exe).WorkingDir(workingDir).Args(args...).BuildAndDoRequest() if err != nil { return err } if fireAndForget { onFeedback("Using fire-and-forget mode so the command will continue in the background.") return nil } responseChannel, errChannel := resp.TextResponseChannel() allErrors := []error{} outerFor: for { select { case feedbackLine, ok := <-responseChannel: if !ok { break outerFor } onFeedback(feedbackLine) case errLine, ok := <-errChannel: if !ok { break outerFor } allErrors = append(allErrors, errLine) } } if len(allErrors) > 0 { errStrs := []string{} for _, e := range allErrors { errStrs = append(errStrs, e.Error()) } return fmt.Errorf("ERRORS WERE: %s", strings.Join(errStrs, "\\n")) } return nil }