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: 12 additions & 2 deletions app/cli/cmd/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package cmd

import (
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -55,6 +56,14 @@ func newAttestationCmd() *cobra.Command {
Short: "Craft Software Supply Chain Attestations",
Example: "Refer to https://docs.chainloop.dev/getting-started/attestation-crafting",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// The organization must be resolved before the root PersistentPreRunE,
// which uses it to build the control plane connection. Set the log
// level first so that resolving it honors --debug.
var err error
if logger, err = initLogger(logger); err != nil {
Comment thread
migmartri marked this conversation as resolved.
return err
}

if err := resolveAttestationOrganization(cmd); err != nil {
return err
}
Expand Down Expand Up @@ -120,8 +129,9 @@ func resolveAttestationOrganization(cmd *cobra.Command) error {
}
return nil
}
if err != nil {
logger.Debug().Err(err).Msg("failed to load repository organization")
// A missing .chainloop.yml is the common case, not a problem.
if err != nil && !errors.Is(err, repositoryconfig.ErrChainloopYMLNotFound) {
logger.Warn().Err(err).Msg("failed to load repository organization")
}
}

Expand Down
6 changes: 4 additions & 2 deletions app/cli/cmd/attestation_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ func newAttestationInitCmd() *cobra.Command {
// Load unresolved project metadata from the repository config.
if projectName == "" || (projectVersion == "" && !useLatestVersion) {
cfg, path, err := repositoryconfig.LoadChainloopYML(".")
if err != nil {
switch {
case errors.Is(err, repositoryconfig.ErrChainloopYMLNotFound):
case err != nil:
logger.Debug().Msgf("failed to load chainloop config: %s", err)
} else {
default:
logger.Debug().Msgf("loaded project metadata from config file %s", path)
if projectName == "" {
projectName = cfg.ProjectName
Expand Down
54 changes: 54 additions & 0 deletions app/cli/cmd/attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,60 @@ func TestAttestationRepositoryOrganizationNotice(t *testing.T) {
assert.Equal(t, "saved-org", viper.GetString(confOptions.organization.viperKey))
}

func TestAttestationRepositoryConfigLookupLogging(t *testing.T) {
for _, tc := range []struct {
name string
config string
debug bool
wantWarn bool
}{
{name: "missing config"},
{name: "missing config with debug", debug: true},
{name: "invalid config", config: "organization: [\n", wantWarn: true},
{name: "invalid config with debug", config: "organization: [\n", debug: true, wantWarn: true},
} {
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
if tc.config != "" {
require.NoError(t, os.WriteFile(filepath.Join(dir, ".chainloop.yml"), []byte(tc.config), 0o600))
}
t.Chdir(dir)

_, cmd := newAttestationOrganizationTestCommand(t, "init", "saved-org")
previousDebug := flagDebug
flagDebug = tc.debug
t.Cleanup(func() { flagDebug = previousDebug })

// Same as the logger seeded in main.go: no level set until the root
// PersistentPreRunE runs.
var output bytes.Buffer
logger = zerolog.New(&output)

require.NoError(t, cmd.Parent().PersistentPreRunE(cmd, nil))
// init reads .chainloop.yml again for the project metadata. --release
// without a version makes PreRunE stop right after that lookup.
require.NoError(t, cmd.Flags().Set("workflow", "build"))
require.NoError(t, cmd.Flags().Set("release", "true"))
require.EqualError(t, cmd.PreRunE(cmd, nil), "project version is required when using --release")

if tc.wantWarn {
assert.Contains(t, output.String(), `"level":"warn"`)
} else {
assert.Empty(t, output.String())
}
if !tc.debug {
assert.NotContains(t, output.String(), `"level":"debug"`)
}

wantLevel := zerolog.InfoLevel
if tc.debug {
wantLevel = zerolog.DebugLevel
}
assert.Equal(t, wantLevel, logger.GetLevel())
})
}
}

func newAttestationOrganizationTestCommand(t *testing.T, command, savedOrganization string) (*cobra.Command, *cobra.Command) {
t.Helper()

Expand Down
Loading