github.com/drycc/workflow-cli@v1.5.3-0.20240322092846-d4ee25983af9/cmd/utils.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"sort"
     8  	"strings"
     9  	"time"
    10  
    11  	drycc "github.com/drycc/controller-sdk-go"
    12  	"github.com/drycc/workflow-cli/pkg/git"
    13  	"github.com/drycc/workflow-cli/settings"
    14  	"github.com/olekukonko/tablewriter"
    15  )
    16  
    17  var defaultLimit = -1
    18  
    19  func progress(wOut io.Writer) chan bool {
    20  	frames := []string{"...", "o..", ".o.", "..o"}
    21  	backspaces := strings.Repeat("\b", 3)
    22  	tick := time.NewTicker(400 * time.Millisecond)
    23  	quit := make(chan bool)
    24  	go func() {
    25  		for {
    26  			for _, frame := range frames {
    27  				fmt.Fprint(wOut, frame)
    28  				select {
    29  				case <-quit:
    30  					fmt.Fprint(wOut, backspaces)
    31  					close(quit)
    32  					return
    33  				case <-tick.C:
    34  					fmt.Fprint(wOut, backspaces)
    35  				}
    36  			}
    37  		}
    38  	}()
    39  	return quit
    40  }
    41  
    42  // load loads settings file and looks up the app name
    43  func load(cf string, appID string) (*settings.Settings, string, error) {
    44  	s, err := settings.Load(cf)
    45  
    46  	if err != nil {
    47  		return nil, "", err
    48  	}
    49  
    50  	if appID == "" {
    51  		appID, err = git.DetectAppName(git.DefaultCmd, s.Client.ControllerURL.Host)
    52  
    53  		if err != nil {
    54  			return nil, "", err
    55  		}
    56  	}
    57  
    58  	return s, appID, nil
    59  }
    60  
    61  func drinkOfChoice() string {
    62  	drink := os.Getenv("DRYCC_DRINK_OF_CHOICE")
    63  
    64  	if drink == "" {
    65  		drink = "coffee"
    66  	}
    67  
    68  	return drink
    69  }
    70  
    71  func limitCount(objs, total int) string {
    72  	if objs == total {
    73  		return "\n"
    74  	}
    75  
    76  	return fmt.Sprintf(" (%d of %d)\n", objs, total)
    77  }
    78  
    79  // checkAPICompatibility handles specific behavior for certain errors,
    80  // such as printing an warning for the API mismatch error
    81  func (d *DryccCmd) checkAPICompatibility(c *drycc.Client, err error) error {
    82  	if err == drycc.ErrAPIMismatch {
    83  		if !d.Warned {
    84  			d.PrintErrf(`!    WARNING: Client and server API versions do not match. Please consider upgrading.
    85  !    Client version: %s
    86  !    Server version: %s
    87  `, drycc.APIVersion, c.ControllerAPIVersion)
    88  			d.Warned = true
    89  		}
    90  
    91  		// API mismatch isn't fatal, so after warning continue on.
    92  		return nil
    93  	}
    94  
    95  	return err
    96  }
    97  
    98  // getDefaultFormatTable return default format ascii table
    99  func (d *DryccCmd) getDefaultFormatTable(headers []string) *tablewriter.Table {
   100  	table := tablewriter.NewWriter(d.WOut)
   101  	table.SetHeader(headers)
   102  	table.SetAlignment(tablewriter.ALIGN_LEFT)
   103  	table.SetAutoWrapText(false)
   104  	table.SetAutoFormatHeaders(true)
   105  	table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
   106  	table.SetAlignment(tablewriter.ALIGN_LEFT)
   107  	table.SetCenterSeparator("")
   108  	table.SetColumnSeparator("")
   109  	table.SetRowSeparator("")
   110  	table.SetHeaderLine(false)
   111  	table.SetBorder(false)
   112  	table.SetTablePadding(fmt.Sprintf("%4s", " "))
   113  	table.SetNoWhiteSpace(true)
   114  	return table
   115  }
   116  
   117  func sortKeys(data map[string]interface{}) *[]string {
   118  	keys := make([]string, 0, len(data))
   119  	for k := range data {
   120  		keys = append(keys, k)
   121  	}
   122  	sort.Strings(keys)
   123  	return &keys
   124  }
   125  
   126  func safeGetString(data string) string {
   127  	if data == "" {
   128  		return "<none>"
   129  	}
   130  	return data
   131  }