-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from keisku/refactor
Refactor for testability
- Loading branch information
Showing
5 changed files
with
337 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/kube-openapi/pkg/util/proto" | ||
"k8s.io/kubectl/pkg/explain" | ||
) | ||
|
||
type explainer struct { | ||
schemaByGvk proto.Schema | ||
gvk schema.GroupVersionKind | ||
pathSchema map[string]proto.Schema | ||
} | ||
|
||
// explain explains the field associated with the given path. | ||
func (e *explainer) explain(w io.Writer, path string) error { | ||
if path == "" { | ||
return fmt.Errorf("path is empty: gvk=%s", e.gvk) | ||
} | ||
// This is the case that path specifies the top-level field, | ||
// for example, "pod.spec", "pod.metadata" | ||
if strings.Count(path, ".") == 1 { | ||
fieldPath := []string{path[strings.LastIndex(path, ".")+1:]} | ||
return explain.PrintModelDescription(fieldPath, w, e.schemaByGvk, e.gvk, false) | ||
} | ||
|
||
// get the parent schema to explain. | ||
// e.g. "pod.spec.containers.env" -> "pod.spec.containers" | ||
parent, ok := e.pathSchema[path[:strings.LastIndex(path, ".")]] | ||
if !ok { | ||
return fmt.Errorf("%q is not found", path) | ||
} | ||
|
||
// get the key from the path. | ||
// e.g. "pod.spec.containers.env" -> "env" | ||
fieldPath := []string{path[strings.LastIndex(path, ".")+1:]} | ||
if err := explain.PrintModelDescription(fieldPath, w, parent, e.gvk, false); err != nil { | ||
return fmt.Errorf("explain %q: %w", path, err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.