aboutsummaryrefslogtreecommitdiffstats
path: root/utils.go
blob: afa0942071761e8bdcbe82250b8126058e05f42d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Copyright (C) 2017-2018 "IoT.bzh"
 * Author Sebastien Douheret <sebastien@iot.bzh>
 *
 * 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"

	"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 {
	// 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

	newCfg := xaapiv1.APIConfig{}
	if err := HTTPCli.Post("/config", cfg, &newCfg); err != nil {
		return err
	}

	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 := xaapiv1.XDSVersion{}
	if err := XdsVersionGet(&ver); err != nil {
		return ""
	}
	if len(ver.Server) < 1 {
		return ""
	}
	return ver.Server[XdsServerIndexGet()].ID
}

// XdsServerIndexGet returns the index number of XDS Server
func XdsServerIndexGet() int {
	// FIXME support multiple server
	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
	if err := HTTPCli.HTTPGet("/projects", &data); err != nil {
		return err
	}
	Log.Debugf("Result of /projects: %v", string(data[:]))

	if err := json.Unmarshal(data, &prjs); err != nil {
		return err
	}

	sort.Sort(_PrjByLabel(*prjs))

	return nil
}

// LogPost Helper to log a POST request
func LogPost(format string, data interface{}) {
	b, _ := json.Marshal(data)
	Log.Infof(format, string(b))
}

// GetID Return a string ID set with --id option or as simple parameter
func GetID(ctx *cli.Context) string {
	id := ctx.String("id")
	idArgs := ctx.Args().First()
	if id == "" && 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")
}