Skip to content
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
14 changes: 14 additions & 0 deletions app/cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ func Execute(rootCmd *cobra.Command) error {
}

func NewRootCmd(l zerolog.Logger) *cobra.Command {
// Seed the root logger here and not only in PersistentPreRunE: cobra
// rejects unknown subcommands and invalid flags before that hook runs, and
// with SilenceErrors set main is the only place those errors get printed.
// A zero-value zerolog.Logger has no writer and would drop them silently.
logger = l

rootCmd := &cobra.Command{
Use: appName,
Short: "Chainloop Command Line Interface",
Expand Down Expand Up @@ -290,6 +296,7 @@ func NewRootCmd(l zerolog.Logger) *cobra.Command {
newAttestationCmd(), newArtifactCmd(), newConfigCmd(),
newIntegrationCmd(), newOrganizationCmd(), newCASBackendCmd(),
newReferrerDiscoverCmd(), newPolicyCmd(), newApplyCmd(),
newTraceCmd(),
)

// Load plugins for root command and subcommands (except completion and help)
Expand Down Expand Up @@ -329,6 +336,13 @@ func isTelemetryDisabled() bool {
return os.Getenv(doNotTrackEnv) == "1" || os.Getenv(doNotTrackEnv) == trueString || Version == devVersion
}

// Logger returns the root logger. Hook commands swap it in place (see
// InitHookLogger), and main reads it for its final error line so that line
// shares the hook's colorless formatting during a git commit or push.
func Logger() zerolog.Logger {
return logger
}

func initLogger(logger zerolog.Logger) (zerolog.Logger, error) {
lvl := zerolog.InfoLevel
if flagDebug {
Expand Down
18 changes: 18 additions & 0 deletions app/cli/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package cmd

import (
"bytes"
"testing"

"github.com/rs/zerolog"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -111,3 +114,18 @@ func TestLoadAuthToken(t *testing.T) {
})
}
}

// Cobra rejects unknown subcommands and invalid flags before PersistentPreRunE
// runs, so main prints those errors through a Logger() that PersistentPreRunE
// never got to initialize. NewRootCmd must leave the root logger usable.
func TestNewRootCmdInitializesLogger(t *testing.T) {
previous := logger
t.Cleanup(func() { logger = previous })

var out bytes.Buffer
NewRootCmd(zerolog.New(&out))

rootLogger := Logger()
rootLogger.Error().Msg("boom")
assert.Contains(t, out.String(), "boom")
}
37 changes: 37 additions & 0 deletions app/cli/cmd/trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Copyright 2026 The Chainloop Authors.
//
// 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 cmd

import (
"github.com/spf13/cobra"
)

// newTraceCmd creates the trace parent command with all subcommands.
func newTraceCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "trace",
Short: "AI coding session tracing and attestation",
}

cmd.AddCommand(
newTraceInitCmd(),
newTraceRunCmd(),
newTraceUninstallCmd(),
newTraceHookCmd(),
)

return cmd
}
Loading
Loading