Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9027 from Luap99/podman-volume-exists
Podman volume exists
- Loading branch information
Showing
14 changed files
with
307 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package volumes | ||
|
||
import ( | ||
"github.com/containers/podman/v2/cmd/podman/common" | ||
"github.com/containers/podman/v2/cmd/podman/registry" | ||
"github.com/containers/podman/v2/pkg/domain/entities" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
volumeExistsDescription = `If the given volume exists, podman volume exists exits with 0, otherwise the exit code will be 1.` | ||
volumeExistsCommand = &cobra.Command{ | ||
Use: "exists VOLUME", | ||
Short: "volume exists", | ||
Long: volumeExistsDescription, | ||
RunE: volumeExists, | ||
Example: `podman volume exists myvol`, | ||
Args: cobra.ExactArgs(1), | ||
ValidArgsFunction: common.AutocompleteVolumes, | ||
} | ||
) | ||
|
||
func init() { | ||
registry.Commands = append(registry.Commands, registry.CliCommand{ | ||
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode}, | ||
Command: volumeExistsCommand, | ||
Parent: volumeCmd, | ||
}) | ||
} | ||
|
||
func volumeExists(cmd *cobra.Command, args []string) error { | ||
response, err := registry.ContainerEngine().VolumeExists(registry.GetContext(), args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
if !response.Value { | ||
registry.SetExitCode(1) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
% podman-volume-exists(1) | ||
|
||
## NAME | ||
podman\-volume\-exists - Check if the given volume exists | ||
|
||
## SYNOPSIS | ||
**podman volume exists** *volume* | ||
|
||
## DESCRIPTION | ||
**podman volume exists** checks if a volume exists. Podman will return an exit code | ||
of `0` when the volume is found. A `1` will be returned otherwise. An exit code of | ||
`125` indicates there was another issue. | ||
|
||
|
||
## OPTIONS | ||
|
||
#### **--help**, **-h** | ||
|
||
Print usage statement | ||
|
||
## EXAMPLE | ||
|
||
Check if a volume called `myvol` exists (the volume does actually exist). | ||
``` | ||
$ podman volume exists myvol | ||
$ echo $? | ||
0 | ||
$ | ||
``` | ||
|
||
Check if an volume called `mysql` exists (the volume does not actually exist). | ||
``` | ||
$ podman volume exists mysql | ||
$ echo $? | ||
1 | ||
$ | ||
``` | ||
|
||
## SEE ALSO | ||
podman(1), podman-volume-create(1), podman-volume-rm(1) | ||
|
||
## HISTORY | ||
January 2021, Originally compiled by Paul Holzinger `<paul.holzinger@web.de>` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package volumes | ||
|
||
import ( | ||
"net/url" | ||
"reflect" | ||
"strconv" | ||
"strings" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
/* | ||
This file is generated automatically by go generate. Do not edit. | ||
*/ | ||
|
||
// Changed | ||
func (o *ExistsOptions) Changed(fieldName string) bool { | ||
r := reflect.ValueOf(o) | ||
value := reflect.Indirect(r).FieldByName(fieldName) | ||
return !value.IsNil() | ||
} | ||
|
||
// ToParams | ||
func (o *ExistsOptions) ToParams() (url.Values, error) { | ||
params := url.Values{} | ||
if o == nil { | ||
return params, nil | ||
} | ||
json := jsoniter.ConfigCompatibleWithStandardLibrary | ||
s := reflect.ValueOf(o) | ||
if reflect.Ptr == s.Kind() { | ||
s = s.Elem() | ||
} | ||
sType := s.Type() | ||
for i := 0; i < s.NumField(); i++ { | ||
fieldName := sType.Field(i).Name | ||
if !o.Changed(fieldName) { | ||
continue | ||
} | ||
fieldName = strings.ToLower(fieldName) | ||
f := s.Field(i) | ||
if reflect.Ptr == f.Kind() { | ||
f = f.Elem() | ||
} | ||
switch f.Kind() { | ||
case reflect.Bool: | ||
params.Set(fieldName, strconv.FormatBool(f.Bool())) | ||
case reflect.String: | ||
params.Set(fieldName, f.String()) | ||
case reflect.Int, reflect.Int64: | ||
// f.Int() is always an int64 | ||
params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) | ||
case reflect.Uint, reflect.Uint64: | ||
// f.Uint() is always an uint64 | ||
params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) | ||
case reflect.Slice: | ||
typ := reflect.TypeOf(f.Interface()).Elem() | ||
switch typ.Kind() { | ||
case reflect.String: | ||
sl := f.Slice(0, f.Len()) | ||
s, ok := sl.Interface().([]string) | ||
if !ok { | ||
return nil, errors.New("failed to convert to string slice") | ||
} | ||
for _, val := range s { | ||
params.Add(fieldName, val) | ||
} | ||
default: | ||
return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) | ||
} | ||
case reflect.Map: | ||
lowerCaseKeys := make(map[string][]string) | ||
iter := f.MapRange() | ||
for iter.Next() { | ||
lowerCaseKeys[iter.Key().Interface().(string)] = iter.Value().Interface().([]string) | ||
|
||
} | ||
s, err := json.MarshalToString(lowerCaseKeys) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
params.Set(fieldName, s) | ||
} | ||
} | ||
return params, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package integration | ||
|
||
import ( | ||
"os" | ||
|
||
. "github.com/containers/podman/v2/test/utils" | ||
"github.com/containers/storage/pkg/stringid" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Podman volume exists", func() { | ||
var ( | ||
tempdir string | ||
err error | ||
podmanTest *PodmanTestIntegration | ||
) | ||
|
||
BeforeEach(func() { | ||
tempdir, err = CreateTempDirInTempDir() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
podmanTest = PodmanTestCreate(tempdir) | ||
podmanTest.Setup() | ||
podmanTest.SeedImages() | ||
}) | ||
|
||
AfterEach(func() { | ||
podmanTest.CleanupVolume() | ||
f := CurrentGinkgoTestDescription() | ||
processTestResult(f) | ||
|
||
}) | ||
|
||
It("podman volume exists", func() { | ||
vol := "vol" + stringid.GenerateNonCryptoID() | ||
session := podmanTest.Podman([]string{"volume", "create", vol}) | ||
session.WaitWithDefaultTimeout() | ||
Expect(session.ExitCode()).To(BeZero()) | ||
|
||
session = podmanTest.Podman([]string{"volume", "exists", vol}) | ||
session.WaitWithDefaultTimeout() | ||
Expect(session.ExitCode()).To(Equal(0)) | ||
|
||
session = podmanTest.Podman([]string{"volume", "exists", stringid.GenerateNonCryptoID()}) | ||
session.WaitWithDefaultTimeout() | ||
Expect(session.ExitCode()).To(Equal(1)) | ||
}) | ||
}) |