Skip to content

Commit

Permalink
Fix stdin block.
Browse files Browse the repository at this point in the history
  • Loading branch information
lexbritvin committed Oct 25, 2024
1 parent 0ad2bc3 commit 428fe0b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 36 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23.1
toolchain go1.23.2

require (
github.com/launchrctl/keyring v0.2.5
github.com/launchrctl/keyring v0.2.6
github.com/launchrctl/launchr v0.16.4
github.com/spf13/cobra v1.8.1
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3x
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/launchrctl/keyring v0.2.5 h1:NbGquLfWNwydL0+oHV2ltUj9Dj+W+2v5PeqFBHaIEKY=
github.com/launchrctl/keyring v0.2.5/go.mod h1:uBHC4ekh6hJikEA8YGlY1LDCi3iF3RBCvty0E4oZebA=
github.com/launchrctl/keyring v0.2.6 h1:R4MbNmlTw7M+Oz/ai/ikTPsbY3k+DVtnAvInYsO83PA=
github.com/launchrctl/keyring v0.2.6/go.mod h1:VIjbUHBeMRkbp9gmRWpyIiRzSU9cdiSPCXdJl1RItCk=
github.com/launchrctl/launchr v0.16.4 h1:uVbRLZZQuTGhjDBN8kLpdPGeZZkpBjiiSjCxDEmeM6g=
github.com/launchrctl/launchr v0.16.4/go.mod h1:EGMn1cGbaGEpo+eQyJVdlq9BO3OSIDP60BOPS2odDxo=
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
Expand Down
61 changes: 28 additions & 33 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (p *Plugin) OnAppInit(app launchr.App) error {
}

type metaOptions struct {
bin string
verboseCount int
keyringPassphrase string
override string
Expand All @@ -54,7 +55,10 @@ type metaOptions struct {

// CobraAddCommands implements launchr.CobraPlugin interface to provide meta functionality.
func (p *Plugin) CobraAddCommands(rootCmd *launchr.Command) error {
options := metaOptions{}
options := metaOptions{
// Retrieve current bin name from args to use in consequent commands.
bin: os.Args[0],
}

metaCmd := &launchr.Command{
Use: "meta [flags] environment tags",
Expand Down Expand Up @@ -118,11 +122,17 @@ func ensureKeyringPassphraseSet(cmd *launchr.Command, options *metaOptions) erro
return nil
}

func (p *Plugin) meta(environment, tags string, options metaOptions) error {
// Retrieve current binary name from args to use in consequent commands.
plasmaBinary := os.Args[0]
func (p *Plugin) createCommand(command string, args ...string) *exec.Cmd {
streams := p.app.Streams()
cmd := exec.Command(command, args...)
cmd.Stdout = streams.Out()
cmd.Stderr = streams.Err()
// Set directly because it's a file. Exec command has special treatment for [*os.File] input.
cmd.Stdin = os.Stdin
return cmd
}

func (p *Plugin) meta(environment, tags string, options metaOptions) error {
var commonArgs []string
verbosity := ""
if options.verboseCount > 0 {
Expand Down Expand Up @@ -232,18 +242,17 @@ func (p *Plugin) meta(environment, tags string, options metaOptions) error {
}
launchr.Term().Println("Checking keyring...")
keyringEntryName := "Artifacts repository"
err := validateCredentials(artifactsRepositoryDomain, plasmaBinary, p.k, keyringEntryName)
err := validateCredentials(artifactsRepositoryDomain, options.bin, p.k, keyringEntryName)
if err != nil {
return err
}

// Appending --keyring-passphrase to commands
keyringCmd := func(command string, args ...string) *exec.Cmd {
keyringCmd := func(cmd *exec.Cmd) *exec.Cmd {
if options.keyringPassphrase != "" {
args = append(args, "--keyring-passphrase", options.keyringPassphrase)
cmd.Args = append(cmd.Args, "--keyring-passphrase", options.keyringPassphrase)
}

return exec.Command(command, args...)
return cmd
}

// Commands executed sequentially
Expand All @@ -254,10 +263,7 @@ func (p *Plugin) meta(environment, tags string, options metaOptions) error {
bumpArgs = append(bumpArgs, "--last")
}
bumpArgs = append(bumpArgs, commonArgs...)
bumpCmd := exec.Command(plasmaBinary, bumpArgs...) //nolint G204
bumpCmd.Stdout = streams.Out()
bumpCmd.Stderr = streams.Err()
bumpCmd.Stdin = streams.In()
bumpCmd := keyringCmd(p.createCommand(options.bin, bumpArgs...))
launchr.Term().Println(sanitizeString(bumpCmd.String(), options.keyringPassphrase))
_ = bumpCmd.Run() //nolint
launchr.Term().Println()
Expand All @@ -270,10 +276,7 @@ func (p *Plugin) meta(environment, tags string, options metaOptions) error {
composeArgs = append(composeArgs, "--clean")
}
composeArgs = append(composeArgs, commonArgs...)
composeCmd := keyringCmd(plasmaBinary, composeArgs...)
composeCmd.Stdout = streams.Out()
composeCmd.Stderr = streams.Err()
composeCmd.Stdin = streams.In()
composeCmd := keyringCmd(p.createCommand(options.bin, composeArgs...))
launchr.Term().Println(sanitizeString(composeCmd.String(), options.keyringPassphrase))
composeErr := composeCmd.Run()
if composeErr != nil {
Expand All @@ -286,10 +289,7 @@ func (p *Plugin) meta(environment, tags string, options metaOptions) error {
bumpSyncArgs = append(bumpSyncArgs, "--override", options.override)
}
bumpSyncArgs = append(bumpSyncArgs, commonArgs...)
syncCmd := keyringCmd(plasmaBinary, bumpSyncArgs...)
syncCmd.Stdout = streams.Out()
syncCmd.Stderr = streams.Err()
syncCmd.Stdin = streams.In()
syncCmd := keyringCmd(p.createCommand(options.bin, bumpSyncArgs...))
launchr.Term().Println(sanitizeString(syncCmd.String(), options.keyringPassphrase))
syncErr := syncCmd.Run()
if syncErr != nil {
Expand All @@ -311,25 +311,23 @@ func (p *Plugin) meta(environment, tags string, options metaOptions) error {
wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
packageCmdArgs := []string{"package"}
packageCmdArgs = append(packageCmdArgs, commonArgs...)
packageCmd := exec.Command(plasmaBinary, packageCmdArgs...) //nolint G204
packageCmdArgs := append([]string{"package"}, commonArgs...)
packageCmd := p.createCommand(options.bin, packageCmdArgs...)
packageCmd.Stdout = &packageStdOut
packageCmd.Stderr = &packageStdErr
// publishCmd.Stdin = os.Stdin // Any interaction will prevent waitgroup to finish and thus stuck before print of stdout
packageCmd.Stdin = nil // Any interaction will prevent waitgroup to finish and thus stuck before print of stdout
// cli.Println(sanitizeString(packageCmd.String(), options.keyringPassphrase)) // TODO: Find a way to prevent it to fill deploy stdin
packageErr = packageCmd.Run()
if packageErr != nil {
publishErr = handleCmdErr(packageErr, "package error")
return
}

publishCmdArgs := []string{"publish"}
publishCmdArgs = append(publishCmdArgs, commonArgs...)
publishCmd := keyringCmd(plasmaBinary, publishCmdArgs...)
publishCmdArgs := append([]string{"publish"}, commonArgs...)
publishCmd := keyringCmd(p.createCommand(options.bin, publishCmdArgs...))
publishCmd.Stdout = &publishStdOut
publishCmd.Stderr = &publishStdErr
// publishCmd.Stdin = os.Stdin // Any interaction will prevent waitgroup to finish and thus stuck before print of stdout
publishCmd.Stdin = nil // Any interaction will prevent waitgroup to finish and thus stuck before print of stdout
// cli.Println(sanitizeString(publishCmd.String(), keyringPassphrase)) // TODO: Debug why it appears during deploy command stdout
publishErr = publishCmd.Run()
if publishErr != nil {
Expand All @@ -349,10 +347,7 @@ func (p *Plugin) meta(environment, tags string, options metaOptions) error {
deployCmdArgs = append(deployCmdArgs, "--debug")
}

deployCmd := keyringCmd(plasmaBinary, deployCmdArgs...)
deployCmd.Stdout = streams.Out()
deployCmd.Stderr = streams.Err()
deployCmd.Stdin = streams.In()
deployCmd := keyringCmd(p.createCommand(options.bin, deployCmdArgs...))
launchr.Term().Println(sanitizeString(deployCmd.String(), options.keyringPassphrase))
deployErr = deployCmd.Run()
if deployErr != nil {
Expand Down

0 comments on commit 428fe0b

Please sign in to comment.