-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andy Doan <andy@foundries.io>
- Loading branch information
Showing
2 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package targets | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/cheynewallace/tabby" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/foundriesio/fioctl/client" | ||
) | ||
|
||
func init() { | ||
cmd.AddCommand(&cobra.Command{ | ||
Use: "tests <target> [<test-id>]", | ||
Short: "Show testing done against a target", | ||
Run: doShowTests, | ||
Args: cobra.RangeArgs(1, 2), | ||
}) | ||
} | ||
|
||
func timestamp(ts float32) string { | ||
if ts == 0 { | ||
return "" | ||
} | ||
secs := int64(ts) | ||
nsecs := int64((ts - float32(secs)) * 1e9) | ||
return time.Unix(secs, nsecs).UTC().String() | ||
} | ||
|
||
func list(factory string, target int) { | ||
t := tabby.New() | ||
t.AddHeader("NAME", "STATUS", "ID", "CREATED AT", "DEVICE") | ||
|
||
var tl *client.TargetTestList | ||
for { | ||
var err error | ||
if tl == nil { | ||
tl, err = api.TargetTests(factory, target) | ||
} else { | ||
if tl.Next != nil { | ||
tl, err = api.TargetTestsCont(*tl.Next) | ||
} else { | ||
break | ||
} | ||
} | ||
if err != nil { | ||
fmt.Print("ERROR: ") | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
for _, test := range tl.Tests { | ||
created := timestamp(test.CreatedOn) | ||
t.AddLine(test.Name, test.Status, test.Id, created, test.DeviceUUID) | ||
} | ||
} | ||
t.Print() | ||
} | ||
|
||
func show(factory string, target int, testId string) { | ||
test, err := api.TargetTestResults(factory, target, testId) | ||
if err != nil { | ||
fmt.Print("ERROR: ") | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
fmt.Println("Name: ", test.Name) | ||
fmt.Println("Status: ", test.Status) | ||
fmt.Println("Created: ", timestamp(test.CreatedOn)) | ||
fmt.Println("Completed:", timestamp(test.CompletedOn)) | ||
fmt.Println("Device: ", test.DeviceUUID) | ||
if len(test.Details) > 0 { | ||
fmt.Println("Details:") | ||
fmt.Println(test.Details) | ||
} | ||
if test.Results != nil { | ||
fmt.Println("") | ||
t := tabby.New() | ||
t.AddHeader("TEST RESULT", "STATUS") | ||
for _, result := range test.Results { | ||
t.AddLine(result.Name, result.Status) | ||
} | ||
t.Print() | ||
} | ||
} | ||
|
||
func doShowTests(cmd *cobra.Command, args []string) { | ||
factory := viper.GetString("factory") | ||
target, err := strconv.Atoi(args[0]) | ||
if err != nil { | ||
fmt.Print("ERROR:") | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
if len(args) == 1 { | ||
logrus.Debugf("Showing target testing for %s %d", factory, target) | ||
list(factory, target) | ||
} else { | ||
testId := args[1] | ||
logrus.Debugf("Showing target test results for %s %d - %s", factory, target, testId) | ||
show(factory, target, testId) | ||
} | ||
} |