Skip to content

Commit

Permalink
Undo moving of some logger function reliant on shared state
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Mar 14, 2024
1 parent 540b93d commit 16617a1
Show file tree
Hide file tree
Showing 20 changed files with 135 additions and 113 deletions.
3 changes: 1 addition & 2 deletions cmd/timoni/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/stefanprodan/timoni/internal/engine/fetcher"
cueerrors "github.com/stefanprodan/timoni/internal/errors"
"github.com/stefanprodan/timoni/internal/flags"
"github.com/stefanprodan/timoni/internal/logger"
"github.com/stefanprodan/timoni/internal/runtime"
)

Expand Down Expand Up @@ -143,7 +142,7 @@ func runApplyCmd(cmd *cobra.Command, args []string) error {
applyArgs.name = args[0]
applyArgs.module = args[1]

log := logger.LoggerInstance(cmd.Context(), applyArgs.name, true)
log := loggerInstance(cmd.Context(), applyArgs.name, true)

version := applyArgs.version.String()
if version == "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/artifact_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func pullArtifactCmdRun(cmd *cobra.Command, args []string) error {
}
ociURL := args[0]

log := logger.LoggerFrom(cmd.Context())
log := LoggerFrom(cmd.Context())

if err := os.MkdirAll(pullArtifactArgs.output, os.ModePerm); err != nil {
return fmt.Errorf("invalid output path %s: %w", pullArtifactArgs.output, err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/artifact_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func pushArtifactCmdRun(cmd *cobra.Command, args []string) error {
pushArtifactArgs.ignorePaths = append(pushArtifactArgs.ignorePaths, ps...)
}

log := logger.LoggerFrom(cmd.Context())
log := LoggerFrom(cmd.Context())
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
defer cancel()

Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/artifact_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func tagArtifactCmdRun(cmd *cobra.Command, args []string) error {
spin := logger.StartSpinner("tagging artifact")
defer spin.Stop()

log := logger.LoggerFrom(cmd.Context())
log := LoggerFrom(cmd.Context())
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
defer cancel()

Expand Down
4 changes: 2 additions & 2 deletions cmd/timoni/bundle_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func runBundleApplyCmd(cmd *cobra.Command, _ []string) error {
return err
}

log := logger.LoggerBundle(cmd.Context(), bundle.Name, cluster.Name, true)
log := loggerBundle(cmd.Context(), bundle.Name, cluster.Name, true)

if !bundleApplyArgs.overwriteOwnership {
err = bundleInstancesOwnershipConflicts(bundle.Instances)
Expand Down Expand Up @@ -286,7 +286,7 @@ func fetchBundleInstanceModule(ctx context.Context, instance *engine.BundleInsta
}

func applyBundleInstance(ctx context.Context, cuectx *cue.Context, instance *engine.BundleInstance, kubeVersion string, rootDir string, diffOutput io.Writer) error {
log := logger.LoggerBundleInstance(ctx, instance.Bundle, instance.Cluster, instance.Name, true)
log := loggerBundleInstance(ctx, instance.Bundle, instance.Cluster, instance.Name, true)

modDir := path.Join(rootDir, instance.Name, "module")
builder := engine.NewModuleBuilder(
Expand Down
4 changes: 2 additions & 2 deletions cmd/timoni/bundle_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func runBundleDelCmd(cmd *cobra.Command, args []string) error {
return err
}

log := logger.LoggerBundle(ctx, bundleDelArgs.name, cluster.Name, true)
log := loggerBundle(ctx, bundleDelArgs.name, cluster.Name, true)

if len(instances) == 0 {
log.Error(nil, "no instances found in bundle")
Expand All @@ -151,7 +151,7 @@ func runBundleDelCmd(cmd *cobra.Command, args []string) error {
}

func deleteBundleInstance(ctx context.Context, instance *engine.BundleInstance, wait bool, dryrun bool) error {
log := logger.LoggerBundle(ctx, instance.Bundle, instance.Cluster, true)
log := loggerBundle(ctx, instance.Bundle, instance.Cluster, true)

sm, err := runtime.NewResourceManager(kubeconfigArgs)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/timoni/bundle_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func runBundleStatusCmd(cmd *cobra.Command, args []string) error {
return err
}

log := logger.LoggerBundle(ctx, bundleStatusArgs.name, cluster.Name, true)
log := loggerBundle(ctx, bundleStatusArgs.name, cluster.Name, true)

if len(instances) == 0 {
log.Error(nil, "no instances found in bundle")
Expand All @@ -117,7 +117,7 @@ func runBundleStatusCmd(cmd *cobra.Command, args []string) error {
}

for _, instance := range instances {
log := logger.LoggerBundleInstance(ctx, bundleStatusArgs.name, cluster.Name, instance.Name, true)
log := loggerBundleInstance(ctx, bundleStatusArgs.name, cluster.Name, instance.Name, true)

log.Info(fmt.Sprintf("last applied %s",
logger.ColorizeSubject(instance.LastTransitionTime)))
Expand Down
7 changes: 3 additions & 4 deletions cmd/timoni/bundle_vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/stefanprodan/timoni/internal/engine"
cueerrors "github.com/stefanprodan/timoni/internal/errors"
"github.com/stefanprodan/timoni/internal/flags"
"github.com/stefanprodan/timoni/internal/logger"
"github.com/stefanprodan/timoni/internal/runtime"
runtimebuild "github.com/stefanprodan/timoni/internal/runtime/build"
)
Expand Down Expand Up @@ -81,7 +80,7 @@ func init() {
}

func runBundleVetCmd(cmd *cobra.Command, args []string) error {
log := logger.LoggerFrom(cmd.Context())
log := LoggerFrom(cmd.Context())
files := bundleVetArgs.files
if len(files) == 0 {
return fmt.Errorf("no bundle provided with -f")
Expand Down Expand Up @@ -175,7 +174,7 @@ func runBundleVetCmd(cmd *cobra.Command, args []string) error {
return err
}

log = logger.LoggerBundle(logr.NewContext(cmd.Context(), log), bundle.Name, apiv1.RuntimeDefaultName, true)
log = loggerBundle(logr.NewContext(cmd.Context(), log), bundle.Name, apiv1.RuntimeDefaultName, true)

if len(bundle.Instances) == 0 {
return fmt.Errorf("no instances found in bundle")
Expand All @@ -199,7 +198,7 @@ func runBundleVetCmd(cmd *cobra.Command, args []string) error {
if i.Namespace == "" {
return fmt.Errorf("instance %s does not have a namespace", i.Name)
}
log := logger.LoggerBundleInstance(logr.NewContext(cmd.Context(), log), bundle.Name, cluster.Name, i.Name, true)
log := loggerBundleInstance(logr.NewContext(cmd.Context(), log), bundle.Name, cluster.Name, i.Name, true)
log.Info("instance is valid")
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func runDeleteCmd(cmd *cobra.Command, args []string) error {

deleteArgs.name = args[0]

log := logger.LoggerInstance(cmd.Context(), deleteArgs.name, true)
log := loggerInstance(cmd.Context(), deleteArgs.name, true)
sm, err := runtime.NewResourceManager(kubeconfigArgs)
if err != nil {
return err
Expand Down
111 changes: 111 additions & 0 deletions cmd/timoni/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Copyright 2024 Stefan Prodan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"context"
"fmt"

"github.com/fatih/color"
"github.com/go-logr/logr"

apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"
"github.com/stefanprodan/timoni/internal/logger"
)

func loggerBundle(ctx context.Context, bundle, cluster string, prettify bool) logr.Logger {
switch cluster {
case apiv1.RuntimeDefaultName:
if !prettify {
return LoggerFrom(ctx, "bundle", bundle)
}
return LoggerFrom(ctx, "caller", logger.ColorizeBundle(bundle))
default:
if !prettify {
return LoggerFrom(ctx, "bundle", bundle, "cluster", cluster)
}
return LoggerFrom(ctx, "caller",
fmt.Sprintf("%s %s %s",
logger.ColorizeBundle(bundle),
color.CyanString(">"),
logger.ColorizeCluster(cluster)))
}
}

func loggerInstance(ctx context.Context, instance string, prettify bool) logr.Logger {
if !prettify {
return LoggerFrom(ctx, "instance", instance)
}
return LoggerFrom(ctx, "caller", logger.ColorizeInstance(instance))
}

func loggerBundleInstance(ctx context.Context, bundle, cluster, instance string, prettify bool) logr.Logger {
switch cluster {
case apiv1.RuntimeDefaultName:
if !prettify {
return LoggerFrom(ctx, "bundle", bundle, "instance", instance)
}
return LoggerFrom(ctx, "caller",
fmt.Sprintf("%s %s %s",
logger.ColorizeBundle(bundle),
color.CyanString(">"),
logger.ColorizeInstance(instance)))
default:
if !prettify {
return LoggerFrom(ctx, "bundle", bundle, "cluster", cluster, "instance", instance)
}
return LoggerFrom(ctx, "caller",
fmt.Sprintf("%s %s %s %s %s",
logger.ColorizeBundle(bundle),
color.CyanString(">"),
logger.ColorizeCluster(cluster),
color.CyanString(">"),
logger.ColorizeInstance(instance)))

}
}

func loggerRuntime(ctx context.Context, runtime, cluster string, prettify bool) logr.Logger {
switch cluster {
case apiv1.RuntimeDefaultName:
if !prettify {
return LoggerFrom(ctx, "runtime", runtime)
}
return LoggerFrom(ctx, "caller", logger.ColorizeRuntime(runtime))
default:
if !prettify {
return LoggerFrom(ctx, "runtime", runtime, "cluster", cluster)
}
return LoggerFrom(ctx, "caller",
fmt.Sprintf("%s %s %s", logger.ColorizeRuntime(runtime),
color.CyanString(">"), logger.ColorizeCluster(cluster)))
}
}

// LoggerFrom returns a logr.Logger with predefined values from a context.Context.
func LoggerFrom(ctx context.Context, keysAndValues ...interface{}) logr.Logger {
if cliLogger.IsZero() {
cliLogger = logger.NewConsoleLogger(false, false)
}
newLogger := cliLogger
if ctx != nil {
if l, err := logr.FromContext(ctx); err == nil {
newLogger = l
}
}
return newLogger.WithValues(keysAndValues...)
}
2 changes: 1 addition & 1 deletion cmd/timoni/mod_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func runInitModCmd(cmd *cobra.Command, args []string) error {
initModArgs.path = "."
}

log := logger.LoggerFrom(cmd.Context())
log := LoggerFrom(cmd.Context())

if fs, err := os.Stat(initModArgs.path); err != nil || !fs.IsDir() {
return fmt.Errorf("path not found: %s", initModArgs.path)
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/mod_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func pullCmdRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("invalid output path %s: %w", pullModArgs.output, err)
}

log := logger.LoggerFrom(cmd.Context())
log := LoggerFrom(cmd.Context())

if pullModArgs.verify != "" {
err := oci.VerifyArtifact(log,
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/mod_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func pushModCmdRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("module not found at path %s", pushModArgs.module)
}

log := logger.LoggerFrom(cmd.Context())
log := LoggerFrom(cmd.Context())

annotations, err := oci.ParseAnnotations(pushModArgs.annotations)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/mod_vendor_crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func runVendorCrdCmd(cmd *cobra.Command, args []string) error {
vendorCrdArgs.modRoot = args[0]
}

log := logger.LoggerFrom(cmd.Context())
log := LoggerFrom(cmd.Context())
cuectx := cuecontext.New()

// Make sure we're importing into a CUE module.
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/mod_vendor_k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func runVendorK8sCmd(cmd *cobra.Command, args []string) error {
vendorK8sArgs.modRoot = args[0]
}

log := logger.LoggerFrom(cmd.Context())
log := LoggerFrom(cmd.Context())

// Make sure we're importing into a CUE module.
cueModDir := path.Join(vendorK8sArgs.modRoot, "cue.mod")
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/mod_vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func runVetModCmd(cmd *cobra.Command, args []string) error {
return fmt.Errorf("module not found at path %s", vetModArgs.path)
}

log := logger.LoggerFrom(cmd.Context())
log := LoggerFrom(cmd.Context())
cuectx := cuecontext.New()

tmpDir, err := os.MkdirTemp("", apiv1.FieldManager)
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/runtime_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func runRuntimeBuildCmd(cmd *cobra.Command, args []string) error {
}

for _, cluster := range clusters {
log := logger.LoggerRuntime(cmd.Context(), rt.Name, cluster.Name, true)
log := loggerRuntime(cmd.Context(), rt.Name, cluster.Name, true)

kubeconfigArgs.Context = &cluster.KubeContext
rm, err := runtime.NewResourceManager(kubeconfigArgs)
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func runStatusCmd(cmd *cobra.Command, args []string) error {

statusArgs.name = args[0]

log := logger.LoggerInstance(cmd.Context(), statusArgs.name, true)
log := loggerInstance(cmd.Context(), statusArgs.name, true)
rm, err := runtime.NewResourceManager(kubeconfigArgs)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion internal/dyff/dyff.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/fluxcd/pkg/ssa"
ssaerr "github.com/fluxcd/pkg/ssa/errors"
ssautil "github.com/fluxcd/pkg/ssa/utils"
"github.com/go-logr/logr"
"github.com/gonvenience/ytbx"
"github.com/homeport/dyff/pkg/dyff"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -94,7 +95,7 @@ func InstanceDryRunDiff(ctx context.Context,
tmpDir string,
withDiff bool,
w io.Writer) error {
log := logger.LoggerFrom(ctx)
log := logr.FromContextOrDiscard(ctx)
diffOpts := ssa.DefaultDiffOptions()
sort.Sort(ssa.SortableUnstructureds(objects))

Expand Down
Loading

0 comments on commit 16617a1

Please sign in to comment.