X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=src%2Fxds%2Fxds-cli.git;a=blobdiff_plain;f=utils.go;h=dc5cb57a29563893751abf45d4453f07a2bbc0aa;hp=f70d5da1f5ae651fddf2613b50f063d5cb403a7a;hb=3b5e82b55433fd49cfe0cd0349756e0c2e9a9c12;hpb=f95aa5f0e39b85198655e8399c161fc5e6cd8a59 diff --git a/utils.go b/utils.go index f70d5da..dc5cb57 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"); @@ -20,28 +20,52 @@ package main import ( "encoding/json" + "fmt" + "os" + "sort" + "strconv" + "strings" - "github.com/iotbzh/xds-agent/lib/xaapiv1" + "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1" + "github.com/franciscocpg/reflectme" "github.com/urfave/cli" ) var cacheXdsVersion *xaapiv1.XDSVersion +var cacheData = xaapiv1.XDSVersion{} // XdsVersionGet Get version of XDS agent & server func XdsVersionGet(ver *xaapiv1.XDSVersion) error { - // Use cached data - if cacheXdsVersion != nil { - ver = cacheXdsVersion - return nil + // Update cached data + if cacheXdsVersion == nil { + if err := HTTPCli.Get("/version", &cacheData); err != nil { + return err + } } + reflectme.Copy(&cacheData, ver) + return nil +} + +// XdsConfigGet Get current XDS Agent config +func XdsConfigGet(cfg *xaapiv1.APIConfig) error { + return HTTPCli.Get("/config", cfg) +} - dataVer := xaapiv1.XDSVersion{} - if err := HTTPCli.Get("/version", &dataVer); err != nil { +// XdsConfigSet Set XDS Agent config +func XdsConfigSet(cfg xaapiv1.APIConfig) error { + // clear cache + cacheXdsVersion = nil + + newCfg := xaapiv1.APIConfig{} + if err := HTTPCli.Post("/config", cfg, &newCfg); err != nil { return err } - cacheXdsVersion = &dataVer - *ver = dataVer + idx := XdsServerIndexGet() + if !newCfg.Servers[idx].Connected { + return fmt.Errorf("XDS server %s still not connected", cfg.Servers[idx].URL) + } + return nil } @@ -63,6 +87,18 @@ func XdsServerIndexGet() int { return 0 } +// XdsServerComputeURL computes the URL used to access to XDS Server API +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 @@ -71,7 +107,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 @@ -81,11 +123,42 @@ 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") +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 } + +// Confirm Return true when user answer 'y' or 'yes' to a question +func Confirm(question string) bool { + var answer string + fmt.Print(question) + fmt.Scanln(&answer) + 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) +}