Skip to content

Commit

Permalink
Merge pull request #3 from skilld-labs/keyring_error_handling
Browse files Browse the repository at this point in the history
Handle keyring error in case of wrong or empty passphrase
  • Loading branch information
davidferlay authored Feb 29, 2024
2 parents 057931c + 4169ee8 commit dfa7b94
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
44 changes: 30 additions & 14 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"path"
"path/filepath"

"github.com/launchrctl/launchr/pkg/cli"

"github.com/launchrctl/keyring"
"github.com/launchrctl/launchr"
"github.com/launchrctl/launchr/pkg/log"
Expand Down Expand Up @@ -91,7 +93,7 @@ func publish(k keyring.Keyring) error {
return fmt.Errorf("artifact %s not found in %s. Execute 'plasmactl platform:package' before", archiveFile, artifactDir)
}

fmt.Printf("Looking for artifact %s in %s\n", archiveFile, artifactDir)
cli.Println("Looking for artifact %s in %s", archiveFile, artifactDir)
file, err := os.Open(path.Clean(artifactPath))
if err != nil {
log.Debug("%s", err)
Expand All @@ -106,14 +108,14 @@ func publish(k keyring.Keyring) error {
return errors.New("error creating HTTP request")
}

fmt.Println("Getting credentials")
ci, err := getCredentials(artifactsRepositoryDomain, k)
cli.Println("Getting credentials")
ci, save, err := getCredentials(artifactsRepositoryDomain, k)
if err != nil {
return err
}
req.SetBasicAuth(ci.Username, ci.Password)

fmt.Printf("Publishing artifact %s/%s to %s...\n", artifactDir, archiveFile, artifactArchiveURL)
cli.Println("Publishing artifact %s/%s to %s...", artifactDir, archiveFile, artifactArchiveURL)
resp, err := client.Do(req)
if err != nil {
log.Debug("%s", err)
Expand All @@ -125,33 +127,47 @@ func publish(k keyring.Keyring) error {
return fmt.Errorf("failed to upload artifact: %s", resp.Status)
}

fmt.Println("Artifact successfully uploaded")
cli.Println("Artifact successfully uploaded")

defer func() {
if save {
err = k.Save()
if err != nil {
log.Err("Error during saving keyring file", err)
}
}
}()

return nil
}

func getCredentials(url string, k keyring.Keyring) (keyring.CredentialsItem, error) {
func getCredentials(url string, k keyring.Keyring) (keyring.CredentialsItem, bool, error) {
ci, err := k.GetForURL(url)
save := false
if err != nil {
if errors.Is(err, keyring.ErrEmptyPass) {
return ci, false, err
} else if !errors.Is(err, keyring.ErrNotFound) {
log.Debug("%s", err)
return ci, false, errors.New("the keyring is malformed or wrong passphrase provided")
}
ci = keyring.CredentialsItem{}
ci.URL = url
if ci.URL != "" {
fmt.Printf("Please add login and password for URL - %s\n", ci.URL)
cli.Println("Please add login and password for URL - %s", ci.URL)
}
err = keyring.RequestCredentialsFromTty(&ci)
if err != nil {
return ci, err
return ci, false, err
}

err = k.AddItem(ci)
if err != nil {
return ci, err
}
err = k.Save()
if err != nil {
return ci, err
return ci, false, err
}

save = true
}

return ci, nil
return ci, save, nil
}
6 changes: 4 additions & 2 deletions publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path/filepath"
"time"

"github.com/launchrctl/launchr/pkg/cli"

"github.com/go-git/go-git/v5"
)

Expand Down Expand Up @@ -41,7 +43,7 @@ func listFiles(dir string) error {
if err != nil {
return err
}
fmt.Printf("Listing files in %s:\n", dir)
cli.Println("Listing files in %s:", dir)
for _, file := range files {
if file.IsDir() {
continue
Expand All @@ -52,7 +54,7 @@ func listFiles(dir string) error {
return err
}
size := humanReadableSize(info.Size())
fmt.Printf("%s %10s %s %s\n", info.Mode(), size, info.ModTime().Format(time.Stamp), file.Name())
cli.Println("%s %10s %s %s", info.Mode(), size, info.ModTime().Format(time.Stamp), file.Name())
}

return nil
Expand Down

0 comments on commit dfa7b94

Please sign in to comment.