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
pull/2/head
Michael Gaffney 6 years ago committed by GitHub
parent 39c629631f
commit 5e5a56cac0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -27,7 +27,7 @@ const (
// a string.
var reRemoveWhitespace = regexp.MustCompile(`[\s]+`)
type BaseCommand struct {
type Command struct {
UI cli.Ui
Address string
Context context.Context
@ -47,7 +47,7 @@ type BaseCommand struct {
flagOutputCurlString bool
}
func (c *BaseCommand) SetAddress(addr string) {
func (c *Command) SetAddress(addr string) {
c.Address = addr
}
@ -62,7 +62,7 @@ const (
// FlagSet creates the flags for this command. The result is cached on the
// command to save performance on future calls.
func (c *BaseCommand) FlagSet(bit FlagSetBit) *FlagSets {
func (c *Command) FlagSet(bit FlagSetBit) *FlagSets {
c.flagsOnce.Do(func() {
set := NewFlagSets(c.UI)

@ -44,7 +44,7 @@ type ServerListener struct {
HTTPServer *http.Server
}
type BaseServer struct {
type Server struct {
InfoKeys []string
Info map[string]string
@ -77,8 +77,8 @@ type BaseServer struct {
dockertestResource *dockertest.Resource
}
func NewBaseServer() *BaseServer {
return &BaseServer{
func NewServer() *Server {
return &Server{
InfoKeys: make([]string, 0, 20),
Info: make(map[string]string),
AllLoggers: make([]hclog.Logger, 0),
@ -88,7 +88,7 @@ func NewBaseServer() *BaseServer {
}
}
func (b *BaseServer) SetupLogging(flagLogLevel, flagLogFormat, configLogLevel, configLogFormat string) error {
func (b *Server) SetupLogging(flagLogLevel, flagLogFormat, configLogLevel, configLogFormat string) error {
b.logOutput = os.Stderr
if b.CombineLogs {
b.logOutput = os.Stdout
@ -131,14 +131,14 @@ func (b *BaseServer) SetupLogging(flagLogLevel, flagLogFormat, configLogLevel, c
return nil
}
func (b *BaseServer) ReleaseLogGate() {
func (b *Server) ReleaseLogGate() {
// Release the log gate.
b.Logger.(hclog.OutputResettable).ResetOutputWithFlush(&hclog.LoggerOptions{
Output: b.logOutput,
}, b.GatedWriter)
}
func (b *BaseServer) StorePidFile(pidPath string) error {
func (b *Server) StorePidFile(pidPath string) error {
// Quit fast if no pidfile
if pidPath == "" {
return nil
@ -168,14 +168,14 @@ func (b *BaseServer) StorePidFile(pidPath string) error {
return nil
}
func (b *BaseServer) RemovePidFile(pidPath string) error {
func (b *Server) RemovePidFile(pidPath string) error {
if pidPath == "" {
return nil
}
return os.Remove(pidPath)
}
func (b *BaseServer) SetupMetrics(ui cli.Ui, telemetry *configutil.Telemetry) error {
func (b *Server) SetupMetrics(ui cli.Ui, telemetry *configutil.Telemetry) error {
// TODO: Figure out a user-agent we want to use for the last param
// TODO: Do we want different names for different components?
var err error
@ -187,9 +187,9 @@ func (b *BaseServer) SetupMetrics(ui cli.Ui, telemetry *configutil.Telemetry) er
return nil
}
func (b *BaseServer) PrintInfo(ui cli.Ui, mode string) {
func (b *Server) PrintInfo(ui cli.Ui, mode string) {
b.InfoKeys = append(b.InfoKeys, "version")
verInfo := version.GetVersion()
verInfo := version.Get()
b.Info["version"] = verInfo.FullVersionNumber(false)
if verInfo.Revision != "" {
b.Info["version sha"] = strings.Trim(verInfo.Revision, "'")
@ -220,7 +220,7 @@ func (b *BaseServer) PrintInfo(ui cli.Ui, mode string) {
}
}
func (b *BaseServer) SetupListeners(ui cli.Ui, config *configutil.SharedConfig) error {
func (b *Server) SetupListeners(ui cli.Ui, config *configutil.SharedConfig) error {
// Initialize the listeners
b.Listeners = make([]*ServerListener, 0, len(config.Listeners))
// Make sure we close everything before we exit
@ -250,7 +250,7 @@ func (b *BaseServer) SetupListeners(ui cli.Ui, config *configutil.SharedConfig)
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
}
lnMux, props, reloadFunc, err := listener.NewListener(lnConfig, b.GatedWriter, ui)
lnMux, props, reloadFunc, err := listener.New(lnConfig, b.GatedWriter, ui)
if err != nil {
return fmt.Errorf("Error initializing listener of type %s: %w", lnConfig.Type, err)
}
@ -306,7 +306,7 @@ func (b *BaseServer) SetupListeners(ui cli.Ui, config *configutil.SharedConfig)
return nil
}
func (b *BaseServer) SetupKMSes(ui cli.Ui, config *configutil.SharedConfig, size int) error {
func (b *Server) SetupKMSes(ui cli.Ui, config *configutil.SharedConfig, size int) error {
switch len(config.Seals) {
case size:
for _, kms := range config.Seals {
@ -371,7 +371,7 @@ func (b *BaseServer) SetupKMSes(ui cli.Ui, config *configutil.SharedConfig, size
return nil
}
func (b *BaseServer) RunShutdownFuncs(ui cli.Ui) {
func (b *Server) RunShutdownFuncs(ui cli.Ui) {
for _, f := range b.ShutdownFuncs {
if err := f(); err != nil {
ui.Error(fmt.Sprintf("Error running a shutdown task: %s", err.Error()))
@ -379,7 +379,7 @@ func (b *BaseServer) RunShutdownFuncs(ui cli.Ui) {
}
}
func (b *BaseServer) CreateDevDatabase() error {
func (b *Server) CreateDevDatabase() error {
pool, err := dockertest.NewPool("")
if err != nil {
return fmt.Errorf("could not connect to docker: %w", err)
@ -420,7 +420,7 @@ func (b *BaseServer) CreateDevDatabase() error {
return nil
}
func (b *BaseServer) DestroyDevDatabase() error {
func (b *Server) DestroyDevDatabase() error {
if b.dockertestPool == nil {
return nil
}

@ -16,8 +16,8 @@ var Commands map[string]cli.CommandFactory
func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) {
/*
getBaseCommand := func() *base.BaseCommand {
return &base.BaseCommand{
getBaseCommand := func() *base.Command {
return &base.Command{
UI: ui,
Address: runOpts.Address,
}
@ -26,8 +26,8 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) {
Commands = map[string]cli.CommandFactory{
"controller": func() (cli.Command, error) {
return &controller.ControllerCommand{
BaseCommand: &base.BaseCommand{
return &controller.Command{
Command: &base.Command{
UI: serverCmdUi,
Address: runOpts.Address,
},
@ -37,8 +37,8 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) {
}, nil
},
"dev": func() (cli.Command, error) {
return &dev.DevCommand{
BaseCommand: &base.BaseCommand{
return &dev.Command{
Command: &base.Command{
UI: serverCmdUi,
Address: runOpts.Address,
},

@ -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
}

@ -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)

@ -13,14 +13,14 @@ import (
"github.com/posener/complete"
)
var _ cli.Command = (*DevCommand)(nil)
var _ cli.CommandAutocomplete = (*DevCommand)(nil)
var _ cli.Command = (*Command)(nil)
var _ cli.CommandAutocomplete = (*Command)(nil)
var memProfilerEnabled = false
type DevCommand struct {
*base.BaseCommand
*base.BaseServer
type Command struct {
*base.Command
*base.Server
ShutdownCh chan struct{}
SighupCh chan struct{}
@ -39,11 +39,11 @@ type DevCommand struct {
flagCombineLogs bool
}
func (c *DevCommand) Synopsis() string {
func (c *Command) Synopsis() string {
return "Start a Watchtower dev environment"
}
func (c *DevCommand) Help() string {
func (c *Command) Help() string {
helpText := `
Usage: watchtower dev [options]
@ -57,7 +57,7 @@ Usage: watchtower dev [options]
return strings.TrimSpace(helpText)
}
func (c *DevCommand) Flags() *base.FlagSets {
func (c *Command) Flags() *base.FlagSets {
set := c.FlagSet(base.FlagSetHTTP)
f := set.NewFlagSet("Command Options")
@ -109,16 +109,16 @@ func (c *DevCommand) Flags() *base.FlagSets {
return set
}
func (c *DevCommand) AutocompleteArgs() complete.Predictor {
func (c *Command) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}
func (c *DevCommand) AutocompleteFlags() complete.Flags {
func (c *Command) AutocompleteFlags() complete.Flags {
return c.Flags().Completions()
}
func (c *DevCommand) Run(args []string) int {
c.BaseServer = base.NewBaseServer()
func (c *Command) Run(args []string) int {
c.Server = base.NewServer()
c.CombineLogs = c.flagCombineLogs
var err error
@ -132,7 +132,7 @@ func (c *DevCommand) Run(args []string) int {
childShutdownCh := make(chan struct{})
devControllerConfig, err := controllerconfig.DevConfig()
devControllerConfig, err := controllerconfig.Dev()
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating controller dev config: %s", err))
return 1
@ -223,12 +223,12 @@ func (c *DevCommand) Run(args []string) int {
c.childSighupCh = append(c.childSighupCh, controllerSighupCh)
go func() {
defer shutdownWg.Done()
devController := &controllercmd.ControllerCommand{
BaseCommand: c.BaseCommand,
BaseServer: c.BaseServer,
ShutdownCh: childShutdownCh,
SighupCh: controllerSighupCh,
Config: devControllerConfig,
devController := &controllercmd.Command{
Command: c.Command,
Server: c.Server,
ShutdownCh: childShutdownCh,
SighupCh: controllerSighupCh,
Config: devControllerConfig,
}
devController.Start()
}()

@ -2,5 +2,5 @@
package dev
func (d *DevCommand) startMemProfiler() {
func (d *Command) startMemProfiler() {
}

@ -14,7 +14,7 @@ func init() {
memProfilerEnabled = true
}
func (d *DevCommand) startMemProfiler() {
func (d *Command) startMemProfiler() {
profileDir := filepath.Join(os.TempDir(), "watchtowerprof")
if err := os.MkdirAll(profileDir, 0700); err != nil {
d.logger.Debug("could not create profile directory", "error", err)

@ -8,7 +8,7 @@ import (
)
type Config struct {
*base.BaseServer
*base.Server
RawConfig *config.Config
BaseContext context.Context
}

@ -16,7 +16,7 @@ type Controller struct {
baseCancel context.CancelFunc
}
func NewController(conf *Config) (*Controller, error) {
func New(conf *Config) (*Controller, error) {
if conf.Logger == nil {
conf.Logger = hclog.New(&hclog.LoggerOptions{
Level: hclog.Trace,

@ -5,15 +5,15 @@ import (
"fmt"
)
// VersionInfo
type VersionInfo struct {
// Info
type Info struct {
Revision string
Version string
VersionPrerelease string
VersionMetadata string
}
func GetVersion() *VersionInfo {
func Get() *Info {
ver := Version
rel := VersionPrerelease
md := VersionMetadata
@ -24,7 +24,7 @@ func GetVersion() *VersionInfo {
rel = "dev"
}
return &VersionInfo{
return &Info{
Revision: GitCommit,
Version: ver,
VersionPrerelease: rel,
@ -32,7 +32,7 @@ func GetVersion() *VersionInfo {
}
}
func (c *VersionInfo) VersionNumber() string {
func (c *Info) VersionNumber() string {
if Version == "unknown" && VersionPrerelease == "unknown" {
return "(version unknown)"
}
@ -50,7 +50,7 @@ func (c *VersionInfo) VersionNumber() string {
return version
}
func (c *VersionInfo) FullVersionNumber(rev bool) string {
func (c *Info) FullVersionNumber(rev bool) string {
var versionString bytes.Buffer
if Version == "unknown" && VersionPrerelease == "unknown" {

Loading…
Cancel
Save