diff --git a/slice/errors.go b/slice/errors.go index 31bf57d..5f6ed10 100644 --- a/slice/errors.go +++ b/slice/errors.go @@ -1,6 +1,9 @@ package slice -import "fmt" +import ( + "fmt" + "strings" +) type strictModeSkipErr struct { fieldName string @@ -20,3 +23,31 @@ type skipErr struct { func (e *skipErr) Error() string { return fmt.Sprintf("resource %s %q is configured to be skipped", e.kind, e.name) } + +const nonK8sHelper = `the file has no Kubernetes metadata: it is most likely a non-Kubernetes YAML file, you can skip it with --skip-non-k8s` + +type cantFindFieldErr struct { + fieldName string + fileCount int + meta kubeObjectMeta +} + +func (e *cantFindFieldErr) Error() string { + var sb strings.Builder + + sb.WriteString(fmt.Sprintf( + "unable to find Kubernetes %q field in file %d", + e.fieldName, e.fileCount, + )) + + if e.meta.empty() { + sb.WriteString(": " + nonK8sHelper) + } else { + sb.WriteString(fmt.Sprintf( + ": processed details: kind %q, name %q, apiVersion %q", + e.meta.Kind, e.meta.Name, e.meta.APIVersion, + )) + } + + return sb.String() +} diff --git a/slice/errors_test.go b/slice/errors_test.go index 437bac4..b3faf1d 100644 --- a/slice/errors_test.go +++ b/slice/errors_test.go @@ -9,6 +9,7 @@ import ( func TestErrorsInterface(t *testing.T) { require.Implementsf(t, (*error)(nil), &strictModeSkipErr{}, "strictModeSkipErr should implement error") require.Implementsf(t, (*error)(nil), &skipErr{}, "skipErr should implement error") + require.Implementsf(t, (*error)(nil), &cantFindFieldErr{}, "cantFindFieldErr should implement error") } func requireErrorIf(t *testing.T, wantErr bool, err error) { diff --git a/slice/kube.go b/slice/kube.go index b872dd5..ba17251 100644 --- a/slice/kube.go +++ b/slice/kube.go @@ -15,6 +15,10 @@ type kubeObjectMeta struct { Namespace string } +func (k kubeObjectMeta) empty() bool { + return k.APIVersion == "" && k.Kind == "" && k.Name == "" && k.Namespace == "" +} + // from: https://github.com/helm/helm/blob/v3.11.1/pkg/releaseutil/kind_sorter.go#LL31-L67C2 var helmInstallOrder = []string{ "Namespace", diff --git a/slice/process.go b/slice/process.go index 583dc5b..cc2ee4f 100644 --- a/slice/process.go +++ b/slice/process.go @@ -58,12 +58,12 @@ func (s *Split) parseYAMLManifest(contents []byte) (yamlFile, error) { // Check if we have a Kubernetes kind and we're requesting inclusion or exclusion if k8smeta.Kind == "" && !s.opts.AllowEmptyKinds && (hasIncluded || hasExcluded) { - return yamlFile{}, fmt.Errorf("unable to find Kubernetes \"kind\" field in file number %d", s.fileCount) + return yamlFile{}, &cantFindFieldErr{fieldName: "kind", fileCount: s.fileCount, meta: k8smeta} } // Check if we have a Kubernetes name and we're requesting inclusion or exclusion if k8smeta.Name == "" && !s.opts.AllowEmptyNames && (hasIncluded || hasExcluded) { - return yamlFile{}, fmt.Errorf("unable to find Kubernetes \"metadata.name\" field in file number %d", s.fileCount) + return yamlFile{}, &cantFindFieldErr{fieldName: "metadata.name", fileCount: s.fileCount, meta: k8smeta} } // We need to check if the file should be skipped