Skip to content

Commit 98a4665

Browse files
committed
Merge pull request #222 from QuentinPerez/_cs
Add _cs
2 parents 417c35e + 7b0ad4e commit 98a4665

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
11311131

11321132
### master (unreleased)
11331133

1134+
* Added _cs ([#180](https://github.com/scaleway/scaleway-cli/issues/180))
11341135
* Added SCALEWAY_VERBOSE_API to make the API more verbose
11351136
* Support of 'scw _ips' command ... ([#196](https://github.com/scaleway/scaleway-cli/issues/196))
11361137
* Report **permissions** in `scw info` ([#191](https://github.com/scaleway/scaleway-cli/issues/191))

pkg/api/api.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,26 @@ type ScalewayTokensDefinition struct {
631631
Token ScalewayTokenDefinition `json:"token"`
632632
}
633633

634+
type ScalewayContainerData struct {
635+
LastModified string `json:"last_modified"`
636+
Name string `json:"name"`
637+
Size string `json:"size"`
638+
}
639+
640+
type ScalewayGetContainerDatas struct {
641+
Container []ScalewayContainerData `json:"container"`
642+
}
643+
644+
type ScalewayContainer struct {
645+
ScalewayOrganizationDefinition `json:"organization"`
646+
Name string `json:"name"`
647+
Size string `json:"size"`
648+
}
649+
650+
type ScalewayGetContainers struct {
651+
Containers []ScalewayContainer `json:"containers"`
652+
}
653+
634654
// ScalewayConnectResponse represents the answer from POST /tokens
635655
type ScalewayConnectResponse struct {
636656
Token ScalewayTokenDefinition `json:"token"`
@@ -1861,6 +1881,46 @@ func (s *ScalewayAPI) GetASecurityGroup(groupsID string) (*ScalewayGetSecurityGr
18611881
return &securityGroups, nil
18621882
}
18631883

1884+
// GetContainers returns a ScalewayGetContainers
1885+
func (s *ScalewayAPI) GetContainers() (*ScalewayGetContainers, error) {
1886+
resp, err := s.GetResponse("containers")
1887+
if err != nil {
1888+
return nil, err
1889+
}
1890+
defer resp.Body.Close()
1891+
1892+
body, err := s.handleHTTPError([]int{200}, resp)
1893+
if err != nil {
1894+
return nil, err
1895+
}
1896+
var containers ScalewayGetContainers
1897+
1898+
if err = json.Unmarshal(body, &containers); err != nil {
1899+
return nil, err
1900+
}
1901+
return &containers, nil
1902+
}
1903+
1904+
// GetContainerDatas returns a ScalewayGetContainerDatas
1905+
func (s *ScalewayAPI) GetContainerDatas(container string) (*ScalewayGetContainerDatas, error) {
1906+
resp, err := s.GetResponse(fmt.Sprintf("containers/%s", container))
1907+
if err != nil {
1908+
return nil, err
1909+
}
1910+
defer resp.Body.Close()
1911+
1912+
body, err := s.handleHTTPError([]int{200}, resp)
1913+
if err != nil {
1914+
return nil, err
1915+
}
1916+
var datas ScalewayGetContainerDatas
1917+
1918+
if err = json.Unmarshal(body, &datas); err != nil {
1919+
return nil, err
1920+
}
1921+
return &datas, nil
1922+
}
1923+
18641924
// GetIPS returns a ScalewayGetIPS
18651925
func (s *ScalewayAPI) GetIPS() (*ScalewayGetIPS, error) {
18661926
resp, err := s.GetResponse("ips")

pkg/cli/commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ var Commands = []*Command{
4747
cmdPatch,
4848
cmdSecurityGroups,
4949
cmdIPS,
50+
cmdCS,
5051
}

pkg/cli/x_cs.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (C) 2015 Scaleway. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE.md file.
4+
5+
package cli
6+
7+
import "fmt"
8+
9+
var cmdCS = &Command{
10+
Exec: runCS,
11+
UsageLine: "_cs [CONTAINER_NAME]",
12+
Description: "",
13+
Hidden: true,
14+
Help: "List containers / datas",
15+
Examples: `
16+
$ scw _cs
17+
$ scw _cs containerName
18+
`,
19+
}
20+
21+
func init() {
22+
cmdCS.Flag.BoolVar(&csHelp, []string{"h", "-help"}, false, "Print usage")
23+
}
24+
25+
// Flags
26+
var csHelp bool // -h, --help flag
27+
28+
func runCS(cmd *Command, args []string) error {
29+
if csHelp {
30+
return cmd.PrintUsage()
31+
}
32+
if len(args) > 1 {
33+
return cmd.PrintShortUsage()
34+
}
35+
if len(args) == 0 {
36+
containers, err := cmd.API.GetContainers()
37+
if err != nil {
38+
return fmt.Errorf("Unable to get your containers: %v", err)
39+
}
40+
printRawMode(cmd.Streams().Stdout, *containers)
41+
return nil
42+
}
43+
datas, err := cmd.API.GetContainerDatas(args[0])
44+
if err != nil {
45+
return fmt.Errorf("Unable to get your data from %s: %v", args[1], err)
46+
}
47+
printRawMode(cmd.Streams().Stdout, *datas)
48+
return nil
49+
}

0 commit comments

Comments
 (0)