X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=utils.go;h=afa0942071761e8bdcbe82250b8126058e05f42d;hb=ad7380c0eeaa8165067a43734d925a1ccc9187e1;hp=b21fc19a36d5ba4d4a0d83154ad3a034a8738464;hpb=75416fe01aa98b53c57315a050fc58f0879a238d;p=src%2Fxds%2Fxds-cli.git diff --git a/utils.go b/utils.go index b21fc19..afa0942 100644 --- a/utils.go +++ b/utils.go @@ -1,29 +1,70 @@ +/* + * Copyright (C) 2017-2018 "IoT.bzh" + * Author Sebastien Douheret + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + package main import ( "encoding/json" + "fmt" + "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 } @@ -45,6 +86,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 @@ -53,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 @@ -71,3 +130,12 @@ func GetID(ctx *cli.Context) string { } 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") +}