<commit_before>package commands

import (
	"os"
	"sort"
	"strconv"
	"time"

	"github.com/concourse/fly/rc"
	"github.com/concourse/fly/ui"
	"github.com/dgrijalva/jwt-go"
	"github.com/fatih/color"
)

type TargetsCommand struct{}

func (command *TargetsCommand) Execute([]string) error {
	flyYAML, err := rc.LoadTargets()
	if err != nil {
		return err
	}

	table := ui.Table{
		Headers: ui.TableRow{
			{Contents: "name", Color: color.New(color.Bold)},
			{Contents: "url", Color: color.New(color.Bold)},
			{Contents: "expiry", Color: color.New(color.Bold)},
		},
	}

	for targetName, targetValues := range flyYAML.Targets {
		expirationTime := GetExpirationFromString(targetValues.Token)

		row := ui.TableRow{
			{Contents: string(targetName)},
			{Contents: targetValues.API},
			{Contents: expirationTime},
		}

		table.Data = append(table.Data, row)
	}

	sort.Sort(table.Data)

	return table.Render(os.Stdout)
}

func GetExpirationFromString(token *rc.TargetToken) string {
	if token == nil {
		return "n/a"
	}

	parsedToken, _ := jwt.Parse(token.Value, func(token *jwt.Token) (interface{}, error) {
		return "", nil
	})

	expClaim, ok := parsedToken.Claims["exp"]
	if !ok {
		return "n/a"
	}

	intSeconds, err := strconv.ParseInt(string(expClaim.(string)), 10, 64)
	if err != nil {
		return "n/a"
	}

	unixSeconds := time.Unix(intSeconds, 0)

	return unixSeconds.Format(time.RFC1123)
}<commit_msg>support empty token struct and unquoted expiration<commit_after>package commands

import (
	"os"
	"sort"
	"strconv"
	"time"

	"github.com/concourse/fly/rc"
	"github.com/concourse/fly/ui"
	"github.com/dgrijalva/jwt-go"
	"github.com/fatih/color"
)

type TargetsCommand struct{}

func (command *TargetsCommand) Execute([]string) error {
	flyYAML, err := rc.LoadTargets()
	if err != nil {
		return err
	}

	table := ui.Table{
		Headers: ui.TableRow{
			{Contents: "name", Color: color.New(color.Bold)},
			{Contents: "url", Color: color.New(color.Bold)},
			{Contents: "expiry", Color: color.New(color.Bold)},
		},
	}

	for targetName, targetValues := range flyYAML.Targets {
		expirationTime := GetExpirationFromString(targetValues.Token)

		row := ui.TableRow{
			{Contents: string(targetName)},
			{Contents: targetValues.API},
			{Contents: expirationTime},
		}

		table.Data = append(table.Data, row)
	}

	sort.Sort(table.Data)

	return table.Render(os.Stdout)
}

func GetExpirationFromString(token *rc.TargetToken) string {
	if token == nil || token.Type == "" || token.Value == "" {
		return "n/a"
	}

	parsedToken, _ := jwt.Parse(token.Value, func(token *jwt.Token) (interface{}, error) {
		return "", nil
	})

	expClaim, ok := parsedToken.Claims["exp"]
	if !ok {
		return "n/a"
	}

	var intSeconds int64

	floatSeconds, ok := expClaim.(float64)
	if ok {
		intSeconds = int64(floatSeconds)
	} else {
		stringSeconds, ok := expClaim.(string)
		if !ok {
			return "n/a"
		}
		var err error
		intSeconds, err = strconv.ParseInt(stringSeconds, 10, 64)
		if err != nil {
			return "n/a"
		}
	}

	unixSeconds := time.Unix(intSeconds, 0)

	return unixSeconds.Format(time.RFC1123)
}
{'fraction_non_alphanumeric': 0.15816823644040273, 'fraction_numerical': 0.007794738551477752, 'mean_word_length': 4.032679738562091, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7106793', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1143, 'n_words': 235}