Skip to content

Commit

Permalink
fix: empty config panic
Browse files Browse the repository at this point in the history
- replace depricated ioutils methods

Signed-off-by: Jae Aeich <jh4official@gmail.com>
  • Loading branch information
JaeAeich committed Feb 16, 2024
1 parent b95a9b2 commit 6f2fb76
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 3 additions & 5 deletions pkg/utils/credential_store.go
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
Expand Down Expand Up @@ -39,11 +38,10 @@ func checkAndUpdateCredentialName(credential *Credential) {

credential.Name = parsedUrl.Hostname() + "-" + credential.Username
log.Println("credential name not specified, storing the credential with the name as:", credential.Name)
return
}

func readCredentialStore() (CredentialStore, error) {
configInfo, err := ioutil.ReadFile(configFile)
configInfo, err := os.ReadFile(configFile)
if err != nil {
return CredentialStore{}, err
}
Expand Down Expand Up @@ -100,7 +98,7 @@ func StoreCredential(credential Credential, setAsCurrentCredential bool) error {
if err != nil {
return err
}
if err = ioutil.WriteFile(configFile, bytes, 0600); err != nil {
if err = os.WriteFile(configFile, bytes, 0600); err != nil {
return err
}
log.Println("Saving credentials to:", configFile)
Expand All @@ -114,7 +112,7 @@ func StoreCredential(credential Credential, setAsCurrentCredential bool) error {
func resolveCredential(credentialName string) (Credential, error) {
credentialStore, err := readCredentialStore()
if err != nil {
panic(fmt.Sprintf("failed to read credential store: %s", err))
return Credential{}, fmt.Errorf("failed to read credential store: %s", err)
}

// If credentialName is not specified, check environment variable
Expand Down
4 changes: 3 additions & 1 deletion pkg/utils/utils.go
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"encoding/json"
"fmt"
"os"

"github.com/goharbor/go-client/pkg/harbor"
v2client "github.com/goharbor/go-client/pkg/sdk/v2.0/client"
Expand All @@ -21,7 +22,8 @@ func GetClientByConfig(clientConfig *harbor.ClientSetConfig) *v2client.HarborAPI
func GetClientByCredentialName(credentialName string) *v2client.HarborAPI {
credential, err := resolveCredential(credentialName)
if err != nil {
panic(err)
fmt.Print(err)
os.Exit(1)
}
clientConfig := &harbor.ClientSetConfig{
URL: credential.ServerAddress,
Expand Down

0 comments on commit 6f2fb76

Please sign in to comment.