github.com/apache/incubator-kie-tools/packages/kn-plugin-workflow@v0.28.1-0.20240311201729-34c6856b157f/pkg/common/helper.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one
     3   * or more contributor license agreements.  See the NOTICE file
     4   * distributed with this work for additional information
     5   * regarding copyright ownership.  The ASF licenses this file
     6   * to you under the Apache License, Version 2.0 (the
     7   * "License"); you may not use this file except in compliance
     8   * with the License.  You may obtain a copy of the License at
     9   * 
    10   *  http://www.apache.org/licenses/LICENSE-2.0
    11   * 
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied.  See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License. 
    18   */
    19  
    20  package common
    21  
    22  import (
    23  	"bufio"
    24  	"fmt"
    25  	"github.com/spf13/cobra"
    26  	"html/template"
    27  	"os/exec"
    28  )
    29  
    30  func RunCommand(command *exec.Cmd, commandName string) error {
    31  	stdout, _ := command.StdoutPipe()
    32  	stderr, _ := command.StderrPipe()
    33  
    34  	if err := command.Start(); err != nil {
    35  		fmt.Printf("ERROR: starting command \"%s\" failed\n", commandName)
    36  		return err
    37  	}
    38  
    39  	stdoutScanner := bufio.NewScanner(stdout)
    40  	for stdoutScanner.Scan() {
    41  		m := stdoutScanner.Text()
    42  		fmt.Println(m)
    43  	}
    44  
    45  	stderrScanner := bufio.NewScanner(stderr)
    46  	for stderrScanner.Scan() {
    47  		m := stderrScanner.Text()
    48  		fmt.Println(m)
    49  	}
    50  
    51  	if err := command.Wait(); err != nil {
    52  		fmt.Printf("ERROR: something went wrong during command \"%s\"\n", commandName)
    53  		return err
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  func RunExtensionCommand(extensionCommand string, extensions string) error {
    60  	command := ExecCommand("mvn", extensionCommand, fmt.Sprintf("-Dextensions=%s", extensions))
    61  	if err := RunCommand(command, extensionCommand); err != nil {
    62  		fmt.Println("ERROR: It wasn't possible to add Quarkus extension in your pom.xml.")
    63  		return err
    64  	}
    65  	return nil
    66  }
    67  
    68  func GetTemplate(cmd *cobra.Command, name string) *template.Template {
    69  	var (
    70  		body = cmd.Long + "\n\n" + cmd.UsageString()
    71  		t    = template.New(name)
    72  		tpl  = template.Must(t.Parse(body))
    73  	)
    74  	return tpl
    75  }
    76  
    77  func DefaultTemplatedHelp(cmd *cobra.Command, args []string) {
    78  	tpl := GetTemplate(cmd, "help")
    79  	var data = struct{ Name string }{Name: cmd.Root().Use}
    80  
    81  	if err := tpl.Execute(cmd.OutOrStdout(), data); err != nil {
    82  		fmt.Fprintf(cmd.ErrOrStderr(), "unable to display help text: %v", err)
    83  	}
    84  }