Refactor names to avoid stutter (#1)

Since client code uses the package name as a prefix when referring to
the package contents, the names for those contents need not repeat the
package name.

- Rename base.BaseCommand to base.Command
- Rename base.BaseServer to base.Server
- Rename config.DevConfig() to config.Dev()
- Rename config.LoadConfigFile() to config.LoadFile()
- Rename config.NewConfig() to config.New()
- Rename config.ParseConfig() to config.Parse()
- Rename controller.ControllerCommand to controller.Command
- Rename controller.NewController() to controller.New()
- Rename dev.DevCommand to dev.Command
- Rename listener.ListenerFactory to listener.Factory
- Rename listener.NewListener() to listener.New()
- Rename version.GetVersion() to version.Get()
- Rename version.VersionInfo to version.Info

See:
- https://golang.org/doc/effective_go.html#package-names
- https://blog.golang.org/package-names#TOC_3.
- https://github.com/golang/go/wiki/CodeReviewComments#package-names
This commit is contained in:
Michael Gaffney authored and GitHub committed 2020-03-10 15:48:04 -04:00
1 parent 39c629631f
commit 5e5a56cac0
14 files changed
+91 -91

No files matched your search

@@ -17,8 +17,8 @@ type Config struct {
*configutil.SharedConfig `hcl:"-"`
}
// DevConfig is a Config that is used for dev mode of Watchtower
func DevConfig() (*Config, error) {
// Dev is a Config that is used for dev mode of Watchtower
func Dev() (*Config, error) {
randBuf := new(bytes.Buffer)
n, err := randBuf.ReadFrom(&io.LimitedReader{
R: rand.Reader,
@@ -61,28 +61,28 @@ kms "aead" {
`
hclStr = fmt.Sprintf(hclStr, controllerKey, workerAuthKey)
parsed, err := ParseConfig(hclStr)
parsed, err := Parse(hclStr)
if err != nil {
return nil, fmt.Errorf("error parsing dev config: %w", err)
}
return parsed, nil
}
func NewConfig() *Config {
func New() *Config {
return &Config{
SharedConfig: new(configutil.SharedConfig),
}
}
// LoadConfigFile loads the configuration from the given file.
func LoadConfigFile(path string) (*Config, error) {
// LoadFile loads the configuration from the given file.
func LoadFile(path string) (*Config, error) {
// Read the file
d, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
conf, err := ParseConfig(string(d))
conf, err := Parse(string(d))
if err != nil {
return nil, err
}
@@ -90,14 +90,14 @@ func LoadConfigFile(path string) (*Config, error) {
return conf, nil
}
func ParseConfig(d string) (*Config, error) {
func Parse(d string) (*Config, error) {
obj, err := hcl.Parse(d)
if err != nil {
return nil, err
}
// Nothing to do here right now
result := NewConfig()
result := New()
if err := hcl.DecodeObject(result, obj); err != nil {
return nil, err
}
+21 -21
View File
@@ -17,14 +17,14 @@ import (
"github.com/posener/complete"
)
var _ cli.Command = (*ControllerCommand)(nil)
var _ cli.CommandAutocomplete = (*ControllerCommand)(nil)
var _ cli.Command = (*Command)(nil)
var _ cli.CommandAutocomplete = (*Command)(nil)
var memProfilerEnabled = false
type ControllerCommand struct {
*base.BaseCommand
*base.BaseServer
type Command struct {
*base.Command
*base.Server
ShutdownCh chan struct{}
SighupCh chan struct{}
@@ -44,11 +44,11 @@ type ControllerCommand struct {
flagCombineLogs bool
}
func (c *ControllerCommand) Synopsis() string {
func (c *Command) Synopsis() string {
return "Start a Watchtower controller"
}
func (c *ControllerCommand) Help() string {
func (c *Command) Help() string {
helpText := `
Usage: watchtower controller [options]
@@ -62,7 +62,7 @@ Usage: watchtower controller [options]
return strings.TrimSpace(helpText)
}
func (c *ControllerCommand) Flags() *base.FlagSets {
func (c *Command) Flags() *base.FlagSets {
set := c.FlagSet(base.FlagSetHTTP)
f := set.NewFlagSet("Command Options")
@@ -131,16 +131,16 @@ func (c *ControllerCommand) Flags() *base.FlagSets {
return set
}
func (c *ControllerCommand) AutocompleteArgs() complete.Predictor {
func (c *Command) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}
func (c *ControllerCommand) AutocompleteFlags() complete.Flags {
func (c *Command) AutocompleteFlags() complete.Flags {
return c.Flags().Completions()
}
func (c *ControllerCommand) Run(args []string) int {
c.BaseServer = base.NewBaseServer()
func (c *Command) Run(args []string) int {
c.Server = base.NewServer()
c.CombineLogs = c.flagCombineLogs
if result := c.ParseFlagsAndConfig(args); result > 0 {
@@ -209,7 +209,7 @@ func (c *ControllerCommand) Run(args []string) int {
return c.Start()
}
func (c *ControllerCommand) ParseFlagsAndConfig(args []string) int {
func (c *Command) ParseFlagsAndConfig(args []string) int {
var err error
f := c.Flags()
@@ -236,14 +236,14 @@ func (c *ControllerCommand) ParseFlagsAndConfig(args []string) int {
c.UI.Error("Must supply a config file with -config")
return 1
}
c.Config, err = config.LoadConfigFile(c.flagConfig)
c.Config, err = config.LoadFile(c.flagConfig)
if err != nil {
c.UI.Error("Error parsing config: " + err.Error())
return 1
}
} else {
c.Config, err = config.DevConfig()
c.Config, err = config.Dev()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating dev config: %s", err))
return 1
@@ -257,15 +257,15 @@ func (c *ControllerCommand) ParseFlagsAndConfig(args []string) int {
return 0
}
func (c *ControllerCommand) Start() int {
func (c *Command) Start() int {
// Instantiate the wait group
controllerConfig := &controller.Config{
RawConfig: c.Config,
BaseServer: c.BaseServer,
RawConfig: c.Config,
Server: c.Server,
}
// Initialize the core
controller, err := controller.NewController(controllerConfig)
controller, err := controller.New(controllerConfig)
if err != nil {
c.UI.Error(fmt.Sprintf("Error initializing controller: %w", err))
return 1
@@ -305,7 +305,7 @@ func (c *ControllerCommand) Start() int {
goto RUNRELOADFUNCS
}
newConf, err = config.LoadConfigFile(c.flagConfig)
newConf, err = config.LoadFile(c.flagConfig)
if err != nil {
c.Logger.Error("could not reload config", "path", c.flagConfig, "error", err)
goto RUNRELOADFUNCS
@@ -355,7 +355,7 @@ func (c *ControllerCommand) Start() int {
return 0
}
func (c *ControllerCommand) Reload() error {
func (c *Command) Reload() error {
c.ReloadFuncsLock.RLock()
defer c.ReloadFuncsLock.RUnlock()
@@ -2,5 +2,5 @@
package controller
func (c *ControllerCommand) startMemProfiler() {
func (c *Command) startMemProfiler() {
}
@@ -14,7 +14,7 @@ func init() {
memProfilerEnabled = true
}
func (c *ControllerCommand) startMemProfiler() {
func (c *Command) startMemProfiler() {
profileDir := filepath.Join(os.TempDir(), "watchtowerprof")
if err := os.MkdirAll(profileDir, 0700); err != nil {
c.logger.Debug("could not create profile directory", "error", err)
@@ -18,17 +18,17 @@ import (
"github.com/mitchellh/cli"
)
// ListenerFactory is the factory function to create a listener.
type ListenerFactory func(*configutil.Listener, io.Writer, cli.Ui) (*alpnmux.ALPNMux, map[string]string, reloadutil.ReloadFunc, error)
// Factory is the factory function to create a listener.
type Factory func(*configutil.Listener, io.Writer, cli.Ui) (*alpnmux.ALPNMux, map[string]string, reloadutil.ReloadFunc, error)
// BuiltinListeners is the list of built-in listener types.
var BuiltinListeners = map[string]ListenerFactory{
var BuiltinListeners = map[string]Factory{
"tcp": tcpListenerFactory,
}
// NewListener creates a new listener of the given type with the given
// New creates a new listener of the given type with the given
// configuration. The type is looked up in the BuiltinListeners map.
func NewListener(l *configutil.Listener, w io.Writer, ui cli.Ui) (*alpnmux.ALPNMux, map[string]string, reloadutil.ReloadFunc, error) {
func New(l *configutil.Listener, w io.Writer, ui cli.Ui) (*alpnmux.ALPNMux, map[string]string, reloadutil.ReloadFunc, error) {
f, ok := BuiltinListeners[l.Type]
if !ok {
return nil, nil, nil, fmt.Errorf("unknown listener type: %q", l.Type)