From 257c69e386939fd3d3845607aea0b97d2ad9af32 Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Mon, 19 Sep 2022 13:15:04 -0700 Subject: [PATCH] feat: add shutdown pass-through command (#267) --- cmd/aspect/root/BUILD.bazel | 1 + cmd/aspect/root/root.go | 3 +- cmd/aspect/shutdown/BUILD.bazel | 15 ++++++++++ cmd/aspect/shutdown/shutdown.go | 46 ++++++++++++++++++++++++++++ docs/aspect.md | 1 + docs/aspect_shutdown.md | 29 ++++++++++++++++++ docs/command_list.bzl | 1 + pkg/aspect/shutdown/BUILD.bazel | 14 +++++++++ pkg/aspect/shutdown/shutdown.go | 53 +++++++++++++++++++++++++++++++++ 9 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 cmd/aspect/shutdown/BUILD.bazel create mode 100644 cmd/aspect/shutdown/shutdown.go create mode 100755 docs/aspect_shutdown.md create mode 100644 pkg/aspect/shutdown/BUILD.bazel create mode 100644 pkg/aspect/shutdown/shutdown.go diff --git a/cmd/aspect/root/BUILD.bazel b/cmd/aspect/root/BUILD.bazel index 87484bff1..c3e855c48 100644 --- a/cmd/aspect/root/BUILD.bazel +++ b/cmd/aspect/root/BUILD.bazel @@ -22,6 +22,7 @@ go_library( "//cmd/aspect/modquery", "//cmd/aspect/query", "//cmd/aspect/run", + "//cmd/aspect/shutdown", "//cmd/aspect/sync", "//cmd/aspect/test", "//cmd/aspect/version", diff --git a/cmd/aspect/root/root.go b/cmd/aspect/root/root.go index 184cfd144..0f4994bfa 100644 --- a/cmd/aspect/root/root.go +++ b/cmd/aspect/root/root.go @@ -36,6 +36,7 @@ import ( "aspect.build/cli/cmd/aspect/modquery" "aspect.build/cli/cmd/aspect/query" "aspect.build/cli/cmd/aspect/run" + "aspect.build/cli/cmd/aspect/shutdown" "aspect.build/cli/cmd/aspect/sync" "aspect.build/cli/cmd/aspect/test" "aspect.build/cli/cmd/aspect/version" @@ -91,7 +92,7 @@ func NewRootCmd( cmd.AddCommand(query.NewDefaultQueryCmd()) cmd.AddCommand(run.NewDefaultRunCmd(pluginSystem)) cmd.AddCommand(sync.NewDefaultSyncCmd()) - // shutdown + cmd.AddCommand(shutdown.NewDefaultShutdownCmd()) cmd.AddCommand(test.NewDefaultTestCmd(pluginSystem)) cmd.AddCommand(version.NewDefaultVersionCmd()) diff --git a/cmd/aspect/shutdown/BUILD.bazel b/cmd/aspect/shutdown/BUILD.bazel new file mode 100644 index 000000000..c9e622fa9 --- /dev/null +++ b/cmd/aspect/shutdown/BUILD.bazel @@ -0,0 +1,15 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "shutdown", + srcs = ["shutdown.go"], + importpath = "aspect.build/cli/cmd/aspect/shutdown", + visibility = ["//visibility:public"], + deps = [ + "//pkg/aspect/root/flags", + "//pkg/aspect/shutdown", + "//pkg/interceptors", + "//pkg/ioutils", + "@com_github_spf13_cobra//:cobra", + ], +) diff --git a/cmd/aspect/shutdown/shutdown.go b/cmd/aspect/shutdown/shutdown.go new file mode 100644 index 000000000..94f076887 --- /dev/null +++ b/cmd/aspect/shutdown/shutdown.go @@ -0,0 +1,46 @@ +/* + * Copyright 2022 Aspect Build Systems, Inc. + * + * 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 shutdown + +import ( + "github.com/spf13/cobra" + + "aspect.build/cli/pkg/aspect/root/flags" + "aspect.build/cli/pkg/aspect/shutdown" + "aspect.build/cli/pkg/interceptors" + "aspect.build/cli/pkg/ioutils" +) + +func NewDefaultShutdownCmd() *cobra.Command { + return NewShutdownCmd(ioutils.DefaultStreams) +} + +func NewShutdownCmd(streams ioutils.Streams) *cobra.Command { + cmd := &cobra.Command{ + Use: "shutdown", + Short: "Stops the bazel server.", + Long: "This command shuts down the memory resident bazel server process.", + RunE: interceptors.Run( + []interceptors.Interceptor{ + flags.FlagsInterceptor(streams), + }, + shutdown.New(streams).Run, + ), + } + + return cmd +} diff --git a/docs/aspect.md b/docs/aspect.md index 71c05d62e..ef8ddf02f 100644 --- a/docs/aspect.md +++ b/docs/aspect.md @@ -29,6 +29,7 @@ Aspect CLI is a better frontend for running bazel * [aspect modquery](aspect_modquery.md) - Queries the Bzlmod external dependency graph. * [aspect query](aspect_query.md) - Executes a dependency graph query. * [aspect run](aspect_run.md) - Builds the specified target and runs it with the given arguments. +* [aspect shutdown](aspect_shutdown.md) - Stops the bazel server. * [aspect sync](aspect_sync.md) - Syncs all repositories specified in the workspace file. * [aspect test](aspect_test.md) - Builds the specified targets and runs all test targets among them. * [aspect version](aspect_version.md) - Print the version of aspect CLI as well as tools it invokes. diff --git a/docs/aspect_shutdown.md b/docs/aspect_shutdown.md new file mode 100755 index 000000000..a2a8cb7ec --- /dev/null +++ b/docs/aspect_shutdown.md @@ -0,0 +1,29 @@ +## aspect shutdown + +Stops the bazel server. + +### Synopsis + +This command shuts down the memory resident bazel server process. + +``` +aspect shutdown [flags] +``` + +### Options + +``` + -h, --help help for shutdown +``` + +### Options inherited from parent commands + +``` + --aspect:config string config file (default is $HOME/.aspect.yaml) + --aspect:interactive Interactive mode (e.g. prompts for user input) +``` + +### SEE ALSO + +* [aspect](aspect.md) - Aspect.build bazel wrapper + diff --git a/docs/command_list.bzl b/docs/command_list.bzl index 4ad9d2c76..d80ebe809 100644 --- a/docs/command_list.bzl +++ b/docs/command_list.bzl @@ -15,6 +15,7 @@ COMMAND_LIST = [ "modquery", "query", "run", + "shutdown", "sync", "test", "version", diff --git a/pkg/aspect/shutdown/BUILD.bazel b/pkg/aspect/shutdown/BUILD.bazel new file mode 100644 index 000000000..3ec123fa7 --- /dev/null +++ b/pkg/aspect/shutdown/BUILD.bazel @@ -0,0 +1,14 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "shutdown", + srcs = ["shutdown.go"], + importpath = "aspect.build/cli/pkg/aspect/shutdown", + visibility = ["//visibility:public"], + deps = [ + "//pkg/aspecterrors", + "//pkg/bazel", + "//pkg/ioutils", + "@com_github_spf13_cobra//:cobra", + ], +) diff --git a/pkg/aspect/shutdown/shutdown.go b/pkg/aspect/shutdown/shutdown.go new file mode 100644 index 000000000..2cbda0936 --- /dev/null +++ b/pkg/aspect/shutdown/shutdown.go @@ -0,0 +1,53 @@ +/* + * Copyright 2022 Aspect Build Systems, Inc. + * + * 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 shutdown + +import ( + "context" + + "github.com/spf13/cobra" + + "aspect.build/cli/pkg/aspecterrors" + "aspect.build/cli/pkg/bazel" + "aspect.build/cli/pkg/ioutils" +) + +type Shutdown struct { + ioutils.Streams +} + +func New(streams ioutils.Streams) *Shutdown { + return &Shutdown{ + Streams: streams, + } +} + +func (v *Shutdown) Run(ctx context.Context, _ *cobra.Command, args []string) error { + bazelCmd := []string{"shutdown"} + bazelCmd = append(bazelCmd, args...) + bzl := bazel.New() + + if exitCode, err := bzl.Spawn(bazelCmd, v.Streams); exitCode != 0 { + err = &aspecterrors.ExitError{ + Err: err, + ExitCode: exitCode, + } + return err + } + + return nil +}