X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=utils.go;h=b47cd04aafdb93d5d23517ef167f307532705bdc;hb=7d1bcb1c9a48514e0e26246b86c8597502833b4b;hp=6a05ef3e01547dc72d532545c6861f0e117af4e7;hpb=c35d7a0fc8bbb1f9123bb41a7b66e45ea2564dd2;p=src%2Fxds%2Fxds-cli.git diff --git a/utils.go b/utils.go index 6a05ef3..b47cd04 100644 --- a/utils.go +++ b/utils.go @@ -1,35 +1,75 @@ +/* + * 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" + "strconv" + "strings" - "github.com/iotbzh/xds-agent/lib/apiv1" + "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1" + "github.com/franciscocpg/reflectme" "github.com/urfave/cli" ) -var cacheXdsVersion *apiv1.XDSVersion +var cacheXdsVersion *xaapiv1.XDSVersion +var cacheData = xaapiv1.XDSVersion{} // XdsVersionGet Get version of XDS agent & server -func XdsVersionGet(ver *apiv1.XDSVersion) error { - // Use cached data - if cacheXdsVersion != nil { - ver = cacheXdsVersion - return nil +func XdsVersionGet(ver *xaapiv1.XDSVersion) error { + // 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) +} + +// XdsConfigSet Set XDS Agent config +func XdsConfigSet(cfg xaapiv1.APIConfig) error { + // clear cache + cacheXdsVersion = nil - dataVer := apiv1.XDSVersion{} - if err := HTTPCli.Get("/version", &dataVer); err != 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 } // XdsServerIDGet returns the XDS Server ID func XdsServerIDGet() string { - ver := apiv1.XDSVersion{} + ver := xaapiv1.XDSVersion{} if err := XdsVersionGet(&ver); err != nil { return "" } @@ -45,8 +85,13 @@ 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 +} + // ProjectsListGet Get the list of existing projects -func ProjectsListGet(prjs *[]apiv1.ProjectConfig) error { +func ProjectsListGet(prjs *[]xaapiv1.ProjectConfig) error { var data []byte if err := HTTPCli.HTTPGet("/projects", &data); err != nil { return err @@ -71,3 +116,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") +}