Skip to content

Commit

Permalink
WIP: progress bar works for local and remote pkgs
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleGedd committed Mar 7, 2024
1 parent 71b5b94 commit 47efc1a
Show file tree
Hide file tree
Showing 7 changed files with 354 additions and 21 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ go 1.21.6
require (
github.com/AlecAivazis/survey/v2 v2.3.7
github.com/alecthomas/jsonschema v0.0.0-20220216202328-9eeeec9d044b
github.com/charmbracelet/bubbles v0.16.1
github.com/charmbracelet/bubbletea v0.25.0
github.com/charmbracelet/lipgloss v0.9.1
github.com/defenseunicorns/maru-runner v0.0.1
github.com/defenseunicorns/zarf v0.32.4
github.com/fsnotify/fsnotify v1.7.0
Expand Down Expand Up @@ -138,10 +141,7 @@ require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
github.com/charmbracelet/bubbles v0.16.1 // indirect
github.com/charmbracelet/bubbletea v0.25.0 // indirect
github.com/charmbracelet/harmonica v0.2.0 // indirect
github.com/charmbracelet/lipgloss v0.9.1 // indirect
github.com/chrismellard/docker-credential-acr-env v0.0.0-20230304212654-82a0ddb27589 // indirect
github.com/clbanning/mxj/v2 v2.7.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
Expand Down
24 changes: 21 additions & 3 deletions src/cmd/uds.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@
package cmd

import (
"bytes"
"os"
"path/filepath"
"strings"

"github.com/AlecAivazis/survey/v2"
tea "github.com/charmbracelet/bubbletea"
"github.com/defenseunicorns/uds-cli/src/config"
"github.com/defenseunicorns/uds-cli/src/config/lang"
"github.com/defenseunicorns/uds-cli/src/pkg/bundle"
"github.com/defenseunicorns/uds-cli/src/pkg/bundle/tui"
zarfConfig "github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/pkg/message"
zarfUtils "github.com/defenseunicorns/zarf/src/pkg/utils"
zarfTypes "github.com/defenseunicorns/zarf/src/types"
goyaml "github.com/goccy/go-yaml"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"golang.org/x/term"
)

var createCmd = &cobra.Command{
Expand Down Expand Up @@ -83,9 +88,22 @@ var deployCmd = &cobra.Command{
bndlClient := bundle.NewOrDie(&bundleCfg)
defer bndlClient.ClearPaths()

if err := bndlClient.Deploy(); err != nil {
bndlClient.ClearPaths()
message.Fatalf(err, "Failed to deploy bundle: %s", err.Error())
// disable pterm output (for now) and set default output to buffer
pterm.DisableOutput()
var ptermBuffer bytes.Buffer
pterm.SetDefaultOutput(&ptermBuffer)

// start up bubbletea
m := tui.InitModel("", bndlClient, &ptermBuffer, tui.DeployOp)
// detect tty
if term.IsTerminal(int(os.Stdout.Fd())) {
tui.Program = tea.NewProgram(&m)
} else {
tui.Program = tea.NewProgram(&m, tea.WithInput(nil))
}

if _, err := tui.Program.Run(); err != nil {
panic(err)
}
},
}
Expand Down
26 changes: 13 additions & 13 deletions src/pkg/bundle/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package bundle

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand All @@ -15,6 +16,7 @@ import (

"github.com/AlecAivazis/survey/v2"
"github.com/defenseunicorns/uds-cli/src/config"
"github.com/defenseunicorns/uds-cli/src/pkg/bundle/tui"
"github.com/defenseunicorns/uds-cli/src/pkg/sources"
"github.com/defenseunicorns/uds-cli/src/types"
zarfConfig "github.com/defenseunicorns/zarf/src/config"
Expand All @@ -35,14 +37,9 @@ type ZarfOverrideMap map[string]map[string]map[string]interface{}
var templatedVarRegex = regexp.MustCompile(`\${([^}]+)}`)

// Deploy deploys a bundle
func (b *Bundle) Deploy() error {
func (b *Bundle) Deploy(ptermBuf *bytes.Buffer) error {
ctx := context.TODO()

pterm.Println()
metadataSpinner := message.NewProgressSpinner("Loading bundle metadata")

defer metadataSpinner.Stop()

// Check that provided oci source path is valid, and update it if it's missing the full path
source, err := CheckOCISourcePath(b.cfg.DeployOpts.Source)
if err != nil {
Expand Down Expand Up @@ -79,8 +76,6 @@ func (b *Bundle) Deploy() error {
return err
}

metadataSpinner.Successf("Loaded bundle metadata")

// confirm deploy
if ok := b.confirmBundleDeploy(); !ok {
return fmt.Errorf("bundle deployment cancelled")
Expand Down Expand Up @@ -110,13 +105,13 @@ func (b *Bundle) Deploy() error {
if len(userSpecifiedPackages) != len(packagesToDeploy) {
return fmt.Errorf("invalid zarf packages specified by --packages")
}
return deployPackages(packagesToDeploy, resume, b, zarfPackageNameMap)
return deployPackages(packagesToDeploy, resume, b, ptermBuf, zarfPackageNameMap)
}

return deployPackages(b.bundle.Packages, resume, b, zarfPackageNameMap)
return deployPackages(b.bundle.Packages, resume, b, ptermBuf, zarfPackageNameMap)
}

func deployPackages(packages []types.Package, resume bool, b *Bundle, zarfPackageNameMap map[string]string) error {
func deployPackages(packages []types.Package, resume bool, b *Bundle, ptermBuf *bytes.Buffer, zarfPackageNameMap map[string]string) error {

Check warning on line 114 in src/pkg/bundle/deploy.go

View workflow job for this annotation

GitHub Actions / validate

parameter 'ptermBuf' seems to be unused, consider removing or renaming it as _
// map of Zarf pkgs and their vars
bundleExportedVars := make(map[string]map[string]string)

Expand Down Expand Up @@ -189,6 +184,13 @@ func deployPackages(packages []types.Package, resume bool, b *Bundle, zarfPackag
if err != nil {
return err
}

// enable output to start filling pterm buffer
pterm.DisableOutput()

// bubbletea recommends calling the Program directly; calling model.Update() doesn't work
// https://github.com/charmbracelet/bubbletea/discussions/374
tui.Program.Send(fmt.Sprintf("package:%s", pkg.Name))
if err := pkgClient.Deploy(); err != nil {
return err
}
Expand Down Expand Up @@ -271,8 +273,6 @@ func (b *Bundle) confirmBundleDeploy() (confirm bool) {
Message: "Deploy this bundle?",
}

pterm.Println()

if err := survey.AskOne(prompt, &confirm); err != nil || !confirm {
return false
}
Expand Down
Loading

0 comments on commit 47efc1a

Please sign in to comment.