Skip to content

Commit

Permalink
fix: resolve outstanding golint errors (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel committed Jan 23, 2021
1 parent c7b9fb9 commit 7558049
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 36 deletions.
10 changes: 7 additions & 3 deletions cli/cmd/exit_codes.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package cmd

const (
ExSuccess = 0
ExUsage = 64
//ExSuccess is the exit code that signals that everything went as expected
ExSuccess = 0
//ExUsage is the exit code that signals that the application was not started with the correct arguments
ExUsage = 64
//ExUnavailable is the exit code that signals that the application failed to perform the intended task
ExUnavailable = 69
ExConfig = 78
//ExConfig is the exit code that signals that the task failed due to a configuration error
ExConfig = 78
)
2 changes: 1 addition & 1 deletion internal/util/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/spf13/viper"
)

// LoadArgsFromAltSources is a WORKAROUND to make cobra count env vars and positional arguments when checking required flags
// LoadFlagsFromAltSources is a WORKAROUND to make cobra count env vars and positional arguments when checking required flags
func LoadFlagsFromAltSources(cmd *cobra.Command, args []string) {

if len(args) > 0 {
Expand Down
30 changes: 19 additions & 11 deletions pkg/format/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func GetConfigMap(service types.Service) (map[string]string, int) {
return formatter.formatStructMap(configType, config, 0)
}

// GetServiceConfigFormat returns type and field information about a ServiceConfig, resolved from it's Service
func GetServiceConfigFormat(service types.Service) (reflect.Type, []FieldInfo) {
configRef := reflect.ValueOf(service).Elem().FieldByName("config")
configType := configRef.Type().Elem()
Expand All @@ -39,6 +40,7 @@ func GetServiceConfigFormat(service types.Service) (reflect.Type, []FieldInfo) {
return GetConfigFormat(serviceConfig)
}

// GetConfigFormat returns type and field information about a ServiceConfig
func GetConfigFormat(serviceConfig types.ServiceConfig) (reflect.Type, []FieldInfo) {
configType := reflect.TypeOf(serviceConfig)
if configType.Kind() == reflect.Ptr {
Expand Down Expand Up @@ -135,6 +137,7 @@ func (fmtr *formatter) formatStructMap(structType reflect.Type, structItem inter
return valueMap, maxKeyLen
}

// FieldInfo is the meta data about a config field
type FieldInfo struct {
Name string
Type reflect.Type
Expand Down Expand Up @@ -281,6 +284,7 @@ func (fmtr *formatter) getFieldValueString(field reflect.Value, depth uint8) (st
return fmt.Sprintf("<?%s>", strVal), len(strVal) + 5
}

// SetConfigField deserializes the inputValue and sets the field of a config to that value
func SetConfigField(config reflect.Value, field FieldInfo, inputValue string) (valid bool, err error) {
configField := config.FieldByName(field.Name)
fieldKind := field.Type.Kind()
Expand All @@ -293,10 +297,11 @@ func SetConfigField(config reflect.Value, field FieldInfo, inputValue string) (v
if value == EnumInvalid {
enumNames := strings.Join(field.EnumFormatter.Names(), ", ")
return false, fmt.Errorf("not a one of %v", enumNames)
} else {
configField.SetInt(int64(value))
return true, nil
}

configField.SetInt(int64(value))
return true, nil

} else if fieldKind >= reflect.Uint && fieldKind <= reflect.Uint64 {
var value uint64
value, err = strconv.ParseUint(inputValue, 10, field.Type.Bits())
Expand All @@ -312,26 +317,29 @@ func SetConfigField(config reflect.Value, field FieldInfo, inputValue string) (v
return true, nil
}
} else if fieldKind == reflect.Bool {
if value, ok := ParseBool(inputValue, false); !ok {
value, ok := ParseBool(inputValue, false)
if !ok {
return false, errors.New("accepted values are 1, true, yes or 0, false, no")
} else {
configField.SetBool(value)
return true, nil
}

configField.SetBool(value)
return true, nil
} else if fieldKind >= reflect.Slice {
elemKind := field.Type.Elem().Kind()
if elemKind != reflect.String {
return false, errors.New("field format is not supported")
} else {
values := strings.Split(inputValue, ",")
configField.Set(reflect.ValueOf(values))
return true, nil
}

values := strings.Split(inputValue, ",")
configField.Set(reflect.ValueOf(values))
return true, nil

}
return false, nil

}

// GetConfigFieldString serializes the config field value to a string representation
func GetConfigFieldString(config reflect.Value, field FieldInfo) (value string, err error) {
configField := config.FieldByName(field.Name)
fieldKind := field.Type.Kind()
Expand Down
2 changes: 2 additions & 0 deletions pkg/generators/basic/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"reflect"
)

// Generator is the Basic Generator implementation
type Generator struct{}

// Generate generates a service URL from a set of user questions/answers
func (g *Generator) Generate(service types.Service, props map[string]string, _ []string) (types.ServiceConfig, error) {
fmt.Println("Enter the configuration values as prompted")
fmt.Println()
Expand Down
4 changes: 3 additions & 1 deletion pkg/generators/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
)

var generatorMap = map[string]func() t.Generator{
"basic": func() t.Generator { return &basic.Generator{} },
"basic": func() t.Generator { return &basic.Generator{} },
"oauth2": func() t.Generator { return &xouath2.Generator{} },
}

// NewGenerator creates an instance of the generator that corresponds to the provided identifier
func NewGenerator(identifier string) (t.Generator, error) {
generatorFactory, valid := generatorMap[strings.ToLower(identifier)]
if !valid {
Expand All @@ -21,6 +22,7 @@ func NewGenerator(identifier string) (t.Generator, error) {
return generatorFactory(), nil
}

// ListGenerators lists all available generators
func ListGenerators() []string {
generators := make([]string, len(generatorMap))

Expand Down
29 changes: 15 additions & 14 deletions pkg/generators/xouath2/xoauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"strings"
)

// Generator is the XOAuth2 Generator implementation
type Generator struct{}

// Generate generates a service URL from a set of user questions/answers
func (g *Generator) Generate(_ types.Service, props map[string]string, args []string) (types.ServiceConfig, error) {

if provider, found := props["provider"]; found {
Expand All @@ -24,10 +26,9 @@ func (g *Generator) Generate(_ types.Service, props map[string]string, args []st

if len(args) > 0 {
return oauth2GeneratorFile(args[0])
} else {
return oauth2Generator()
}

return oauth2Generator()
}

func oauth2GeneratorFile(file string) (*smtp.Config, error) {
Expand Down Expand Up @@ -67,9 +68,9 @@ func oauth2GeneratorFile(file string) (*smtp.Config, error) {

func oauth2Generator() (*smtp.Config, error) {

var clientId string
var clientID string
fmt.Print("ClientID: ")
_, err := fmt.Scanln(&clientId)
_, err := fmt.Scanln(&clientID)
if err != nil {
return nil, err
}
Expand All @@ -81,23 +82,23 @@ func oauth2Generator() (*smtp.Config, error) {
return nil, err
}

var authUrl string
var authURL string
fmt.Print("AuthURL: ")
_, err = fmt.Scanln(&authUrl)
_, err = fmt.Scanln(&authURL)
if err != nil {
return nil, err
}

var tokenUrl string
var tokenURL string
fmt.Print("TokenURL: ")
_, err = fmt.Scanln(&tokenUrl)
_, err = fmt.Scanln(&tokenURL)
if err != nil {
return nil, err
}

var redirectUrl string
var redirectURL string
fmt.Print("RedirectURL: ")
_, err = fmt.Scanln(&redirectUrl)
_, err = fmt.Scanln(&redirectURL)
if err != nil {
return nil, err
}
Expand All @@ -117,14 +118,14 @@ func oauth2Generator() (*smtp.Config, error) {
}

conf := oauth2.Config{
ClientID: clientId,
ClientID: clientID,
ClientSecret: clientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: authUrl,
TokenURL: tokenUrl,
AuthURL: authURL,
TokenURL: tokenURL,
AuthStyle: oauth2.AuthStyleAutoDetect,
},
RedirectURL: redirectUrl,
RedirectURL: redirectURL,
Scopes: strings.Split(scopes, ","),
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/services/rocketchat/rocketchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (service *Service) Send(message string, params *types.Params) error {
func buildURL(config *Config) string {
if config.Port != "" {
return fmt.Sprintf("https://%s:%s/hooks/%s/%s", config.Host, config.Port, config.TokenA, config.TokenB)
} else {
return fmt.Sprintf("https://%s/hooks/%s/%s", config.Host, config.TokenA, config.TokenB)
}

return fmt.Sprintf("https://%s/hooks/%s/%s", config.Host, config.TokenA, config.TokenB)
}
4 changes: 2 additions & 2 deletions pkg/services/smtp/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ func (service *Service) Send(message string, params *types.Params) error {
config := service.config.Clone()
if err := service.propKeyResolver.UpdateConfigFromParams(&config, params); err != nil {
return fail(FailApplySendParams, err)
} else {
return service.doSend(client, message, &config)
}

return service.doSend(client, message, &config)
}

func getClientConnection(config *Config) (*smtp.Client, error) {
Expand Down
9 changes: 7 additions & 2 deletions pkg/services/smtp/smtp_encmethod.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ import (
type encMethod int

type encMethodVals struct {
None encMethod
// None means no encryption
None encMethod
// ExplicitTLS means that TLS needs to be initiated by using StartTLS
ExplicitTLS encMethod
// ImplicitTLS means that TLS is used for the whole session
ImplicitTLS encMethod
Auto encMethod
// Auto means that TLS will be implicitly used for port 465, otherwise explicit TLS will be used if its supported
Auto encMethod

// Enum is the EnumFormatter instance for EncMethods
Enum types.EnumFormatter
}

Expand Down
1 change: 1 addition & 0 deletions pkg/services/standard/standard_failures.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
FailUnknown f.FailureID = iota
)

// Failure creates a Failure instance corresponding to the provided failureID, wrapping the provided error
func Failure(failureID f.FailureID, err error, v ...interface{}) f.Failure {
messages := map[int]string{
int(FailParseURL): "error parsing Service URL",
Expand Down
1 change: 1 addition & 0 deletions pkg/types/generator.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package types

// Generator is the interface for tools that generate service configurations from a user dialog
type Generator interface {
Generate(service Service, props map[string]string, args []string) (ServiceConfig, error)
}
1 change: 1 addition & 0 deletions pkg/types/service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type ServiceConfig interface {
Enums() map[string]EnumFormatter
}

// ConfigQueryResolver is the interface used to get/set and list service config query fields
type ConfigQueryResolver interface {
Get(string) (string, error)
Set(string, string) error
Expand Down

0 comments on commit 7558049

Please sign in to comment.