Skip to content

Commit

Permalink
fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jahvon committed Oct 10, 2023
1 parent cb6262c commit b1ecc95
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 84 deletions.
1 change: 1 addition & 0 deletions .github/workflows/validate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ jobs:
with:
version: v1.54.2
skip-pkg-cache: true
only-new-issues: true

# _____ _
# |_ _|__ ___| |_
Expand Down
7 changes: 5 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ linters:
- whitespace # detects leading and trailing whitespace
- zerologlint # detects the wrong usage of zerolog that a user forgets to dispatch zerolog.Event
- decorder # checks declaration order and count of types, constants, variables and functions
- gci # controls golang package import order and makes it always deterministic
#- gci # controls golang package import order and makes it always deterministic - doesn't work as expected
- ginkgolinter # [if you use ginkgo/gomega] enforces standards of using ginkgo and gomega
- interfacebloat # checks the number of methods inside an interface
- tagalign # checks that struct tags are well aligned
Expand Down Expand Up @@ -185,4 +185,7 @@ issues:
- goconst
- gosec
- noctx
- wrapcheck
- wrapcheck
- linters:
- staticcheck
text: "SA5011" # SA5011: Should not use unsafe.Pointer - this throws false positives when a nil check does not result in a return
9 changes: 7 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"errors"
"fmt"

"github.com/spf13/cobra"

"github.com/jahvon/flow/internal/common"
"github.com/jahvon/flow/internal/config"
"github.com/jahvon/flow/internal/io"
"github.com/spf13/cobra"
)

// createCmd represents the create command.
Expand Down Expand Up @@ -53,7 +54,11 @@ var createWorkspaceCmd = &cobra.Command{
}
io.PrintSuccess(fmt.Sprintf("Workspace %s created in %s", name, path))

if cmd.Flag("set").Value.String() == "true" {
set, err := cmd.Flags().GetBool("set")
if err != nil {
io.PrintErrorAndExit(err)
}
if set {
if err := config.SetCurrentWorkspace(rootCfg, name); err != nil {
io.PrintErrorAndExit(err)
}
Expand Down
14 changes: 11 additions & 3 deletions cmd/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package cmd
import (
"fmt"

"github.com/spf13/cobra"

"github.com/jahvon/flow/internal/cmd/flags"
"github.com/jahvon/flow/internal/crypto"
"github.com/jahvon/flow/internal/io"
"github.com/jahvon/flow/internal/vault"
"github.com/spf13/cobra"
)

var vaultCmd = &cobra.Command{
Expand Down Expand Up @@ -67,7 +68,10 @@ var vaultGetCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
reference := args[0]
asPlainText := cmd.Flag(flags.PlainTextFlagName).Value.String() == "true"
asPlainText, err := cmd.Flags().GetBool(flags.PlainTextFlagName)
if err != nil {
io.PrintErrorAndExit(err)
}

v := vault.NewVault()
secret, err := v.GetSecret(reference)
Expand All @@ -89,7 +93,11 @@ var vaultListCmd = &cobra.Command{
Short: "List all secrets in the vault.",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
asPlainText := cmd.Flag(flags.PlainTextFlagName).Value.String() == "true"
asPlainText, err := cmd.Flags().GetBool(flags.PlainTextFlagName)
if err != nil {
io.PrintErrorAndExit(err)
}

v := vault.NewVault()
secrets, err := v.GetAllSecrets()
if err != nil {
Expand Down
97 changes: 57 additions & 40 deletions internal/cmd/executable/executable.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//nolint:cyclop
package executable

import (
"errors"
"strings"

"github.com/spf13/cobra"

"github.com/jahvon/flow/internal/cmd/flags"
"github.com/jahvon/flow/internal/cmd/utils"
"github.com/jahvon/flow/internal/config"
Expand All @@ -13,7 +14,6 @@ import (
"github.com/jahvon/flow/internal/executable/consts"
"github.com/jahvon/flow/internal/io"
"github.com/jahvon/flow/internal/workspace"
"github.com/spf13/cobra"
)

var log = io.Log()
Expand Down Expand Up @@ -70,55 +70,38 @@ func FlagsToExecutableList(cmd *cobra.Command, currentConfig *config.RootConfig)
}
var agentFilter, tagFilter, namespaceFilter *string
if flag := cmd.Flag(flags.AgentTypeFlagName); flag != nil && flag.Changed {
*agentFilter = flag.Value.String()
val := flag.Value.String()
agentFilter = &val
log.Debug().Msgf("only including executables of type %s", *agentFilter)
}
if flag := cmd.Flag(flags.TagFlagName); flag != nil && flag.Changed {
*tagFilter = flag.Value.String()
val := flag.Value.String()
tagFilter = &val
log.Debug().Msgf("only including executables with tag %s", *tagFilter)
}
if flag := cmd.Flag(flags.NamespaceFlagName); flag != nil && flag.Changed {
*namespaceFilter = flag.Value.String()
val := flag.Value.String()
namespaceFilter = &val
log.Debug().Msgf("only including executable within namespace %s", *namespaceFilter)
}

var executables executable.List
collectExecutablesInWorkspace := func(ws, wsPath string) error {
log.Trace().Msgf("searching for executables in workspace %s", ws)
definitions, err := workspace.LoadDefinitions(ws, wsPath)
if err != nil {
return err
} else if len(definitions) == 0 {
log.Debug().Msgf("no definitions found in workspace %s", ws)
return nil
}

if namespaceFilter != nil {
definitions = definitions.FilterByNamespace(*namespaceFilter)
}
for _, definition := range definitions {
defExecutables := definition.Executables
if agentFilter != nil {
defExecutables = defExecutables.FilterByType(consts.AgentType(*agentFilter))
}
if tagFilter == nil || definition.HasTag(*tagFilter) {
executables = append(executables, defExecutables...)
} else {
defExecutables = defExecutables.FilterByTag(*tagFilter)
executables = append(executables, defExecutables...)
}
}
return nil
}

if context == "global" {
for ws, wsPath := range currentConfig.Workspaces {
if err := collectExecutablesInWorkspace(ws, wsPath); err != nil {
executables, err = collectExecutablesInWorkspace(ws, wsPath, agentFilter, tagFilter, namespaceFilter)
if err != nil {
return nil, err
}
}
} else {
if err := collectExecutablesInWorkspace(context, currentConfig.Workspaces[context]); err != nil {
executables, err = collectExecutablesInWorkspace(
context,
currentConfig.Workspaces[context],
agentFilter,
tagFilter,
namespaceFilter,
)
if err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -146,14 +129,48 @@ func SplitExecutableIdentifier(identifier string) (ws, ns, name string, _ error)
}

split = strings.Split(remaining, ":")
if len(split) > 2 {
return "", "", "", errors.New("invalid executable identifier")
} else if len(split) == 2 {
switch len(split) {
case 1:
name = split[0]
case 2:
ns = split[0]
name = split[1]
} else {
name = split[0]
default:
return "", "", "", errors.New("invalid executable identifier")
}

return ws, ns, name, nil
}

func collectExecutablesInWorkspace(
ws, wsPath string,
agentFilter, tagFilter, namespaceFilter *string,
) (executable.List, error) {
log.Trace().Msgf("searching for executables in workspace %s", ws)

var executables executable.List
definitions, err := workspace.LoadDefinitions(ws, wsPath)
if err != nil {
return nil, err
} else if len(definitions) == 0 {
log.Debug().Msgf("no definitions found in workspace %s", ws)
return nil, nil
}

if namespaceFilter != nil {
definitions = definitions.FilterByNamespace(*namespaceFilter)
}
for _, definition := range definitions {
defExecutables := definition.Executables
if agentFilter != nil {
defExecutables = defExecutables.FilterByType(consts.AgentType(*agentFilter))
}
if tagFilter == nil || definition.HasTag(*tagFilter) {
executables = append(executables, defExecutables...)
} else {
defExecutables = defExecutables.FilterByTag(*tagFilter)
executables = append(executables, defExecutables...)
}
}
return executables, nil
}
13 changes: 8 additions & 5 deletions internal/cmd/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
//nolint:cyclop
package utils

import (
"errors"
"fmt"
"strconv"

"github.com/spf13/cobra"

"github.com/jahvon/flow/internal/cmd/flags"
"github.com/jahvon/flow/internal/config"
"github.com/jahvon/flow/internal/io"
"github.com/spf13/cobra"
)

var log = io.Log()
Expand All @@ -34,11 +36,12 @@ func ValidateAndGetContext(cmd *cobra.Command, currentConfig *config.RootConfig)
globalSet := global != nil
wsSet := ws != nil

if (globalSet && *global) && (wsSet && *ws != "") {
switch {
case (globalSet && *global) && (wsSet && *ws != ""):
return "", errors.New("cannot set a secret to both global and workspace scope")
} else if globalSet && *global {
case globalSet && *global:
context = "global"
} else if wsSet && *ws != "" {
case wsSet && *ws != "":
var found bool
for _, curWs := range currentConfig.Workspaces {
if curWs == *ws {
Expand All @@ -51,7 +54,7 @@ func ValidateAndGetContext(cmd *cobra.Command, currentConfig *config.RootConfig)
return "", fmt.Errorf("workspace %s does not exist", *ws)
}
context = *ws
} else {
default:
log.Debug().Msg("defaulting to the current workspace")
context = currentConfig.CurrentWorkspace
}
Expand Down
27 changes: 18 additions & 9 deletions internal/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"fmt"
"os"

"gopkg.in/yaml.v3"

"github.com/jahvon/flow/internal/common"
"github.com/jahvon/flow/internal/io"
"github.com/jahvon/flow/internal/workspace"
"gopkg.in/yaml.v3"
)

var (
Expand Down Expand Up @@ -60,16 +61,11 @@ func LoadConfig() *RootConfig {

var config *RootConfig
file, err := os.Open(RootConfigPath)
if err != nil {
if err != nil { //nolint: nestif
if os.IsNotExist(err) {
config = defaultConfig()
file, err = os.Create(RootConfigPath)
if err != nil {
log.Panic().Err(err).Msg("unable to create config file")
}
err = writeConfigFile(config)
config, err = initializeDefaultConfig()
if err != nil {
log.Panic().Err(err).Msg("unable to write config file")
log.Panic().Err(err).Msg("unable to initialize default config")
}
} else {
log.Panic().Err(err).Msg("unable to open config file")
Expand Down Expand Up @@ -109,3 +105,16 @@ func defaultConfig() *RootConfig {
CurrentWorkspace: "default",
}
}

func initializeDefaultConfig() (*RootConfig, error) {
config := defaultConfig()
_, err := os.Create(RootConfigPath)
if err != nil {
return nil, fmt.Errorf("unable to create config file - %w", err)
}
err = writeConfigFile(config)
if err != nil {
return nil, fmt.Errorf("unable to write config file - %w", err)
}
return config, nil
}
38 changes: 21 additions & 17 deletions internal/executable/parameter/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ const (
)

func (p *Parameter) Validate() error {
if p.Text == "" && p.SecretRef == "" && p.Prompt == "" {
switch {
case p.Text == "" && p.SecretRef == "" && p.Prompt == "":
return errors.New("must set either text, secretRef, or prompt for parameter")
} else if p.Text != "" && p.SecretRef != "" {
case p.Text != "" && p.SecretRef != "":
return errors.New("cannot set both text and secretRef for parameter")
} else if p.Text != "" && p.Prompt != "" {
case p.Text != "" && p.Prompt != "":
return errors.New("cannot set both text and prompt for parameter")
} else if p.SecretRef != "" && p.Prompt != "" {
case p.SecretRef != "" && p.Prompt != "":
return errors.New("cannot set both secretRef and prompt for parameter")
}

Expand All @@ -49,24 +50,27 @@ func (p *Parameter) Validate() error {
}

func (p *Parameter) Value() (string, error) {
if p.Text == "" && p.SecretRef == "" && p.Prompt == "" {
switch {
case p.Text == "" && p.SecretRef == "" && p.Prompt == "":
return "", nil
} else if p.Text != "" {
case p.Text != "":
return p.Text, nil
} else if p.Prompt != "" {
case p.Prompt != "":
response := io.Ask(p.Prompt)
return response, nil
case p.SecretRef != "":
if err := vault.ValidateReference(p.SecretRef); err != nil {
return "", err
}
v := vault.NewVault()
secret, err := v.GetSecret(p.SecretRef)
if err != nil {
return "", err
}
return secret.PlainTextString(), nil
default:
return "", errors.New("failed to get value for parameter")
}

if err := vault.ValidateReference(p.SecretRef); err != nil {
return "", err
}
v := vault.NewVault()
secret, err := v.GetSecret(p.SecretRef)
if err != nil {
return "", err
}
return secret.PlainTextString(), nil
}

func ParameterListToEnvList(params []Parameter) ([]string, error) {
Expand Down
Loading

0 comments on commit b1ecc95

Please sign in to comment.