/*
- * Copyright (C) 2017 "IoT.bzh"
+ * Copyright (C) 2017-2018 "IoT.bzh"
* Author Sebastien Douheret <sebastien@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
import (
"encoding/json"
"fmt"
+ "os"
+ "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"
)
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
}
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
}
// GetID Return a string ID set with --id option or as simple parameter
-func GetID(ctx *cli.Context) string {
- id := ctx.String("id")
+func GetID(ctx *cli.Context, idEnvVarName string) string {
+ return GetIDName(ctx, "id", idEnvVarName)
+}
+
+// GetIDName Return a string ID set with --XXX option or as simple parameter
+func GetIDName(ctx *cli.Context, idName, idEnvVarName string) string {
+ if idName == "" {
+ return ""
+ }
+
+ // Get id set using option --id
+ id := ctx.String(idName)
+
+ // Check if id has been set using env variable
+ envSdkid, _ := os.LookupEnv(idEnvVarName)
+
+ // Support as 1st arg without --id option string (for example xds-cli sdk install 123456)
idArgs := ctx.Args().First()
- if id == "" && idArgs != "" {
+
+ // Set or overwrite id using first arg (knowing that 1st is overwrite env variable)
+ if (id == "" && idArgs != "") || (envSdkid != "" && idArgs != "") {
id = idArgs
}
return id
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)
+}