mirror of https://github.com/dapr/cli.git
122 lines
2.4 KiB
Go
122 lines
2.4 KiB
Go
// ------------------------------------------------------------
|
|
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
// ------------------------------------------------------------
|
|
|
|
package utils
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"github.com/docker/docker/client"
|
|
"github.com/olekukonko/tablewriter"
|
|
)
|
|
|
|
// PrintTable to print in the table format
|
|
func PrintTable(csvContent string) {
|
|
table := tablewriter.NewWriter(os.Stdout)
|
|
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
|
|
table.SetBorder(false)
|
|
table.SetHeaderLine(false)
|
|
table.SetRowLine(false)
|
|
table.SetCenterSeparator("")
|
|
table.SetRowSeparator("")
|
|
table.SetColumnSeparator("")
|
|
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
|
scanner := bufio.NewScanner(strings.NewReader(csvContent))
|
|
header := true
|
|
|
|
for scanner.Scan() {
|
|
text := strings.Split(scanner.Text(), ",")
|
|
|
|
if header {
|
|
table.SetHeader(text)
|
|
header = false
|
|
} else {
|
|
table.Append(text)
|
|
}
|
|
}
|
|
|
|
table.Render()
|
|
}
|
|
|
|
func TruncateString(str string, maxLength int) string {
|
|
strLength := len(str)
|
|
if strLength <= maxLength {
|
|
return str
|
|
}
|
|
|
|
return str[0:maxLength-3] + "..."
|
|
}
|
|
|
|
func RunCmdAndWait(name string, args ...string) (string, error) {
|
|
cmd := exec.Command(name, args...)
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
stderr, err := cmd.StderrPipe()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
err = cmd.Start()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
resp, err := ioutil.ReadAll(stdout)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
errB, err := ioutil.ReadAll(stderr)
|
|
if err != nil {
|
|
return "", nil
|
|
}
|
|
|
|
err = cmd.Wait()
|
|
if err != nil {
|
|
// in case of error, capture the exact message
|
|
if len(errB) > 0 {
|
|
return "", errors.New(string(errB))
|
|
}
|
|
return "", err
|
|
}
|
|
|
|
return string(resp), nil
|
|
}
|
|
|
|
func CreateContainerName(serviceContainerName string, dockerNetwork string) string {
|
|
if dockerNetwork != "" {
|
|
return fmt.Sprintf("%s_%s", serviceContainerName, dockerNetwork)
|
|
}
|
|
|
|
return serviceContainerName
|
|
}
|
|
|
|
func CreateDirectory(dir string) error {
|
|
if _, err := os.Stat(dir); !os.IsNotExist(err) {
|
|
return nil
|
|
}
|
|
return os.Mkdir(dir, 0777)
|
|
}
|
|
|
|
// IsDockerInstalled checks whether docker is installed/running
|
|
func IsDockerInstalled() bool {
|
|
// nolint:staticcheck
|
|
cli, err := client.NewEnvClient()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
_, err = cli.Ping(context.Background())
|
|
return err == nil
|
|
}
|