Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLI] cobra lots of things #14007

Merged
merged 12 commits into from
Sep 22, 2023
Merged
68 changes: 68 additions & 0 deletions go/cmd/vtaclcheck/cli/vtactlcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2023 The Vitess 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 cli

import (
"github.com/spf13/cobra"

"vitess.io/vitess/go/acl"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/vtaclcheck"
)

var (
aclFile string
staticAuthFile string

Main = &cobra.Command{
Use: "vtaclcheck",
Short: "vtaclcheck checks that the access-control list (ACL) rules in a given file are valid.",
Args: cobra.NoArgs,
Version: servenv.AppVersion.String(),
PreRunE: servenv.CobraPreRunE,
PostRun: func(cmd *cobra.Command, args []string) {
logutil.Flush()
},
RunE: run,
}
)

func run(cmd *cobra.Command, args []string) error {
servenv.Init()
defer servenv.Close()

opts := &vtaclcheck.Options{
ACLFile: aclFile,
StaticAuthFile: staticAuthFile,
}

if err := vtaclcheck.Init(opts); err != nil {
return err
}

return vtaclcheck.Run()
}

func init() {
servenv.MoveFlagsToCobraCommand(Main)

Main.Flags().StringVar(&aclFile, "acl-file", aclFile, "The path of the JSON ACL file to check")
Main.Flags().StringVar(&staticAuthFile, "static-auth-file", staticAuthFile, "The path of the auth_server_static JSON file to check")

acl.RegisterFlags(Main.Flags())
}
37 changes: 37 additions & 0 deletions go/cmd/vtaclcheck/docgen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2023 The Vitess 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 main

import (
"github.com/spf13/cobra"

"vitess.io/vitess/go/cmd/internal/docgen"
"vitess.io/vitess/go/cmd/vtaclcheck/cli"
)

func main() {
var dir string
cmd := cobra.Command{
Use: "docgen [-d <dir>]",
RunE: func(cmd *cobra.Command, args []string) error {
return docgen.GenerateMarkdownTree(cli.Main, dir)
},
}

cmd.Flags().StringVarP(&dir, "dir", "d", "doc", "output directory to write documentation")
_ = cmd.Execute()
}
37 changes: 3 additions & 34 deletions go/cmd/vtaclcheck/vtaclcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,52 +19,21 @@ package main
import (
"fmt"

"github.com/spf13/pflag"

"vitess.io/vitess/go/acl"
"vitess.io/vitess/go/cmd/vtaclcheck/cli"
"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/vtaclcheck"
)

var aclFile, staticAuthFile string

func init() {
logger := logutil.NewConsoleLogger()
servenv.OnParse(func(fs *pflag.FlagSet) {
fs.StringVar(&aclFile, "acl-file", aclFile, "The path of the JSON ACL file to check")
fs.StringVar(&staticAuthFile, "static-auth-file", staticAuthFile, "The path of the auth_server_static JSON file to check")

acl.RegisterFlags(fs)

fs.SetOutput(logutil.NewLoggerWriter(logger))
})
cli.Main.SetOutput(logutil.NewLoggerWriter(logger))
}

func main() {
defer exit.RecoverAll()
defer logutil.Flush()

servenv.ParseFlags("vtaclcheck")
servenv.Init()

err := run()
if err != nil {
if err := cli.Main.Execute(); err != nil {
fmt.Printf("ERROR: %s\n", err)
exit.Return(1)
}
}

func run() error {
opts := &vtaclcheck.Options{
ACLFile: aclFile,
StaticAuthFile: staticAuthFile,
}

if err := vtaclcheck.Init(opts); err != nil {
return err
}

return vtaclcheck.Run()
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package cli

import (
_ "vitess.io/vitess/go/vt/mysqlctl/azblobbackupstorage"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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 main
package cli

import (
_ "vitess.io/vitess/go/vt/mysqlctl/cephbackupstorage"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package cli

import (
_ "vitess.io/vitess/go/vt/topo/consultopo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package cli

import (
_ "vitess.io/vitess/go/vt/topo/etcd2topo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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 main
package cli

import (
_ "vitess.io/vitess/go/vt/mysqlctl/filebackupstorage"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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 main
package cli

import (
_ "vitess.io/vitess/go/vt/mysqlctl/gcsbackupstorage"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package cli

import "vitess.io/vitess/go/stats/opentsdb"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package cli

// This plugin imports Prometheus to allow for instrumentation
// with the Prometheus client library
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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 main
package cli

import (
_ "vitess.io/vitess/go/vt/mysqlctl/s3backupstorage"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package cli

import (
_ "vitess.io/vitess/go/vt/topo/zk2topo"
Expand Down
Loading
Loading