Skip to content

Commit b128ae6

Browse files
committed
Split code across packages
1 parent b5b3a97 commit b128ae6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+600
-485
lines changed

api.go renamed to api/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package api
22

33
import (
44
"bytes"
@@ -452,7 +452,7 @@ type ScalewayImageDefinition struct {
452452
Arch string `json:"arch"`
453453
}
454454

455-
var funcMap = template.FuncMap{
455+
var FuncMap = template.FuncMap{
456456
"json": func(v interface{}) string {
457457
a, _ := json.Marshal(v)
458458
return string(a)

cache.go renamed to api/cache.go

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package api
22

33
import (
44
"encoding/json"
@@ -9,6 +9,8 @@ import (
99
"strings"
1010
"sync"
1111

12+
"github.com/scaleway/scaleway-cli/utils"
13+
1214
"code.google.com/p/go-uuid/uuid"
1315
)
1416

@@ -160,7 +162,7 @@ func (c *ScalewayCache) LookUpImages(needle string, acceptUUID bool) []string {
160162
return exactMatches
161163
}
162164

163-
return RemoveDuplicates(res)
165+
return utils.RemoveDuplicates(res)
164166
}
165167

166168
// LookUpSnapshots attempts to return identifiers matching a pattern
@@ -190,7 +192,7 @@ func (c *ScalewayCache) LookUpSnapshots(needle string, acceptUUID bool) []string
190192
return exactMatches
191193
}
192194

193-
return RemoveDuplicates(res)
195+
return utils.RemoveDuplicates(res)
194196
}
195197

196198
// LookUpVolumes attempts to return identifiers matching a pattern
@@ -219,7 +221,7 @@ func (c *ScalewayCache) LookUpVolumes(needle string, acceptUUID bool) []string {
219221
return exactMatches
220222
}
221223

222-
return RemoveDuplicates(res)
224+
return utils.RemoveDuplicates(res)
223225
}
224226

225227
// LookUpBootscripts attempts to return identifiers matching a pattern
@@ -248,7 +250,7 @@ func (c *ScalewayCache) LookUpBootscripts(needle string, acceptUUID bool) []stri
248250
return exactMatches
249251
}
250252

251-
return RemoveDuplicates(res)
253+
return utils.RemoveDuplicates(res)
252254
}
253255

254256
// LookUpServers attempts to return identifiers matching a pattern
@@ -277,7 +279,7 @@ func (c *ScalewayCache) LookUpServers(needle string, acceptUUID bool) []string {
277279
return exactMatches
278280
}
279281

280-
return RemoveDuplicates(res)
282+
return utils.RemoveDuplicates(res)
281283
}
282284

283285
// LookUpIdentifiers attempts to return identifiers matching a pattern
@@ -322,23 +324,6 @@ func (c *ScalewayCache) LookUpIdentifiers(needle string) []ScalewayIdentifier {
322324
return results
323325
}
324326

325-
// RemoveDuplicates transforms an array into a unique array
326-
func RemoveDuplicates(elements []string) []string {
327-
encountered := map[string]bool{}
328-
329-
// Create a map of all unique elements.
330-
for v := range elements {
331-
encountered[elements[v]] = true
332-
}
333-
334-
// Place all keys from the map into a slice.
335-
result := []string{}
336-
for key := range encountered {
337-
result = append(result, key)
338-
}
339-
return result
340-
}
341-
342327
// InsertServer registers a server in the cache
343328
func (c *ScalewayCache) InsertServer(identifier, name string) {
344329
c.Lock.Lock()

api_helpers.go renamed to api/helpers.go

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package api
22

33
import (
44
"fmt"
@@ -10,8 +10,29 @@ import (
1010
log "github.com/Sirupsen/logrus"
1111
"github.com/docker/docker/pkg/namesgenerator"
1212
"github.com/dustin/go-humanize"
13+
"github.com/scaleway/scaleway-cli/utils"
1314
)
1415

16+
// ScalewayResolvedIdentifier represents a list of matching identifier for a specifier pattern
17+
type ScalewayResolvedIdentifier struct {
18+
// Identifiers holds matching identifiers
19+
Identifiers []ScalewayIdentifier
20+
21+
// Needle is the criteria used to lookup identifiers
22+
Needle string
23+
}
24+
25+
// ScalewayImageInterface is an interface to multiple Scaleway items
26+
type ScalewayImageInterface struct {
27+
CreationDate time.Time
28+
Identifier string
29+
Name string
30+
Tag string
31+
VirtualSize float64
32+
Public bool
33+
Type string
34+
}
35+
1536
// CreateVolumeFromHumanSize creates a volume on the API with a human readable size
1637
func CreateVolumeFromHumanSize(api *ScalewayAPI, size string) (*string, error) {
1738
bytes, err := humanize.ParseBytes(size)
@@ -32,15 +53,6 @@ func CreateVolumeFromHumanSize(api *ScalewayAPI, size string) (*string, error) {
3253
return &volumeID, nil
3354
}
3455

35-
// ScalewayResolvedIdentifier represents a list of matching identifier for a specifier pattern
36-
type ScalewayResolvedIdentifier struct {
37-
// Identifiers holds matching identifiers
38-
Identifiers []ScalewayIdentifier
39-
40-
// Needle is the criteria used to lookup identifiers
41-
Needle string
42-
}
43-
4456
// fillIdentifierCache fills the cache by fetching fro the API
4557
func fillIdentifierCache(api *ScalewayAPI) {
4658
log.Debugf("Filling the cache")
@@ -69,9 +81,9 @@ func fillIdentifierCache(api *ScalewayAPI) {
6981
wg.Wait()
7082
}
7183

72-
// getIdentifier returns a an identifier if the resolved needles only match one element, else, it exists the program
73-
func getIdentifier(api *ScalewayAPI, needle string) *ScalewayIdentifier {
74-
idents := resolveIdentifier(api, needle)
84+
// GetIdentifier returns a an identifier if the resolved needles only match one element, else, it exists the program
85+
func GetIdentifier(api *ScalewayAPI, needle string) *ScalewayIdentifier {
86+
idents := ResolveIdentifier(api, needle)
7587

7688
if len(idents) == 1 {
7789
return &idents[0]
@@ -88,8 +100,8 @@ func getIdentifier(api *ScalewayAPI, needle string) *ScalewayIdentifier {
88100
return nil
89101
}
90102

91-
// resolveIdentifier resolves needle provided by the user
92-
func resolveIdentifier(api *ScalewayAPI, needle string) []ScalewayIdentifier {
103+
// ResolveIdentifier resolves needle provided by the user
104+
func ResolveIdentifier(api *ScalewayAPI, needle string) []ScalewayIdentifier {
93105
idents := api.Cache.LookUpIdentifiers(needle)
94106
if len(idents) > 0 {
95107
return idents
@@ -101,8 +113,8 @@ func resolveIdentifier(api *ScalewayAPI, needle string) []ScalewayIdentifier {
101113
return idents
102114
}
103115

104-
// resolveIdentifiers resolves needles provided by the user
105-
func resolveIdentifiers(api *ScalewayAPI, needles []string, out chan ScalewayResolvedIdentifier) {
116+
// ResolveIdentifiers resolves needles provided by the user
117+
func ResolveIdentifiers(api *ScalewayAPI, needles []string, out chan ScalewayResolvedIdentifier) {
106118
// first attempt, only lookup from the cache
107119
var unresolved []string
108120
for _, needle := range needles {
@@ -131,8 +143,8 @@ func resolveIdentifiers(api *ScalewayAPI, needles []string, out chan ScalewayRes
131143
close(out)
132144
}
133145

134-
// inspectIdentifiers inspects identifiers concurrently
135-
func inspectIdentifiers(api *ScalewayAPI, ci chan ScalewayResolvedIdentifier, cj chan interface{}) {
146+
// InspectIdentifiers inspects identifiers concurrently
147+
func InspectIdentifiers(api *ScalewayAPI, ci chan ScalewayResolvedIdentifier, cj chan interface{}) {
136148
var wg sync.WaitGroup
137149
for {
138150
idents, ok := <-ci
@@ -187,7 +199,7 @@ func inspectIdentifiers(api *ScalewayAPI, ci chan ScalewayResolvedIdentifier, cj
187199
close(cj)
188200
}
189201

190-
func createServer(api *ScalewayAPI, imageName string, name string, bootscript string, env string, additionalVolumes string) (string, error) {
202+
func CreateServer(api *ScalewayAPI, imageName string, name string, bootscript string, env string, additionalVolumes string) (string, error) {
191203
if name == "" {
192204
name = strings.Replace(namesgenerator.GetRandomName(0), "_", "-", -1)
193205
}
@@ -268,33 +280,22 @@ func WaitForServerReady(api *ScalewayAPI, serverID string) (*ScalewayServer, err
268280

269281
dest := fmt.Sprintf("%s:22", server.PublicAddress.IP)
270282

271-
err = WaitForTCPPortOpen(dest)
283+
err = utils.WaitForTCPPortOpen(dest)
272284
if err != nil {
273285
return nil, err
274286
}
275287

276288
return server, nil
277289
}
278290

279-
// ScalewayImageInterface is an interface to multiple Scaleway items
280-
type ScalewayImageInterface struct {
281-
CreationDate time.Time
282-
Identifier string
283-
Name string
284-
Tag string
285-
VirtualSize float64
286-
Public bool
287-
Type string
288-
}
289-
290291
// ByCreationDate sorts images by CreationDate field
291292
type ByCreationDate []ScalewayImageInterface
292293

293294
func (a ByCreationDate) Len() int { return len(a) }
294295
func (a ByCreationDate) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
295296
func (a ByCreationDate) Less(i, j int) bool { return a[j].CreationDate.Before(a[i].CreationDate) }
296297

297-
func startServer(api *ScalewayAPI, needle string, wait bool) error {
298+
func StartServer(api *ScalewayAPI, needle string, wait bool) error {
298299
server := api.GetServerID(needle)
299300

300301
err := api.PostServerAction(server, "poweron")
@@ -313,8 +314,8 @@ func startServer(api *ScalewayAPI, needle string, wait bool) error {
313314
return nil
314315
}
315316

316-
func startServerOnce(api *ScalewayAPI, needle string, wait bool, successChan chan bool, errChan chan error) {
317-
err := startServer(api, needle, wait)
317+
func StartServerOnce(api *ScalewayAPI, needle string, wait bool, successChan chan bool, errChan chan error) {
318+
err := StartServer(api, needle, wait)
318319

319320
if err != nil {
320321
errChan <- err

api/types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package api
2+
3+
// Config is a Scaleway CLI configuration file
4+
type Config struct {
5+
// APIEndpoint is the endpoint to the Scaleway API
6+
APIEndPoint string `json:"api_endpoint"`
7+
8+
// Organization is the identifier of the Scaleway orgnization
9+
Organization string `json:"organization"`
10+
11+
// Token is the authentication token for the Scaleway organization
12+
Token string `json:"token"`
13+
}

0 commit comments

Comments
 (0)