diff --git a/README.md b/README.md index 61903e46f4..9425569ebd 100644 --- a/README.md +++ b/README.md @@ -1131,6 +1131,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address' ### master (unreleased) +* Added _cs ([#180](https://github.com/scaleway/scaleway-cli/issues/180)) * Added SCALEWAY_VERBOSE_API to make the API more verbose * Support of 'scw _ips' command ... ([#196](https://github.com/scaleway/scaleway-cli/issues/196)) * Report **permissions** in `scw info` ([#191](https://github.com/scaleway/scaleway-cli/issues/191)) diff --git a/pkg/api/api.go b/pkg/api/api.go index 325b3c2177..19a73e2d46 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -631,6 +631,26 @@ type ScalewayTokensDefinition struct { Token ScalewayTokenDefinition `json:"token"` } +type ScalewayContainerData struct { + LastModified string `json:"last_modified"` + Name string `json:"name"` + Size string `json:"size"` +} + +type ScalewayGetContainerDatas struct { + Container []ScalewayContainerData `json:"container"` +} + +type ScalewayContainer struct { + ScalewayOrganizationDefinition `json:"organization"` + Name string `json:"name"` + Size string `json:"size"` +} + +type ScalewayGetContainers struct { + Containers []ScalewayContainer `json:"containers"` +} + // ScalewayConnectResponse represents the answer from POST /tokens type ScalewayConnectResponse struct { Token ScalewayTokenDefinition `json:"token"` @@ -1861,6 +1881,46 @@ func (s *ScalewayAPI) GetASecurityGroup(groupsID string) (*ScalewayGetSecurityGr return &securityGroups, nil } +// GetContainers returns a ScalewayGetContainers +func (s *ScalewayAPI) GetContainers() (*ScalewayGetContainers, error) { + resp, err := s.GetResponse("containers") + if err != nil { + return nil, err + } + defer resp.Body.Close() + + body, err := s.handleHTTPError([]int{200}, resp) + if err != nil { + return nil, err + } + var containers ScalewayGetContainers + + if err = json.Unmarshal(body, &containers); err != nil { + return nil, err + } + return &containers, nil +} + +// GetContainerDatas returns a ScalewayGetContainerDatas +func (s *ScalewayAPI) GetContainerDatas(container string) (*ScalewayGetContainerDatas, error) { + resp, err := s.GetResponse(fmt.Sprintf("containers/%s", container)) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + body, err := s.handleHTTPError([]int{200}, resp) + if err != nil { + return nil, err + } + var datas ScalewayGetContainerDatas + + if err = json.Unmarshal(body, &datas); err != nil { + return nil, err + } + return &datas, nil +} + // GetIPS returns a ScalewayGetIPS func (s *ScalewayAPI) GetIPS() (*ScalewayGetIPS, error) { resp, err := s.GetResponse("ips") diff --git a/pkg/cli/commands.go b/pkg/cli/commands.go index 8e15b9b6f2..0f91365a35 100644 --- a/pkg/cli/commands.go +++ b/pkg/cli/commands.go @@ -47,4 +47,5 @@ var Commands = []*Command{ cmdPatch, cmdSecurityGroups, cmdIPS, + cmdCS, } diff --git a/pkg/cli/x_cs.go b/pkg/cli/x_cs.go new file mode 100644 index 0000000000..ce0a3a16cc --- /dev/null +++ b/pkg/cli/x_cs.go @@ -0,0 +1,49 @@ +// Copyright (C) 2015 Scaleway. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE.md file. + +package cli + +import "fmt" + +var cmdCS = &Command{ + Exec: runCS, + UsageLine: "_cs [CONTAINER_NAME]", + Description: "", + Hidden: true, + Help: "List containers / datas", + Examples: ` + $ scw _cs + $ scw _cs containerName +`, +} + +func init() { + cmdCS.Flag.BoolVar(&csHelp, []string{"h", "-help"}, false, "Print usage") +} + +// Flags +var csHelp bool // -h, --help flag + +func runCS(cmd *Command, args []string) error { + if csHelp { + return cmd.PrintUsage() + } + if len(args) > 1 { + return cmd.PrintShortUsage() + } + if len(args) == 0 { + containers, err := cmd.API.GetContainers() + if err != nil { + return fmt.Errorf("Unable to get your containers: %v", err) + } + printRawMode(cmd.Streams().Stdout, *containers) + return nil + } + datas, err := cmd.API.GetContainerDatas(args[0]) + if err != nil { + return fmt.Errorf("Unable to get your data from %s: %v", args[1], err) + } + printRawMode(cmd.Streams().Stdout, *datas) + return nil +}