Commit ae5b9466 authored by Addshore's avatar Addshore 🏄 Committed by Addshore
Browse files

Name cmd files and var with some consistency

As more command are going to be added to this repo some sort
of order is needed so that the various vars do not end up
conflicting.
Example: when multiple "start" commands end up existing within
the tree.

I chose the structure used in this commit after reading
https://www.linode.com/docs/guides/using-cobra/

Change-Id: I5a8ec1668c36752c5366ef4e629f931206282fce
parent 1c189970
...@@ -29,6 +29,16 @@ Execute the script from any directory with `go run ~/go/src/gerrit.wikimedia.org ...@@ -29,6 +29,16 @@ Execute the script from any directory with `go run ~/go/src/gerrit.wikimedia.org
- `internal/exec`: Wrapper for the main `exec` package, providing easy verbosity etc - `internal/exec`: Wrapper for the main `exec` package, providing easy verbosity etc
- `internal/mediawiki`: Logic interacting with a MediaWiki directory on disk - `internal/mediawiki`: Logic interacting with a MediaWiki directory on disk
### cmd names
No naming structured is enforced in CI but a convention exists that should be followed.
- `root.go` exists as the overall CLI script.
- Top level commands will have their own file in the `cmd` directory, named after the command. Example: `docker.go`.
- Simple sub commands will be defined in those files as vars prefixed with the parent command. For example `dockerStart`.
- Complex sub commands will be split out into their own file. For example `docker_env.go`.
- This is a recursive solution.
### Using a binary ### Using a binary
Make a binary by running `make install` Make a binary by running `make install`
......
...@@ -59,7 +59,7 @@ func mediawikiOrFatal() mediawiki.MediaWiki { ...@@ -59,7 +59,7 @@ func mediawikiOrFatal() mediawiki.MediaWiki {
return MediaWiki return MediaWiki
} }
var startCmd = &cobra.Command{ var dockerStartCmd = &cobra.Command{
Use: "start", Use: "start",
Short: "Start the development environment", Short: "Start the development environment",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
...@@ -156,7 +156,7 @@ var startCmd = &cobra.Command{ ...@@ -156,7 +156,7 @@ var startCmd = &cobra.Command{
}, },
} }
var execCmd = &cobra.Command{ var dockerExecCmd = &cobra.Command{
Use: "exec [service] [command] [args]", Use: "exec [service] [command] [args]",
Short: "Run a command in the specified container", Short: "Run a command in the specified container",
Args: cobra.MinimumNArgs(2), Args: cobra.MinimumNArgs(2),
...@@ -202,7 +202,7 @@ var execCmd = &cobra.Command{ ...@@ -202,7 +202,7 @@ var execCmd = &cobra.Command{
}, },
} }
var destroyCmd = &cobra.Command{ var dockerDestroyCmd = &cobra.Command{
Use: "destroy [service...]", Use: "destroy [service...]",
Short: "destroys the development environment or specified containers", Short: "destroys the development environment or specified containers",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
...@@ -232,7 +232,7 @@ func contains(slice []string, s string) bool { ...@@ -232,7 +232,7 @@ func contains(slice []string, s string) bool {
return false return false
} }
var stopCmd = &cobra.Command{ var dockerStopCmd = &cobra.Command{
Use: "stop", Use: "stop",
Short: "Stop development environment", Short: "Stop development environment",
PreRun: func(cmd *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
...@@ -250,7 +250,7 @@ var stopCmd = &cobra.Command{ ...@@ -250,7 +250,7 @@ var stopCmd = &cobra.Command{
}, },
} }
var statusCmd = &cobra.Command{ var dockerStatusCmd = &cobra.Command{
Use: "status", Use: "status",
Short: "List development environment status", Short: "List development environment status",
PreRun: func(cmd *cobra.Command, args []string) { PreRun: func(cmd *cobra.Command, args []string) {
...@@ -303,17 +303,17 @@ func init() { ...@@ -303,17 +303,17 @@ func init() {
rootCmd.AddCommand(dockerCmd) rootCmd.AddCommand(dockerCmd)
dockerCmd.AddCommand(startCmd) dockerCmd.AddCommand(dockerStartCmd)
dockerCmd.AddCommand(stopCmd) dockerCmd.AddCommand(dockerStopCmd)
dockerCmd.AddCommand(statusCmd) dockerCmd.AddCommand(dockerStatusCmd)
dockerCmd.AddCommand(destroyCmd) dockerCmd.AddCommand(dockerDestroyCmd)
execCmd.Flags().BoolVarP(&Detach, "detach", "d", false, "Detached mode: Run command in the background.") dockerExecCmd.Flags().BoolVarP(&Detach, "detach", "d", false, "Detached mode: Run command in the background.")
execCmd.Flags().BoolVarP(&Privileged, "privileged", "p", false, "Give extended privileges to the process.") dockerExecCmd.Flags().BoolVarP(&Privileged, "privileged", "p", false, "Give extended privileges to the process.")
execCmd.Flags().StringVarP(&User, "user", "u", "", "Run the command as this user.") dockerExecCmd.Flags().StringVarP(&User, "user", "u", "", "Run the command as this user.")
execCmd.Flags().BoolVarP(&NoTTY, "TTY", "T", false, "Disable pseudo-tty allocation. By default a TTY is allocated") dockerExecCmd.Flags().BoolVarP(&NoTTY, "TTY", "T", false, "Disable pseudo-tty allocation. By default a TTY is allocated")
execCmd.Flags().StringVarP(&Index, "index", "i", "", "Index of the container if there are multiple instances of a service [default: 1]") dockerExecCmd.Flags().StringVarP(&Index, "index", "i", "", "Index of the container if there are multiple instances of a service [default: 1]")
execCmd.Flags().StringSliceVarP(&Env, "env", "e", []string{}, "Set environment variables. Can be used multiple times") dockerExecCmd.Flags().StringSliceVarP(&Env, "env", "e", []string{}, "Set environment variables. Can be used multiple times")
execCmd.Flags().StringVarP(&Workdir, "workdir", "w", "", "Path to workdir directory for this command.") dockerExecCmd.Flags().StringVarP(&Workdir, "workdir", "w", "", "Path to workdir directory for this command.")
dockerCmd.AddCommand(execCmd) dockerCmd.AddCommand(dockerExecCmd)
} }
...@@ -25,13 +25,13 @@ import ( ...@@ -25,13 +25,13 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var envCmd = &cobra.Command{ var dockerEnvCmd = &cobra.Command{
Use: "env", Use: "env",
Short: "Provides subcommands for interacting with development environment variables", Short: "Provides subcommands for interacting with development environment variables",
RunE: nil, RunE: nil,
} }
var deleteCmd = &cobra.Command{ var dockerEnvDeleteCmd = &cobra.Command{
Use: "delete [name]", Use: "delete [name]",
Short: "Deletes an environment variable", Short: "Deletes an environment variable",
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
...@@ -40,7 +40,7 @@ var deleteCmd = &cobra.Command{ ...@@ -40,7 +40,7 @@ var deleteCmd = &cobra.Command{
}, },
} }
var setCmd = &cobra.Command{ var dockerEnvSetCmd = &cobra.Command{
Use: "set [name] [value]", Use: "set [name] [value]",
Short: "Set an environment variable", Short: "Set an environment variable",
Args: cobra.MinimumNArgs(2), Args: cobra.MinimumNArgs(2),
...@@ -49,7 +49,7 @@ var setCmd = &cobra.Command{ ...@@ -49,7 +49,7 @@ var setCmd = &cobra.Command{
}, },
} }
var getCmd = &cobra.Command{ var dockerEnvGetCmd = &cobra.Command{
Use: "get [name]", Use: "get [name]",
Short: "Get an environment variable", Short: "Get an environment variable",
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
...@@ -58,7 +58,7 @@ var getCmd = &cobra.Command{ ...@@ -58,7 +58,7 @@ var getCmd = &cobra.Command{
}, },
} }
var listCmd = &cobra.Command{ var dockerEnvListCmd = &cobra.Command{
Use: "list", Use: "list",
Short: "List all environment variables", Short: "List all environment variables",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
...@@ -68,7 +68,7 @@ var listCmd = &cobra.Command{ ...@@ -68,7 +68,7 @@ var listCmd = &cobra.Command{
}, },
} }
var whereCmd = &cobra.Command{ var dockerEnvWhereCmd = &cobra.Command{
Use: "where", Use: "where",
Short: "Output the location of the .env file", Short: "Output the location of the .env file",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
...@@ -77,11 +77,11 @@ var whereCmd = &cobra.Command{ ...@@ -77,11 +77,11 @@ var whereCmd = &cobra.Command{
} }
func init() { func init() {
dockerCmd.AddCommand(envCmd) dockerCmd.AddCommand(dockerEnvCmd)
envCmd.AddCommand(whereCmd) dockerEnvCmd.AddCommand(dockerEnvWhereCmd)
envCmd.AddCommand(setCmd) dockerEnvCmd.AddCommand(dockerEnvSetCmd)
envCmd.AddCommand(getCmd) dockerEnvCmd.AddCommand(dockerEnvGetCmd)
envCmd.AddCommand(listCmd) dockerEnvCmd.AddCommand(dockerEnvListCmd)
envCmd.AddCommand(deleteCmd) dockerEnvCmd.AddCommand(dockerEnvDeleteCmd)
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment