Volume cli minor refactor

Signed-off-by: Guillaume Tardif <guillaume.tardif@docker.com>
This commit is contained in:
Guillaume Tardif 2020-09-08 09:44:26 +02:00
parent 08562b403e
commit dcbc1c3fb2
5 changed files with 88 additions and 44 deletions

View File

@ -35,5 +35,5 @@ func TestPrintList(t *testing.T) {
}
out := &bytes.Buffer{}
printList(out, secrets)
golden.Assert(t, out.String(), "secrets-out.golden")
golden.Assert(t, out.String(), "volumes-out.golden")
}

View File

@ -22,54 +22,11 @@ import (
"os"
"strings"
"text/tabwriter"
"github.com/docker/compose-cli/aci"
"github.com/spf13/cobra"
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/volumes"
)
// Command manage volumes
func Command() *cobra.Command {
cmd := &cobra.Command{
Use: "volume",
Short: "Manages volumes",
}
cmd.AddCommand(
createVolume(),
listVolume(),
)
return cmd
}
func createVolume() *cobra.Command {
opts := aci.VolumeCreateOptions{}
cmd := &cobra.Command{
Use: "create",
Short: "Creates an Azure file share to use as ACI volume.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := client.New(cmd.Context())
if err != nil {
return err
}
id, err := c.VolumeService().Create(cmd.Context(), opts)
if err != nil {
return err
}
fmt.Println(id)
return nil
},
}
cmd.Flags().StringVar(&opts.Account, "storage-account", "", "Storage account name")
cmd.Flags().StringVar(&opts.Fileshare, "fileshare", "", "Fileshare name")
return cmd
}
func listVolume() *cobra.Command {
cmd := &cobra.Command{
Use: "ls",

View File

@ -0,0 +1,22 @@
package volume
import (
"bytes"
"github.com/docker/compose-cli/api/volumes"
"gotest.tools/v3/golden"
"testing"
)
func TestPrintList(t *testing.T) {
secrets := []volumes.Volume{
{
ID: "volume@123",
Name: "123",
Description: "volume 123",
},
}
out := &bytes.Buffer{}
printList(out, secrets)
golden.Assert(t, out.String(), "volumes-out.golden")
}

View File

@ -0,0 +1,2 @@
ID NAME DESCRIPTION
volume@123 123 volume 123

63
cli/cmd/volume/volume.go Normal file
View File

@ -0,0 +1,63 @@
package volume
/*
Copyright 2020 Docker, Inc.
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.
*/
import (
"fmt"
"github.com/spf13/cobra"
"github.com/docker/compose-cli/aci"
"github.com/docker/compose-cli/api/client"
)
// Command manage volumes
func Command() *cobra.Command {
cmd := &cobra.Command{
Use: "volume",
Short: "Manages volumes",
}
cmd.AddCommand(
createVolume(),
listVolume(),
)
return cmd
}
func createVolume() *cobra.Command {
opts := aci.VolumeCreateOptions{}
cmd := &cobra.Command{
Use: "create",
Short: "Creates an Azure file share to use as ACI volume.",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := client.New(cmd.Context())
if err != nil {
return err
}
id, err := c.VolumeService().Create(cmd.Context(), opts)
if err != nil {
return err
}
fmt.Println(id)
return nil
},
}
cmd.Flags().StringVar(&opts.Account, "storage-account", "", "Storage account name")
cmd.Flags().StringVar(&opts.Fileshare, "fileshare", "", "Fileshare name")
return cmd
}