Skip to content

Commit

Permalink
List targets grouped by version
Browse files Browse the repository at this point in the history
A little sloppy, but for a factory with multiple hwid targets, this
option will let you group them together by "version" (ie the CI build
that produced them). eg:

= 649
	raspberrypi3 / 6faeb4efdf3ae7be757b67b5df8774e7ce7be9fe1de3c77bdfa7db6f869ed3577a79df35d3ad3be9ce5af7d7fc7dbf3d
		tags:postmerge,promoted
	raspberrypi4-64 / 775d3defb736e9df1cf75d78dbbf357db6f7efbdb5e5bf5cebcd35d76f7b79f736e5bf7673dd3cf3c7797b8d7a7b4d7d
		tags:postmerge,promoted
	raspberrypi4 / 6f473ddb479ad35774dda75f75beddedeede75d73ae1e7da7f66fd735f3c69af3bddad38d5e71adf87fddb7f5bf3471e
		tags:postmerge,promoted
	beaglebone-yocto / 7b7f37eb4f3675de79d777f8ddc79c7fa79b7f86ddd74ddc6df69def8d757f6f7aefd7dbe3cd9b69be76f39d9f69def8
		tags:postmerge,promoted
	qemuarm64 / eb56f5d5a7387b9777d7775ad7aefb777737e367fc75d7db7b7e1d79ddb5d7bd1f7b97dd6fa75df1a7b6d36dddf7aeb7
		tags:postmerge,promoted
	raspberrypi3-64 / d35f77779f5d69c6fbf1ef1c6b4e9ed357767bcef97f9f5d6dd6bceb56dd77d7f4eb8ddfedb7dd79ed346fad9c775f37
		tags:postmerge,promoted
	intel-corei7-64 / ef5e75e3cd5ce5ae1d6dd7376f9ede6dfd3c77575af36e5aef7d1de9be1af3a6bae1ee74d377bdd1b7fb71d71d6da7dd
		tags:postmerge,promoted
	qemuriscv64 / e37dbad76f78f7de9dd75dddd1a69fdf77fc7dd6b8f76735e38ddedb96b6e7577be77e7cebdd5dd78d9d6faf7df7d6b9
		tags:postmerge,promoted
	apalis-imx6 / 6dcd9df7bf5fddaf3df5a6ba776dddf77df8e35e1d7fc7bb7bc6dfd7d6db73a7b9ef4db47de7f9df86de7fd75b7b9edf
		tags:postmerge,promoted
	cubox-i / e766baddef5fe7971f6fad1a6f6f1ae76d1ee5fd5f75d7b6df5d5bf5ce777f9f7ad34d9de9a71d6bae5cdded9e7dc79c
		tags:postmerge,promoted
	cl-som-imx7 / f5ce9bf1e6b4d1b77ce9ad5ed1e75e6bcf1c6b8d5d734ebb79c69be7d7b87f86bb7b9e1de787da6f5e37e1c73df7a7f6
		tags:postmerge,promoted

Signed-off-by: Andy Doan <[email protected]>
  • Loading branch information
doanac committed Sep 28, 2019
1 parent ba82fb1 commit 92df44b
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions cmd/targets_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import (
"encoding/hex"
"fmt"
"os"
"sort"
"strings"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
tuf "github.com/theupdateframework/notary/tuf/data"

"github.com/foundriesio/fioctl/client"
)

var targetListCmd = &cobra.Command{
Expand All @@ -21,15 +25,54 @@ var (
listRaw bool
listByTag string
listByHwId string
listByVer bool
)

type targetCustom struct {
target tuf.FileMeta
custom *client.TufCustom
}

func init() {
targetsCmd.AddCommand(targetListCmd)
targetListCmd.Flags().BoolVarP(&listRaw, "raw", "r", false, "Print raw targets.json")
targetListCmd.Flags().StringVarP(&listByHwId, "by-hwid", "", "", "Only list targets that match the given hardware-id")
targetListCmd.Flags().StringVarP(&listByTag, "by-tag", "", "", "Only list targets that match the given tag")
targetListCmd.Flags().BoolVarP(&listByVer, "by-version", "", false, "Group by \"versions\" so that each version shows the hwids and shas for it")
}

func printSorted(targets map[string][]targetCustom) {
keys := make([]string, 0, len(targets))
for k := range targets {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
fmt.Println("=", k)
for _, tc := range targets[k] {
hash := hex.EncodeToString(tc.target.Hashes["sha256"])
fmt.Printf("\t%s / %s\n", strings.Join(tc.custom.HardwareIds, ","), hash)
if len(tc.custom.Tags) > 0 {
fmt.Printf("\t\ttags:%s\n", strings.Join(tc.custom.Tags, ","))
}
if len(tc.custom.DockerApps) > 0 {
fmt.Printf("\t\tdocker-apps:")
idx := 0
for name, app := range tc.custom.DockerApps {
if idx > 0 {
fmt.Printf(",")
}
fmt.Printf("%s=%s", name, app.FileName)
idx += 1
}
fmt.Printf("\n")
}
}
}
}


func doTargetsList(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
logrus.Debugf("Listing targets for %s hwid(%s) tag(%s)", factory, listByHwId, listByTag)
Expand All @@ -50,18 +93,24 @@ func doTargetsList(cmd *cobra.Command, args []string) {
fmt.Println(err)
os.Exit(1)
}

verTargets := map[string][]targetCustom{}
for name, target := range targets.Signed.Targets {
custom, err := api.TargetCustom(target)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
continue
}
if len(listByHwId) > 0 && !intersectionInSlices([]string{listByHwId}, custom.HardwareIds) {
logrus.Debugf("Target(%r) does not match hwid)", target)
logrus.Debugf("Target(%v) does not match hwid)", target)
continue
}
if len(listByTag) > 0 && !intersectionInSlices([]string{listByTag}, custom.Tags) {
logrus.Debugf("Target(%r) does not include tag)", target)
logrus.Debugf("Target(%v) does not include tag)", target)
continue
}
if listByVer {
verTargets[custom.Version] = append(verTargets[custom.Version], targetCustom{target, custom})
continue
}
fmt.Println("=", name)
Expand All @@ -86,4 +135,7 @@ func doTargetsList(cmd *cobra.Command, args []string) {
fmt.Printf("\n")
}
}
if listByVer {
printSorted(verTargets)
}
}

0 comments on commit 92df44b

Please sign in to comment.