github.com/chenbh/concourse/v6@v6.4.2/fly/commands/targets.go (about)

     1  package commands
     2  
     3  import (
     4  	"os"
     5  	"sort"
     6  	"time"
     7  
     8  	"github.com/chenbh/concourse/v6/fly/rc"
     9  	"github.com/chenbh/concourse/v6/fly/ui"
    10  	"github.com/chenbh/concourse/v6/skymarshal/token"
    11  	"github.com/fatih/color"
    12  )
    13  
    14  type TargetsCommand struct{}
    15  
    16  func (command *TargetsCommand) Execute([]string) error {
    17  	targets, err := rc.LoadTargets()
    18  	if err != nil {
    19  		return err
    20  	}
    21  
    22  	table := ui.Table{
    23  		Headers: ui.TableRow{
    24  			{Contents: "name", Color: color.New(color.Bold)},
    25  			{Contents: "url", Color: color.New(color.Bold)},
    26  			{Contents: "team", Color: color.New(color.Bold)},
    27  			{Contents: "expiry", Color: color.New(color.Bold)},
    28  		},
    29  	}
    30  
    31  	for targetName, targetValues := range targets {
    32  		expirationTime := getExpirationFromString(targetValues.Token)
    33  
    34  		row := ui.TableRow{
    35  			{Contents: string(targetName)},
    36  			{Contents: targetValues.API},
    37  			{Contents: targetValues.TeamName},
    38  			{Contents: expirationTime},
    39  		}
    40  
    41  		table.Data = append(table.Data, row)
    42  	}
    43  
    44  	sort.Sort(table.Data)
    45  
    46  	return table.Render(os.Stdout, Fly.PrintTableHeaders)
    47  }
    48  
    49  func getExpirationFromString(ttoken *rc.TargetToken) string {
    50  	if ttoken == nil || ttoken.Type == "" || ttoken.Value == "" {
    51  		return "n/a"
    52  	}
    53  
    54  	expiry, err := token.Factory{}.ParseExpiry(ttoken.Value)
    55  	if err != nil {
    56  		return "n/a: invalid token"
    57  	}
    58  
    59  	return expiry.UTC().Format(time.RFC1123)
    60  }