Skip to content

Commit

Permalink
Use ctx instead of GLOBALODOCONFIG
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Nov 9, 2022
1 parent 01fec61 commit 67387da
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 102 deletions.
6 changes: 4 additions & 2 deletions cmd/cli-doc/cli-doc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -171,11 +172,12 @@ func main() {
if len(args) == 0 {
fmt.Print(command.Usage())
} else {
ctx := context.Background()
switch args[0] {
case "reference":
fmt.Print(referencePrinter(cli.NewCmdOdo(cli.OdoRecommendedName, cli.OdoRecommendedName), 0))
fmt.Print(referencePrinter(cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName), 0))
case "structure":
fmt.Print(commandPrinter(cli.NewCmdOdo(cli.OdoRecommendedName, cli.OdoRecommendedName), 0))
fmt.Print(commandPrinter(cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName), 0))
default:
fmt.Print(command.Usage())
}
Expand Down
27 changes: 20 additions & 7 deletions cmd/odo/odo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,36 @@ import (
"os"

"github.com/posener/complete"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/redhat-developer/odo/pkg/config"
envcontext "github.com/redhat-developer/odo/pkg/config/context"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/odo/cli"
"github.com/redhat-developer/odo/pkg/odo/cli/version"
"github.com/redhat-developer/odo/pkg/odo/util"
"github.com/redhat-developer/odo/pkg/odo/util/completion"
"github.com/redhat-developer/odo/pkg/preference"
segment "github.com/redhat-developer/odo/pkg/segment/context"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"k8s.io/klog"
)

func main() {
// Create a context ready for receiving telemetry data
// and save into it configuration based on environment variables
ctx := segment.NewContext(context.Background())
envConfig, err := config.GetConfiguration()
if err != nil {
util.LogErrorAndExit(err, "")
}
ctx = envcontext.WithEnvConfig(ctx, *envConfig)

// create the complete command
klog.InitFlags(nil)

root := cli.NewCmdOdo(cli.OdoRecommendedName, cli.OdoRecommendedName)
root := cli.NewCmdOdo(ctx, cli.OdoRecommendedName, cli.OdoRecommendedName)
rootCmp := createCompletion(root)
cmp := complete.New("odo", rootCmp)

Expand All @@ -41,7 +54,7 @@ func main() {
// parse the flags but hack around to avoid exiting with error code 2 on help
flag.CommandLine.Init(os.Args[0], flag.ContinueOnError)
args := os.Args[1:]
if err := flag.CommandLine.Parse(args); err != nil {
if err = flag.CommandLine.Parse(args); err != nil {
if err == flag.ErrHelp {
os.Exit(0)
}
Expand All @@ -55,7 +68,7 @@ func main() {
return
}

cfg, err := preference.NewClient()
cfg, err := preference.NewClient(ctx)
if err != nil {
util.LogErrorAndExit(err, "")
}
Expand All @@ -67,15 +80,15 @@ func main() {
updateInfo := make(chan string)
go version.GetLatestReleaseInfo(updateInfo)

util.LogErrorAndExit(root.ExecuteContext(segment.NewContext(context.Background())), "")
util.LogErrorAndExit(root.ExecuteContext(ctx), "")
select {
case message := <-updateInfo:
log.Info(message)
default:
klog.V(4).Info("Could not get the latest release information in time. Never mind, exiting gracefully :)")
}
} else {
util.LogErrorAndExit(root.ExecuteContext(segment.NewContext(context.Background())), "")
util.LogErrorAndExit(root.ExecuteContext(ctx), "")
}
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/odo/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"context"
"errors"
"flag"
"fmt"
Expand Down Expand Up @@ -101,8 +102,8 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e
const pluginPrefix = "odo"

// NewCmdOdo creates a new root command for odo
func NewCmdOdo(name, fullName string) *cobra.Command {
rootCmd := odoRootCmd(name, fullName)
func NewCmdOdo(ctx context.Context, name, fullName string) *cobra.Command {
rootCmd := odoRootCmd(ctx, name, fullName)

if len(os.Args) > 1 {
cmdPathPieces := os.Args[1:]
Expand All @@ -120,7 +121,7 @@ func NewCmdOdo(name, fullName string) *cobra.Command {
return rootCmd
}

func odoRootCmd(name, fullName string) *cobra.Command {
func odoRootCmd(ctx context.Context, name, fullName string) *cobra.Command {
// rootCmd represents the base command when called without any subcommands
rootCmd := &cobra.Command{
Use: name,
Expand Down Expand Up @@ -177,7 +178,7 @@ func odoRootCmd(name, fullName string) *cobra.Command {
login.NewCmdLogin(login.RecommendedCommandName, util.GetFullName(fullName, login.RecommendedCommandName)),
logout.NewCmdLogout(logout.RecommendedCommandName, util.GetFullName(fullName, logout.RecommendedCommandName)),
version.NewCmdVersion(version.RecommendedCommandName, util.GetFullName(fullName, version.RecommendedCommandName)),
preference.NewCmdPreference(preference.RecommendedCommandName, util.GetFullName(fullName, preference.RecommendedCommandName)),
preference.NewCmdPreference(ctx, preference.RecommendedCommandName, util.GetFullName(fullName, preference.RecommendedCommandName)),
telemetry.NewCmdTelemetry(telemetry.RecommendedCommandName),
list.NewCmdList(list.RecommendedCommandName, util.GetFullName(fullName, list.RecommendedCommandName)),
build_images.NewCmdBuildImages(build_images.RecommendedCommandName, util.GetFullName(fullName, build_images.RecommendedCommandName)),
Expand Down
5 changes: 3 additions & 2 deletions pkg/odo/cli/preference/preference.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package preference

import (
"context"
"fmt"

"github.com/redhat-developer/odo/pkg/odo/cli/preference/add"
Expand All @@ -20,11 +21,11 @@ var preferenceLongDesc = ktemplates.LongDesc(`Modifies odo specific configuratio
%[1]s`)

// NewCmdPreference implements the utils config odo command
func NewCmdPreference(name, fullName string) *cobra.Command {
func NewCmdPreference(ctx context.Context, name, fullName string) *cobra.Command {

// Main Commands
preferenceViewCmd := NewCmdView(viewCommandName, util.GetFullName(fullName, viewCommandName))
preferenceSetCmd := NewCmdSet(setCommandName, util.GetFullName(fullName, setCommandName))
preferenceSetCmd := NewCmdSet(ctx, setCommandName, util.GetFullName(fullName, setCommandName))
preferenceUnsetCmd := NewCmdUnset(unsetCommandName, util.GetFullName(fullName, unsetCommandName))
preferenceAddCmd := add.NewCmdAdd(add.RecommendedCommandName, util.GetFullName(fullName, add.RecommendedCommandName))
preferenceRemoveCmd := remove.NewCmdRemove(remove.RecommendedCommandName, util.GetFullName(fullName, remove.RecommendedCommandName))
Expand Down
4 changes: 2 additions & 2 deletions pkg/odo/cli/preference/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ func (o *SetOptions) Run(ctx context.Context) (err error) {
}

// NewCmdSet implements the config set odo command
func NewCmdSet(name, fullName string) *cobra.Command {
func NewCmdSet(ctx context.Context, name, fullName string) *cobra.Command {
o := NewSetOptions()
preferenceSetCmd := &cobra.Command{
Use: name,
Short: "Set a value in the odo preference file",
Long: fmt.Sprintf(setLongDesc, preference.FormatSupportedParameters()),
Example: func(exampleString, fullName string) string {
prefClient, err := preference.NewClient()
prefClient, err := preference.NewClient(ctx)
if err != nil {
util.LogErrorAndExit(err, "unable to set preference, something is wrong with odo, kindly raise an issue at https://github.com/redhat-developer/odo/issues/new?template=Bug.md")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/odo/genericclioptions/clientset/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func Fetch(command *cobra.Command, platform string) (*Clientset, error) {
}
}
if isDefined(command, PREFERENCE) {
dep.PreferenceClient, err = preference.NewClient()
dep.PreferenceClient, err = preference.NewClient(command.Context())
if err != nil {
return nil, err
}
Expand Down
23 changes: 10 additions & 13 deletions pkg/odo/genericclioptions/runnable.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/devfile/library/pkg/devfile/parser"

"github.com/redhat-developer/odo/pkg/config"
"github.com/redhat-developer/odo/pkg/machineoutput"

"github.com/redhat-developer/odo/pkg/odo/cmdline"
Expand Down Expand Up @@ -81,14 +80,14 @@ const (
)

func GenericRun(o Runnable, cmd *cobra.Command, args []string) {
var err error
startTime := time.Now()
userConfig, _ := preference.NewClient()
var (
err error
startTime = time.Now()
ctx = cmd.Context()
)

envConfig, err := config.GetConfiguration()
if err != nil {
util.LogErrorAndExit(err, "")
}
userConfig, _ := preference.NewClient(ctx)
envConfig := envcontext.GetEnvConfig(ctx)

//lint:ignore SA1019 We deprecated this env var, but until it is removed, we still need to support it
disableTelemetryEnvSet := envConfig.OdoDisableTelemetry != nil
Expand All @@ -97,7 +96,7 @@ func GenericRun(o Runnable, cmd *cobra.Command, args []string) {
disableTelemetry = *envConfig.OdoDisableTelemetry
}
debugTelemetry := pointer.StringDeref(envConfig.OdoDebugTelemetryFile, "")
trackingConsentValue, isTrackingConsentEnabled, trackingConsentEnvSet, trackingConsentErr := segment.IsTrackingConsentEnabled(envConfig)
trackingConsentValue, isTrackingConsentEnabled, trackingConsentEnvSet, trackingConsentErr := segment.IsTrackingConsentEnabled(&envConfig)

// check for conflicting settings
if trackingConsentErr == nil && disableTelemetryEnvSet && trackingConsentEnvSet && disableTelemetry == isTrackingConsentEnabled {
Expand Down Expand Up @@ -158,7 +157,7 @@ func GenericRun(o Runnable, cmd *cobra.Command, args []string) {

scontext.SetFlags(cmd.Context(), cmd.Flags())
// set value for telemetry status in context so that we do not need to call IsTelemetryEnabled every time to check its status
scontext.SetTelemetryStatus(cmd.Context(), segment.IsTelemetryEnabled(userConfig, *envConfig))
scontext.SetTelemetryStatus(cmd.Context(), segment.IsTelemetryEnabled(userConfig, envConfig))

// Send data to telemetry in case of user interrupt
captureSignals := []os.Signal{syscall.SIGHUP, syscall.SIGTERM, syscall.SIGQUIT, os.Interrupt}
Expand All @@ -174,7 +173,7 @@ func GenericRun(o Runnable, cmd *cobra.Command, args []string) {
startTelemetry(cmd, err, startTime)
})

util.LogErrorAndExit(commonflags.CheckMachineReadableOutputCommand(envConfig, cmd), "")
util.LogErrorAndExit(commonflags.CheckMachineReadableOutputCommand(&envConfig, cmd), "")
util.LogErrorAndExit(commonflags.CheckRunOnCommand(cmd), "")
util.LogErrorAndExit(commonflags.CheckVariablesCommand(cmd), "")

Expand All @@ -186,8 +185,6 @@ func GenericRun(o Runnable, cmd *cobra.Command, args []string) {
}
o.SetClientset(deps)

ctx := cmdLineObj.Context()
ctx = envcontext.WithEnvConfig(ctx, *envConfig)
ctx = fcontext.WithJsonOutput(ctx, commonflags.GetJsonOutputValue(cmdLineObj))
ctx = fcontext.WithRunOn(ctx, platform)
ctx = odocontext.WithApplication(ctx, defaultAppName)
Expand Down
17 changes: 10 additions & 7 deletions pkg/preference/implem.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package preference

import (
"context"
"fmt"
"os"
"os/user"
Expand All @@ -9,6 +10,7 @@ import (
"strings"
"time"

envcontext "github.com/redhat-developer/odo/pkg/config/context"
"github.com/redhat-developer/odo/pkg/log"
"github.com/redhat-developer/odo/pkg/odo/cli/ui"
"github.com/redhat-developer/odo/pkg/util"
Expand Down Expand Up @@ -69,9 +71,10 @@ type preferenceInfo struct {

var _ Client = (*preferenceInfo)(nil)

func getPreferenceFile() (string, error) {
if env, ok := os.LookupEnv(GlobalConfigEnvName); ok {
return env, nil
func getPreferenceFile(ctx context.Context) (string, error) {
envConfig := envcontext.GetEnvConfig(ctx)
if envConfig.Globalodoconfig != nil {
return *envConfig.Globalodoconfig, nil
}

if len(customHomeDir) != 0 {
Expand All @@ -85,8 +88,8 @@ func getPreferenceFile() (string, error) {
return filepath.Join(currentUser.HomeDir, ".odo", configFileName), nil
}

func NewClient() (Client, error) {
return newPreferenceInfo()
func NewClient(ctx context.Context) (Client, error) {
return newPreferenceInfo(ctx)
}

// newPreference creates an empty Preference struct with type meta information
Expand All @@ -101,8 +104,8 @@ func newPreference() Preference {

// newPreferenceInfo gets the PreferenceInfo from preference file
// or returns default PreferenceInfo if preference file does not exist
func newPreferenceInfo() (*preferenceInfo, error) {
preferenceFile, err := getPreferenceFile()
func newPreferenceInfo(ctx context.Context) (*preferenceInfo, error) {
preferenceFile, err := getPreferenceFile(ctx)
klog.V(4).Infof("The path for preference file is %+v", preferenceFile)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 67387da

Please sign in to comment.