Skip to content

Commit

Permalink
Merge pull request #19 from skilld-labs/18-replace_hardcode_binary
Browse files Browse the repository at this point in the history
#18: Execute sub command using same binary instead of hardcoded alias
  • Loading branch information
davidferlay authored Aug 27, 2024
2 parents d9671cc + 7ddb31e commit e762610
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func init() {
}

var (
errAddCredentials = errors.New("execute 'plasmactl login --url=https://repositories.skilld.cloud' to add credentials to keyring")
tplAddCredentials = "execute '%s login --url=https://repositories.skilld.cloud' to add credentials to keyring" //nolint G101
)

// Plugin is launchr plugin providing meta action.
Expand Down Expand Up @@ -118,6 +118,9 @@ func ensureKeyringPassphraseSet(cmd *cobra.Command, options *metaOptions) error
}

func meta(environment, tags string, options metaOptions, k keyring.Keyring) error {
// Retrieve current binary name from args to use in consequent commands.
plasmaBinary := os.Args[0]

// Check if provided keyring pw is correct, since it will be used for multiple commands
// Check if publish command credentials are available in keyring and correct as stdin will not be available in goroutine
artifactsRepositoryDomain := "https://repositories.skilld.cloud"
Expand All @@ -126,7 +129,7 @@ func meta(environment, tags string, options metaOptions, k keyring.Keyring) erro
artifactsRepositoryDomain = "http://repositories.interaction.svc.skilld:8081"
}
cli.Println("Checking keyring...")
err := validateCredentials(artifactsRepositoryDomain, k)
err := validateCredentials(artifactsRepositoryDomain, plasmaBinary, k)
if err != nil {
return err
}
Expand Down Expand Up @@ -159,7 +162,7 @@ func meta(environment, tags string, options metaOptions, k keyring.Keyring) erro
bumpArgs = append(bumpArgs, "--last")
}
bumpArgs = append(bumpArgs, commonArgs...)
bumpCmd := exec.Command("plasmactl", bumpArgs...)
bumpCmd := exec.Command(plasmaBinary, bumpArgs...) //nolint G204
bumpCmd.Stdout = os.Stdout
bumpCmd.Stderr = os.Stderr
bumpCmd.Stdin = os.Stdin
Expand All @@ -172,7 +175,7 @@ func meta(environment, tags string, options metaOptions, k keyring.Keyring) erro
composeArgs = append(composeArgs, "--clean")
}
composeArgs = append(composeArgs, commonArgs...)
composeCmd := keyringCmd("plasmactl", composeArgs...)
composeCmd := keyringCmd(plasmaBinary, composeArgs...)
composeCmd.Stdout = os.Stdout
composeCmd.Stderr = os.Stderr
composeCmd.Stdin = os.Stdin
Expand All @@ -188,7 +191,7 @@ func meta(environment, tags string, options metaOptions, k keyring.Keyring) erro
bumpSyncArgs = append(bumpSyncArgs, "--override", options.override)
}
bumpSyncArgs = append(bumpSyncArgs, commonArgs...)
syncCmd := keyringCmd("plasmactl", bumpSyncArgs...)
syncCmd := keyringCmd(plasmaBinary, bumpSyncArgs...)
syncCmd.Stdout = os.Stdout
syncCmd.Stderr = os.Stderr
syncCmd.Stdin = os.Stdin
Expand All @@ -215,7 +218,7 @@ func meta(environment, tags string, options metaOptions, k keyring.Keyring) erro
defer wg.Done()
packageCmdArgs := []string{"package"}
packageCmdArgs = append(packageCmdArgs, commonArgs...)
packageCmd := exec.Command("plasmactl", packageCmdArgs...)
packageCmd := exec.Command(plasmaBinary, packageCmdArgs...) //nolint G204
packageCmd.Stdout = &packageStdOut
packageCmd.Stderr = &packageStdErr
//publishCmd.Stdin = os.Stdin // Any interaction will prevent waitgroup to finish and thus stuck before print of stdout
Expand All @@ -227,7 +230,7 @@ func meta(environment, tags string, options metaOptions, k keyring.Keyring) erro

publishCmdArgs := []string{"publish"}
publishCmdArgs = append(publishCmdArgs, commonArgs...)
publishCmd := keyringCmd("plasmactl", publishCmdArgs...)
publishCmd := keyringCmd(plasmaBinary, 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
Expand All @@ -248,7 +251,7 @@ func meta(environment, tags string, options metaOptions, k keyring.Keyring) erro
deployCmdArgs = append(deployCmdArgs, "--debug")
}

deployCmd := keyringCmd("plasmactl", deployCmdArgs...)
deployCmd := keyringCmd(plasmaBinary, deployCmdArgs...)
deployCmd.Stdout = os.Stdout
deployCmd.Stderr = os.Stderr
deployCmd.Stdin = os.Stdin
Expand Down Expand Up @@ -286,10 +289,10 @@ func handleCmdErr(cmdErr error) {
os.Exit(1)
}

func validateCredentials(url string, k keyring.Keyring) error {
func validateCredentials(url, plasmaBinary string, k keyring.Keyring) error {
if !k.Exists() {
cli.Println("Keyring doesn't exist")
return errAddCredentials
return fmt.Errorf(tplAddCredentials, plasmaBinary)
}

ci, err := k.GetForURL(url)
Expand All @@ -301,7 +304,7 @@ func validateCredentials(url string, k keyring.Keyring) error {
return err
} else if errors.Is(err, keyring.ErrNotFound) {
cli.Println("Keyring was unlocked successfully: publish credentials were not found")
return errAddCredentials
return fmt.Errorf(tplAddCredentials, plasmaBinary)
} else if !errors.Is(err, keyring.ErrNotFound) {
log.Debug("%s", err)
return errors.New("the keyring is malformed or wrong passphrase provided")
Expand Down

0 comments on commit e762610

Please sign in to comment.