github.com/naphatkrit/deis@v1.12.3/client/cmd/utils.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/deis/deis/client/controller/client"
    10  	"github.com/deis/deis/client/pkg/git"
    11  )
    12  
    13  var defaultLimit = -1
    14  
    15  func progress() chan bool {
    16  	frames := []string{"...", "o..", ".o.", "..o"}
    17  	backspaces := strings.Repeat("\b", 3)
    18  	tick := time.Tick(400 * time.Millisecond)
    19  	quit := make(chan bool)
    20  	go func() {
    21  		for {
    22  			for _, frame := range frames {
    23  				fmt.Print(frame)
    24  				select {
    25  				case <-quit:
    26  					fmt.Print(backspaces)
    27  					close(quit)
    28  					return
    29  				case <-tick:
    30  					fmt.Print(backspaces)
    31  				}
    32  			}
    33  		}
    34  	}()
    35  	return quit
    36  }
    37  
    38  // Choose an ANSI color by converting a string to an int.
    39  func chooseColor(input string) string {
    40  	var sum uint8
    41  
    42  	for _, char := range []byte(input) {
    43  		sum += uint8(char)
    44  	}
    45  
    46  	// Seven possible terminal colors
    47  	color := (sum % 7) + 1
    48  
    49  	if color == 7 {
    50  		color = 9
    51  	}
    52  
    53  	return fmt.Sprintf("\033[3%dm", color)
    54  }
    55  
    56  func load(appID string) (*client.Client, string, error) {
    57  	c, err := client.New()
    58  
    59  	if err != nil {
    60  		return nil, "", err
    61  	}
    62  
    63  	if appID == "" {
    64  		appID, err = git.DetectAppName(c.ControllerURL.Host)
    65  
    66  		if err != nil {
    67  			return nil, "", err
    68  		}
    69  	}
    70  
    71  	return c, appID, nil
    72  }
    73  
    74  func drinkOfChoice() string {
    75  	drink := os.Getenv("DEIS_DRINK_OF_CHOICE")
    76  
    77  	if drink == "" {
    78  		drink = "coffee"
    79  	}
    80  
    81  	return drink
    82  }
    83  
    84  func limitCount(objs, total int) string {
    85  	if objs == total {
    86  		return "\n"
    87  	}
    88  
    89  	return fmt.Sprintf(" (%d of %d)\n", objs, total)
    90  }