-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
skaffold inspect
command (#5765)
* Add `skaffold inspect` command * add tests
- Loading branch information
1 parent
91c4480
commit d14f34d
Showing
12 changed files
with
361 additions
and
9 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
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
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
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,42 @@ | ||
/* | ||
Copyright 2021 The Skaffold 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" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
var inspectFlags = struct { | ||
fileName string | ||
outFormat string | ||
}{ | ||
fileName: "skaffold.yaml", | ||
} | ||
|
||
func NewCmdInspect() *cobra.Command { | ||
return NewCmd("inspect"). | ||
WithDescription("Helper commands for Cloud Code IDEs to interact with and modify skaffold configuration files."). | ||
WithPersistentFlagAdder(cmdInspectFlags). | ||
Hidden(). | ||
WithCommands(cmdModules()) | ||
} | ||
|
||
func cmdInspectFlags(f *pflag.FlagSet) { | ||
f.StringVarP(&inspectFlags.fileName, "filename", "f", "skaffold.yaml", "Path to the local Skaffold config file. Defaults to `skaffold.yaml`") | ||
f.StringVarP(&inspectFlags.outFormat, "format", "o", "json", "Output format. One of: json(default)") | ||
} |
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,43 @@ | ||
/* | ||
Copyright 2021 The Skaffold 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 ( | ||
"context" | ||
"io" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/inspect" | ||
) | ||
|
||
func cmdModules() *cobra.Command { | ||
return NewCmd("modules"). | ||
WithDescription("Interact with configuration modules"). | ||
WithCommands(cmdModulesList()) | ||
} | ||
|
||
func cmdModulesList() *cobra.Command { | ||
return NewCmd("list"). | ||
WithExample("Get list of modules", "skaffold inspect modules list --format json"). | ||
WithDescription("Print the list of module names that can be invoked with the --module flag in other skaffold commands."). | ||
NoArgs(listModules) | ||
} | ||
|
||
func listModules(ctx context.Context, out io.Writer) error { | ||
return inspect.PrintModulesList(ctx, out, inspect.Options{Filename: inspectFlags.fileName, OutFormat: inspectFlags.outFormat}) | ||
} |
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
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,54 @@ | ||
/* | ||
Copyright 2021 The Skaffold 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 inspect | ||
|
||
import ( | ||
"context" | ||
"io" | ||
|
||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/parser" | ||
) | ||
|
||
var ( | ||
getConfigSetFunc = parser.GetConfigSet | ||
) | ||
|
||
type moduleList struct { | ||
Modules []moduleEntry `json:"modules"` | ||
} | ||
|
||
type moduleEntry struct { | ||
Name string `json:"name"` | ||
Path string `json:"path"` | ||
} | ||
|
||
func PrintModulesList(ctx context.Context, out io.Writer, opts Options) error { | ||
formatter := getOutputFormatter(out, opts.OutFormat) | ||
cfgs, err := getConfigSetFunc(config.SkaffoldOptions{ConfigurationFile: opts.Filename}) | ||
if err != nil { | ||
return formatter.WriteErr(err) | ||
} | ||
|
||
l := &moduleList{} | ||
for _, c := range cfgs { | ||
if c.Metadata.Name != "" { | ||
l.Modules = append(l.Modules, moduleEntry{Name: c.Metadata.Name, Path: c.SourceFile}) | ||
} | ||
} | ||
return formatter.Write(l) | ||
} |
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,71 @@ | ||
/* | ||
Copyright 2021 The Skaffold 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 inspect | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/parser" | ||
sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/errors" | ||
v1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1" | ||
"github.com/GoogleContainerTools/skaffold/testutil" | ||
) | ||
|
||
func TestPrintModulesList(t *testing.T) { | ||
tests := []struct { | ||
description string | ||
configSet parser.SkaffoldConfigSet | ||
err error | ||
expected string | ||
}{ | ||
{ | ||
description: "print modules", | ||
configSet: parser.SkaffoldConfigSet{ | ||
&parser.SkaffoldConfigEntry{SkaffoldConfig: &v1.SkaffoldConfig{Metadata: v1.Metadata{Name: "cfg1"}}, SourceFile: "path/to/cfg1"}, | ||
&parser.SkaffoldConfigEntry{SkaffoldConfig: &v1.SkaffoldConfig{Metadata: v1.Metadata{Name: "cfg2"}}, SourceFile: "path/to/cfg2"}, | ||
}, | ||
expected: `{"modules":[{"name":"cfg1","path":"path/to/cfg1"},{"name":"cfg2","path":"path/to/cfg2"}]}` + "\n", | ||
}, | ||
{ | ||
description: "actionable error", | ||
err: sErrors.MainConfigFileNotFoundErr("path/to/skaffold.yaml", fmt.Errorf("failed to read file : %q", "skaffold.yaml")), | ||
expected: `{"errorCode":"CONFIG_FILE_NOT_FOUND_ERR","errorMessage":"unable to find configuration file \"path/to/skaffold.yaml\": failed to read file : \"skaffold.yaml\". Check that the specified configuration file exists at \"path/to/skaffold.yaml\"."}` + "\n", | ||
}, | ||
{ | ||
description: "generic error", | ||
err: errors.New("some error occurred"), | ||
expected: `{"errorCode":"UNKNOWN_ERROR","errorMessage":"some error occurred"}` + "\n", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
testutil.Run(t, test.description, func(t *testutil.T) { | ||
t.Override(&getConfigSetFunc, func(config.SkaffoldOptions) (parser.SkaffoldConfigSet, error) { | ||
return test.configSet, test.err | ||
}) | ||
var buf bytes.Buffer | ||
err := PrintModulesList(context.Background(), &buf, Options{OutFormat: "json"}) | ||
t.CheckNoError(err) | ||
t.CheckDeepEqual(test.expected, buf.String()) | ||
}) | ||
} | ||
} |
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,60 @@ | ||
/* | ||
Copyright 2021 The Skaffold 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 inspect | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
|
||
sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/errors" | ||
"github.com/GoogleContainerTools/skaffold/proto/v1" | ||
) | ||
|
||
type formatter interface { | ||
Write(interface{}) error | ||
WriteErr(error) error | ||
} | ||
|
||
func getOutputFormatter(out io.Writer, _ string) formatter { | ||
// TODO: implement other output formatters. Currently only JSON is implemented | ||
return jsonFormatter{out: out} | ||
} | ||
|
||
type jsonFormatter struct { | ||
out io.Writer | ||
} | ||
|
||
func (j jsonFormatter) Write(data interface{}) error { | ||
return json.NewEncoder(j.out).Encode(data) | ||
} | ||
|
||
type jsonErrorOutput struct { | ||
ErrorCode string `json:"errorCode"` | ||
ErrorMessage string `json:"errorMessage"` | ||
} | ||
|
||
func (j jsonFormatter) WriteErr(err error) error { | ||
var sErr sErrors.Error | ||
var jsonErr jsonErrorOutput | ||
if errors.As(err, &sErr) { | ||
jsonErr = jsonErrorOutput{ErrorCode: sErr.StatusCode().String(), ErrorMessage: sErr.Error()} | ||
} else { | ||
jsonErr = jsonErrorOutput{ErrorCode: proto.StatusCode_UNKNOWN_ERROR.String(), ErrorMessage: err.Error()} | ||
} | ||
return json.NewEncoder(j.out).Encode(jsonErr) | ||
} |
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,25 @@ | ||
/* | ||
Copyright 2021 The Skaffold 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 inspect | ||
|
||
// Options holds flag values for the various `skaffold inspect` commands | ||
type Options struct { | ||
// Filename is the `skaffold.yaml` file path | ||
Filename string | ||
// OutFormat is the output format. One of: json | ||
OutFormat string | ||
} |
Oops, something went wrong.