Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add _cs #222

Merged
merged 2 commits into from
Oct 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
60 changes: 60 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,26 @@ type ScalewayTokensDefinition struct {
Token ScalewayTokenDefinition `json:"token"`
}

type ScalewayContainerData struct {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported type ScalewayContainerData should have comment or be unexported

LastModified string `json:"last_modified"`
Name string `json:"name"`
Size string `json:"size"`
}

type ScalewayGetContainerDatas struct {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported type ScalewayGetContainerDatas should have comment or be unexported

Container []ScalewayContainerData `json:"container"`
}

type ScalewayContainer struct {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported type ScalewayContainer should have comment or be unexported

ScalewayOrganizationDefinition `json:"organization"`
Name string `json:"name"`
Size string `json:"size"`
}

type ScalewayGetContainers struct {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported type ScalewayGetContainers should have comment or be unexported

Containers []ScalewayContainer `json:"containers"`
}

// ScalewayConnectResponse represents the answer from POST /tokens
type ScalewayConnectResponse struct {
Token ScalewayTokenDefinition `json:"token"`
Expand Down Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ var Commands = []*Command{
cmdPatch,
cmdSecurityGroups,
cmdIPS,
cmdCS,
}
49 changes: 49 additions & 0 deletions pkg/cli/x_cs.go
Original file line number Diff line number Diff line change
@@ -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
}