Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove abrubt os.Exit calls #5087

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ func getRegistryDevfiles(registry Registry) (registryDevfiles []DevfileComponent
request := util.HTTPRequestParams{
URL: indexLink,
}
if registryUtil.IsSecure(registry.Name) {
secure, err := registryUtil.IsSecure(registry.Name)
if err != nil {
return nil, err
}
if secure {
token, err := keyring.Get(fmt.Sprintf("%s%s", util.CredentialPrefix, registry.Name), registryUtil.RegistryUser)
if err != nil {
return nil, errors.Wrap(err, "unable to get secure registry credential from keyring")
Expand Down
3 changes: 1 addition & 2 deletions pkg/devfile/adapters/kubernetes/component/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/openshift/odo/pkg/kclient"
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/occlient"
odoutil "github.com/openshift/odo/pkg/odo/util"
storagepkg "github.com/openshift/odo/pkg/storage"
storagelabels "github.com/openshift/odo/pkg/storage/labels"
"github.com/openshift/odo/pkg/sync"
Expand Down Expand Up @@ -265,7 +264,7 @@ func (a Adapter) Push(parameters common.PushParameters) (err error) {
parameters.EnvSpecificInfo.SetDevfileObj(a.Devfile)
err = component.ApplyConfig(&a.Client, config.LocalConfigInfo{}, parameters.EnvSpecificInfo, color.Output, componentExists, false)
if err != nil {
odoutil.LogErrorAndExit(err, "Failed to update config to component deployed.")
return errors.Wrapf(err, "Failed to update config to component deployed.")
}

// Compare the name of the pod with the one before the rollout. If they differ, it means there's a new pod and a force push is required
Expand Down
5 changes: 4 additions & 1 deletion pkg/odo/cli/component/common_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ func (o *commonLinkOptions) run() (err error) {
component = o.EnvSpecificInfo.GetName()
err = o.operation(o.secretName, component, o.Application)
} else {
component = o.Component()
component, err = o.Component()
if err != nil {
return err
}
err = o.operation(o.secretName, component, o.Application)
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/odo/cli/component/common_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/openshift/odo/pkg/kclient"
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/odo/genericclioptions"
odoutil "github.com/openshift/odo/pkg/odo/util"
"github.com/openshift/odo/pkg/project"
"github.com/openshift/odo/pkg/util"
"github.com/pkg/errors"
Expand Down Expand Up @@ -113,13 +112,13 @@ func (cpo *CommonPushOptions) createCmpIfNotExistsAndApplyCmpConfig(stdout io.Wr
cmpName,
err,
)
os.Exit(1)
return err
valaparthvi marked this conversation as resolved.
Show resolved Hide resolved
}
}
// Apply config
err := component.ApplyConfig(cpo.Context.Client, *cpo.LocalConfigInfo, envinfo.EnvSpecificInfo{}, stdout, cpo.doesComponentExist, true)
if err != nil {
odoutil.LogErrorAndExit(err, "Failed to update config to component deployed.")
return errors.Wrapf(err, "Failed to update config to component deployed.")
}

return nil
Expand Down
17 changes: 13 additions & 4 deletions pkg/odo/cli/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,28 @@ type ComponentOptions struct {

// Complete completes component options
func (co *ComponentOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
context := genericclioptions.GetContextFlagValue(cmd)
context, err := genericclioptions.GetContextFlagValue(cmd)
if err != nil {
return err
}

devfilePath := filepath.Join(context, devFile)
if util.CheckPathExists(devfilePath) {
co.Context, err = genericclioptions.NewDevfileContext(cmd)
} else {
co.Context, err = genericclioptions.NewContext(cmd)
}
if err != nil {
co.Context = genericclioptions.NewOfflineDevfileContext(cmd)
err = nil
co.Context, err = genericclioptions.NewOfflineDevfileContext(cmd)
if err != nil {
return err
}
}

co.componentName = co.Context.Component(args...)
co.componentName, err = co.Context.Component(args...)
if err != nil {
return nil
}
return
}

Expand Down
19 changes: 16 additions & 3 deletions pkg/odo/cli/component/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ func (co *CreateOptions) Complete(name string, cmd *cobra.Command, args []string
return err
}
} else {
co.Context = genericclioptions.NewOfflineDevfileContext(cmd)
co.Context, err = genericclioptions.NewOfflineDevfileContext(cmd)
if err != nil {
return err
}
}

// Configure the context
Expand Down Expand Up @@ -488,7 +491,13 @@ func (co *CreateOptions) devfileRun(cmd *cobra.Command) (err error) {
params = util.HTTPRequestParams{
URL: co.devfileMetadata.devfileRegistry.URL + co.devfileMetadata.devfileLink,
}
if registryUtil.IsSecure(co.devfileMetadata.devfileRegistry.Name) {

secure, err := registryUtil.IsSecure(co.devfileMetadata.devfileRegistry.Name)
if err != nil {
return err
}

if secure {
var token string
token, err = keyring.Get(fmt.Sprintf("%s%s", util.CredentialPrefix, co.devfileMetadata.devfileRegistry.Name), registryUtil.RegistryUser)
if err != nil {
Expand Down Expand Up @@ -543,7 +552,11 @@ func (co *CreateOptions) devfileRun(cmd *cobra.Command) (err error) {
return err
}

if co.devfileMetadata.starterToken == "" && registryUtil.IsSecure(co.devfileMetadata.devfileRegistry.Name) {
secure, err := registryUtil.IsSecure(co.devfileMetadata.devfileRegistry.Name)
if err != nil {
return err
}
if co.devfileMetadata.starterToken == "" && secure {
var token string
token, err = keyring.Get(fmt.Sprintf("%s%s", util.CredentialPrefix, co.devfileMetadata.devfileRegistry.Name), registryUtil.RegistryUser)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/odo/cli/component/devfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (lo LogOptions) DevfileComponentLog() error {
componentName,
err,
)
os.Exit(1)
return err
}

return util.DisplayLog(lo.logFollow, rd, os.Stdout, componentName, -1)
Expand Down
5 changes: 4 additions & 1 deletion pkg/odo/cli/component/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ func (gto *GetOptions) Complete(name string, cmd *cobra.Command, args []string)
if err != nil {
return err
}
gto.componentName = gto.Context.ComponentAllowingEmpty(true)
gto.componentName, err = gto.Context.ComponentAllowingEmpty(true)
if err != nil {
return err
}
return
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/odo/cli/component/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ func (o *LinkOptions) Validate() (err error) {
if o.Context.EnvSpecificInfo != nil {
return
}
componentName, err := o.Component()
if err != nil {
return err
}

alreadyLinkedSecretNames, err := component.GetComponentLinkedSecretNames(o.Client, o.Component(), o.Application)
alreadyLinkedSecretNames, err := component.GetComponentLinkedSecretNames(o.Client, componentName, o.Application)
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/odo/cli/component/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ func (lo *ListOptions) Complete(name string, cmd *cobra.Command, args []string)

} else {
klog.V(4).Infof("New Config Context")
lo.Context = genericclioptions.NewConfigContext(cmd)
lo.Context, err = genericclioptions.NewConfigContext(cmd)
if err != nil {
return err
}
// for disconnected situation we just assume we have DC support
lo.hasDCSupport = true
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/odo/cli/preference/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/machineoutput"
"github.com/openshift/odo/pkg/odo/genericclioptions"
"github.com/openshift/odo/pkg/odo/util"
"github.com/openshift/odo/pkg/preference"
"github.com/spf13/cobra"
ktemplates "k8s.io/kubectl/pkg/util/templates"
Expand Down Expand Up @@ -46,7 +45,7 @@ func (o *ViewOptions) Run(cmd *cobra.Command) (err error) {
cfg, err := preference.New()

if err != nil {
util.LogErrorAndExit(err, "")
valaparthvi marked this conversation as resolved.
Show resolved Hide resolved
return err
}

if log.IsJSON() {
Expand Down
6 changes: 4 additions & 2 deletions pkg/odo/cli/registry/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ func (o *DeleteOptions) Validate() (err error) {

// Run contains the logic for "odo registry delete" command
func (o *DeleteOptions) Run(cmd *cobra.Command) (err error) {
isSecure := registryUtil.IsSecure(o.registryName)

isSecure, err := registryUtil.IsSecure(o.registryName)
if err != nil {
return err
}
cfg, err := preference.New()
if err != nil {
return errors.Wrap(err, "unable to delete registry")
Expand Down
9 changes: 4 additions & 5 deletions pkg/odo/cli/registry/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package registry
import (
// Built-in packages
"fmt"
util2 "github.com/openshift/odo/pkg/odo/cli/registry/util"
"io"
"os"
"text/tabwriter"
Expand All @@ -15,8 +14,8 @@ import (
ktemplates "k8s.io/kubectl/pkg/util/templates"

// odo packages
util "github.com/openshift/odo/pkg/odo/cli/registry/util"
"github.com/openshift/odo/pkg/odo/genericclioptions"
"github.com/openshift/odo/pkg/odo/util"
"github.com/openshift/odo/pkg/preference"
)

Expand Down Expand Up @@ -57,7 +56,7 @@ func (o *ListOptions) Validate() (err error) {
func (o *ListOptions) Run(cmd *cobra.Command) (err error) {
cfg, err := preference.New()
if err != nil {
util.LogErrorAndExit(err, "")
return err
}

registryList := cfg.OdoSettings.RegistryList
Expand All @@ -75,7 +74,7 @@ func (o *ListOptions) Run(cmd *cobra.Command) (err error) {
o.printRegistryList(w, registryList)
w.Flush()
if o.printGitRegistryDeprecationWarning {
util2.PrintGitRegistryDeprecationWarning()
util.PrintGitRegistryDeprecationWarning()
}
return
}
Expand All @@ -94,7 +93,7 @@ func (o *ListOptions) printRegistryList(w io.Writer, registryList *[]preference.
secure = "Yes"
}
fmt.Fprintln(w, registry.Name, "\t", registry.URL, "\t", secure)
if util2.IsGitBasedRegistry(registry.URL) {
if util.IsGitBasedRegistry(registry.URL) {
o.printGitRegistryDeprecationWarning = true
}
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/odo/cli/registry/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,16 @@ func (o *UpdateOptions) Validate() (err error) {
func (o *UpdateOptions) Run(cmd *cobra.Command) (err error) {
secureBeforeUpdate := false
secureAfterUpdate := false
if registryUtil.IsSecure(o.registryName) {

secure, err := registryUtil.IsSecure(o.registryName)
if err != nil {
return err
}

if secure {
secureBeforeUpdate = true
}

if o.token != "" {
secureAfterUpdate = true
}
Expand Down
9 changes: 3 additions & 6 deletions pkg/odo/cli/registry/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@ package util
import (
// odo packages

"os"
"strings"

"github.com/openshift/odo/pkg/log"
"github.com/openshift/odo/pkg/preference"
"github.com/pkg/errors"
)

const (
RegistryUser = "default"
)

// IsSecure checks if the registry is secure
func IsSecure(registryName string) bool {
func IsSecure(registryName string) (bool, error) {
cfg, err := preference.New()
if err != nil {
log.Error(errors.Cause(err))
os.Exit(1)
return false, err
}

isSecure := false
Expand All @@ -33,7 +30,7 @@ func IsSecure(registryName string) bool {
}
}

return isSecure
return isSecure, nil
}

func IsGitBasedRegistry(url string) bool {
Expand Down
5 changes: 4 additions & 1 deletion pkg/odo/cli/registry/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ func TestIsSecure(t *testing.T) {
t.Errorf("Unable to add registry to preference file with error: %v", err)
}

got := IsSecure(tt.registryName)
got, err := IsSecure(tt.registryName)
if err != nil {
t.Errorf("Unable to check if the registry is secure or not")
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Got: %t, want %t", got, tt.want)
}
Expand Down
Loading