Skip to content

Commit

Permalink
Merge pull request #495 from navidys/secret_create_rm
Browse files Browse the repository at this point in the history
podman secret create and remove command
  • Loading branch information
navidys authored Jun 10, 2024
2 parents d123afe + 15e842e commit 7a9f261
Show file tree
Hide file tree
Showing 6 changed files with 609 additions and 17 deletions.
98 changes: 98 additions & 0 deletions pdcs/secrets/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package secrets

import (
"errors"
"io"
"os"
"strings"

"github.com/containers/podman-tui/pdcs/registry"
"github.com/containers/podman/v5/pkg/bindings/secrets"
"github.com/rs/zerolog/log"
)

var (
errSecretInvalidLabelFormat = errors.New("invalid label format")
errSecretInvalidDriverOptionFormat = errors.New("invalid driver option format")
)

// SecretCreateOptions secret create options.
type SecretCreateOptions struct {
Name string
Replace bool
File string
Text string
Labels []string
Driver string
DriverOptions []string
}

// Create creates a new secret.
func Create(opts *SecretCreateOptions) error { //nolint:cyclop
log.Debug().Msgf("pdcs: podman secret create %v", opts)

var reader io.Reader

createOpts := new(secrets.CreateOptions)
createOpts = createOpts.WithReplace(opts.Replace)
createOpts = createOpts.WithName(opts.Name)
createOpts = createOpts.WithDriver(opts.Driver)
labels := make(map[string]string)

for _, label := range opts.Labels {
if label == "" {
continue
}

key, value, _ := strings.Cut(label, "=")
if key == "" {
return errSecretInvalidLabelFormat
}

labels[key] = value
}

createOpts.WithLabels(labels)

driverOptions := make(map[string]string)

for _, driverOpt := range opts.DriverOptions {
if driverOpt == "" {
continue
}

key, value, _ := strings.Cut(driverOpt, "=")
if key == "" {
return errSecretInvalidDriverOptionFormat
}

driverOptions[key] = value
}

createOpts.WithDriverOpts(driverOptions)

if opts.File != "" {
file, err := os.Open(opts.File)
if err != nil {
return err
}

defer file.Close()
reader = file
}

if opts.Text != "" {
reader = strings.NewReader(opts.Text)
}

conn, err := registry.GetConnection()
if err != nil {
return err
}

if _, err := secrets.Create(conn, reader, createOpts); err != nil {
return err
}

return nil
}
37 changes: 34 additions & 3 deletions ui/secrets/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

func (s *Secrets) runCommand(cmd string) {
switch cmd {
case "create":
s.createDialog.Display()
case "inspect":
s.inspect()
case "rm":
Expand All @@ -26,8 +28,32 @@ func (s *Secrets) displayError(title string, err error) {
s.errorDialog.Display()
}

func (s *Secrets) create() {
createOpts := s.createDialog.GetCreateOptions()

if createOpts.File != "" && createOpts.Text != "" {
s.displayError("SECRET CREATE ERROR", errSecretFileAndText)

return
}

if createOpts.File == "" && createOpts.Text == "" {
s.displayError("SECRET CREATE ERROR", errEmptySecretFileOrText)

return
}

if err := secrets.Create(createOpts); err != nil {
s.displayError("SECRET CREATE ERROR", err)

return
}

s.UpdateData()
}

func (s *Secrets) inspect() {
secID, secName := s.getSelectedItem()
_, secID, secName := s.getSelectedItem()
if secID == "" {
s.displayError("", errNoSecretInspect)

Expand All @@ -50,7 +76,7 @@ func (s *Secrets) inspect() {
}

func (s *Secrets) rm() {
secID, secName := s.getSelectedItem()
_, secID, secName := s.getSelectedItem()
if secID == "" {
s.displayError("", errNoSecretRemove)

Expand All @@ -70,7 +96,7 @@ func (s *Secrets) rm() {
}

func (s *Secrets) remove() {
secID, _ := s.getSelectedItem()
rowIndex, secID, _ := s.getSelectedItem()
if secID == "" {
s.displayError("", errNoSecretRemove)

Expand All @@ -92,6 +118,11 @@ func (s *Secrets) remove() {
return
}

rowIndex--
if rowIndex > 0 {
s.table.Select(rowIndex, 0)
}

s.UpdateData()
}

Expand Down
6 changes: 6 additions & 0 deletions ui/secrets/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,10 @@ func (s *Secrets) Draw(screen tcell.Screen) {

return
}

// create dialog
if s.createDialog.IsDisplay() {
s.createDialog.SetRect(x, y, width, height)
s.createDialog.Draw(screen)
}
}
7 changes: 7 additions & 0 deletions ui/secrets/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ func (s *Secrets) InputHandler() func(event *tcell.EventKey, setFocus func(p tvi
}
}

// create dialog
if s.createDialog.HasFocus() {
if createHandler := s.createDialog.InputHandler(); createHandler != nil {
createHandler(event, setFocus)
}
}

// table handlers
if s.table.HasFocus() { //nolint:nestif
if event.Rune() == utils.CommandMenuKey.Rune() {
Expand Down
Loading

0 comments on commit 7a9f261

Please sign in to comment.