X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=utils.go;h=393ee2a21bb05d28108b4f2f0048adc4d034a670;hb=881633b639856a9575072d2f53bb3518acbf64a8;hp=3d01346f57481a0e4089a44044250e21cbcb3587;hpb=f2b2c900fcc8f02a5e532794772ed5359618c3cc;p=src%2Fxds%2Fxds-cli.git diff --git a/utils.go b/utils.go index 3d01346..393ee2a 100644 --- a/utils.go +++ b/utils.go @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017 "IoT.bzh" + * Copyright (C) 2017-2018 "IoT.bzh" * Author Sebastien Douheret * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,11 +21,12 @@ package main import ( "encoding/json" "fmt" + "sort" "strconv" "strings" + "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1" "github.com/franciscocpg/reflectme" - "github.com/iotbzh/xds-agent/lib/xaapiv1" "github.com/urfave/cli" ) @@ -90,6 +91,13 @@ func XdsServerComputeURL(endURL string) string { return "servers/" + strconv.Itoa(XdsServerIndexGet()) + endURL } +// Sort projects by Label +type _PrjByLabel []xaapiv1.ProjectConfig + +func (s _PrjByLabel) Len() int { return len(s) } +func (s _PrjByLabel) Swap(i, j int) { s[i], s[j] = s[j], s[i] } +func (s _PrjByLabel) Less(i, j int) bool { return s[i].Label < s[j].Label } + // ProjectsListGet Get the list of existing projects func ProjectsListGet(prjs *[]xaapiv1.ProjectConfig) error { var data []byte @@ -98,7 +106,13 @@ func ProjectsListGet(prjs *[]xaapiv1.ProjectConfig) error { } Log.Debugf("Result of /projects: %v", string(data[:])) - return json.Unmarshal(data, &prjs) + if err := json.Unmarshal(data, &prjs); err != nil { + return err + } + + sort.Sort(_PrjByLabel(*prjs)) + + return nil } // LogPost Helper to log a POST request @@ -109,7 +123,15 @@ func LogPost(format string, data interface{}) { // GetID Return a string ID set with --id option or as simple parameter func GetID(ctx *cli.Context) string { - id := ctx.String("id") + return GetIDName(ctx, "id") +} + +// GetIDName Return a string ID set with --XXX option or as simple parameter +func GetIDName(ctx *cli.Context, idName string) string { + if idName == "" { + return "" + } + id := ctx.String(idName) idArgs := ctx.Args().First() if id == "" && idArgs != "" { id = idArgs @@ -125,3 +147,8 @@ func Confirm(question string) bool { ans := strings.ToLower(strings.TrimSpace(answer)) return (ans == "y" || ans == "yes") } + +// compareID Compare an ID to a reference ID +func compareID(refID, ID string) bool { + return refID != "" && ID != "" && strings.HasPrefix(refID, ID) +}