Skip to content

Commit

Permalink
Merge pull request #5 from jippi/upserter-refactor
Browse files Browse the repository at this point in the history
More refactoring & tests
  • Loading branch information
jippi authored Feb 15, 2024
2 parents a5d649b + 3161d0d commit 44323e2
Show file tree
Hide file tree
Showing 87 changed files with 2,124 additions and 528 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.env
.env.*
!.env.example
env.*

dist/
bin/
Expand Down
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ linters-settings:
ignore-names:
- tt
- err
- i

tagalign:
# Align and sort can be used together or separately.
Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ metadata:
mod_timestamp: "{{ .CommitTimestamp }}"

builds:
- main: ./cmd/
- main: .
env:
- CGO_ENABLED=0
goos:
Expand Down
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@
],
"[go]": {
"editor.formatOnSave": true,
}
},
"cSpell.words": [
"bitmask",
"Upsert",
"upserter"
]
}
4 changes: 2 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tasks:
generates:
- ./dottie
cmds:
- go build -o dottie ./cmd/
- go build -o dottie .

test:
desc: Run tests
Expand All @@ -35,7 +35,7 @@ tasks:
SOURCE_FILES: '{{default "./..." .SOURCE_FILES}}'
TEST_PATTERN: '{{default "." .TEST_PATTERN}}'
cmds:
- go test {{.TEST_OPTIONS}} -race -covermode=atomic -coverprofile=coverage.out {{.SOURCE_FILES}} -run {{.TEST_PATTERN}} -timeout=5m
- go test {{.TEST_OPTIONS}} -race -coverpkg=./... -covermode=atomic -coverprofile=coverage.out {{.SOURCE_FILES}} -run {{.TEST_PATTERN}} -timeout=5m

cover:
desc: Open the cover tool
Expand Down
58 changes: 30 additions & 28 deletions cmd/disable/disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,34 @@ import (
"github.com/spf13/cobra"
)

var Command = &cobra.Command{
Use: "disable KEY",
Short: "Disable (comment out) a KEY if it exists",
GroupID: "manipulate",
ValidArgsFunction: shared.NewCompleter().WithHandlers(render.ExcludeDisabledAssignments).Get(),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("Missing required argument: KEY")
}

key := args[0]

filename := cmd.Flag("file").Value.String()

env, err := pkg.Load(filename)
if err != nil {
return err
}

existing := env.Get(key)
if existing == nil {
return fmt.Errorf("Could not find KEY [%s]", key)
}

existing.Disable()

return pkg.Save(filename, env)
},
func NewCommand() *cobra.Command {
return &cobra.Command{
Use: "disable KEY",
Short: "Disable (comment out) a KEY if it exists",
GroupID: "manipulate",
ValidArgsFunction: shared.NewCompleter().WithHandlers(render.ExcludeDisabledAssignments).Get(),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("Missing required argument: KEY")
}

key := args[0]

filename := cmd.Flag("file").Value.String()

env, err := pkg.Load(filename)
if err != nil {
return err
}

existing := env.Get(key)
if existing == nil {
return fmt.Errorf("Could not find KEY [%s]", key)
}

existing.Disable()

return pkg.Save(filename, env)
},
}
}
13 changes: 13 additions & 0 deletions cmd/disable/disable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package disable_test

import (
"testing"

"github.com/jippi/dottie/pkg/test_helpers"
)

func TestCommand(t *testing.T) {
t.Parallel()

test_helpers.RunFilebasedCommandTests(t, 0, "disable")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KEY_B
6 changes: 6 additions & 0 deletions cmd/disable/tests/disable-a-key-already-disabled.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KEY_A="I'm key A"

# Comment for KEY_B
#KEY_B="I'm key B"

KEY_C="I'm key C"
6 changes: 6 additions & 0 deletions cmd/disable/tests/disable-a-key-already-disabled/env.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KEY_A="I'm key A"

# Comment for KEY_B
#KEY_B="I'm key B"

KEY_C="I'm key C"
1 change: 1 addition & 0 deletions cmd/disable/tests/disable-a-key.command.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KEY_B
6 changes: 6 additions & 0 deletions cmd/disable/tests/disable-a-key.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KEY_A="I'm key A"

# Comment for KEY_B
#KEY_B="I'm key B"

KEY_C="I'm key C"
6 changes: 6 additions & 0 deletions cmd/disable/tests/disable-a-key/env.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KEY_A="I'm key A"

# Comment for KEY_B
#KEY_B="I'm key B"

KEY_C="I'm key C"
1 change: 1 addition & 0 deletions cmd/disable/tests/invalid-key.command.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NONEXISTING_KEY
6 changes: 6 additions & 0 deletions cmd/disable/tests/invalid-key.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KEY_A="I'm key A"

# Comment for KEY_B
KEY_B="I'm key B"

KEY_C="I'm key C"
6 changes: 6 additions & 0 deletions cmd/disable/tests/invalid-key/env.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KEY_A="I'm key A"

# Comment for KEY_B
KEY_B="I'm key B"

KEY_C="I'm key C"
1 change: 1 addition & 0 deletions cmd/disable/tests/invalid-key/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Could not find KEY [NONEXISTING_KEY]
Empty file.
6 changes: 6 additions & 0 deletions cmd/disable/tests/missing-key.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KEY_A="I'm key A"

# Comment for KEY_B
KEY_B="I'm key B"

KEY_C="I'm key C"
6 changes: 6 additions & 0 deletions cmd/disable/tests/missing-key/env.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KEY_A="I'm key A"

# Comment for KEY_B
KEY_B="I'm key B"

KEY_C="I'm key C"
1 change: 1 addition & 0 deletions cmd/disable/tests/missing-key/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Missing required argument: KEY
58 changes: 30 additions & 28 deletions cmd/enable/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,34 @@ import (
"github.com/spf13/cobra"
)

var Command = &cobra.Command{
Use: "enable KEY",
Short: "Enable (uncomment) a KEY if it exists",
GroupID: "manipulate",
ValidArgsFunction: shared.NewCompleter().WithHandlers(render.ExcludeActiveAssignments).Get(),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("Missing required argument: KEY")
}

filename := cmd.Flag("file").Value.String()

env, err := pkg.Load(filename)
if err != nil {
return err
}

key := args[0]

existing := env.Get(key)
if existing == nil {
return fmt.Errorf("Could not find KEY [%s]", key)
}

existing.Enable()

return pkg.Save(filename, env)
},
func NewCommand() *cobra.Command {
return &cobra.Command{
Use: "enable KEY",
Short: "Enable (uncomment) a KEY if it exists",
GroupID: "manipulate",
ValidArgsFunction: shared.NewCompleter().WithHandlers(render.ExcludeActiveAssignments).Get(),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("Missing required argument: KEY")
}

filename := cmd.Flag("file").Value.String()

env, err := pkg.Load(filename)
if err != nil {
return err
}

key := args[0]

existing := env.Get(key)
if existing == nil {
return fmt.Errorf("Could not find KEY [%s]", key)
}

existing.Enable()

return pkg.Save(filename, env)
},
}
}
13 changes: 13 additions & 0 deletions cmd/enable/enable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package enable_test

import (
"testing"

"github.com/jippi/dottie/pkg/test_helpers"
)

func TestCommand(t *testing.T) {
t.Parallel()

test_helpers.RunFilebasedCommandTests(t, 0, "enable")
}
1 change: 1 addition & 0 deletions cmd/enable/tests/enable-a-key-already-enabled.command.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KEY_B
6 changes: 6 additions & 0 deletions cmd/enable/tests/enable-a-key-already-enabled.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KEY_A="I'm key A"

# Comment for KEY_B
KEY_B="I'm key B"

KEY_C="I'm key C"
6 changes: 6 additions & 0 deletions cmd/enable/tests/enable-a-key-already-enabled/env.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KEY_A="I'm key A"

# Comment for KEY_B
KEY_B="I'm key B"

KEY_C="I'm key C"
1 change: 1 addition & 0 deletions cmd/enable/tests/enable-a-key.command.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KEY_B
6 changes: 6 additions & 0 deletions cmd/enable/tests/enable-a-key.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KEY_A="I'm key A"

# Comment for KEY_B
#KEY_B="I'm key B"

KEY_C="I'm key C"
6 changes: 6 additions & 0 deletions cmd/enable/tests/enable-a-key/env.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KEY_A="I'm key A"

# Comment for KEY_B
KEY_B="I'm key B"

KEY_C="I'm key C"
1 change: 1 addition & 0 deletions cmd/enable/tests/invalid-key.command.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NONEXISTING_KEY
6 changes: 6 additions & 0 deletions cmd/enable/tests/invalid-key.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KEY_A="I'm key A"

# Comment for KEY_B
KEY_B="I'm key B"

KEY_C="I'm key C"
6 changes: 6 additions & 0 deletions cmd/enable/tests/invalid-key/env.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KEY_A="I'm key A"

# Comment for KEY_B
KEY_B="I'm key B"

KEY_C="I'm key C"
1 change: 1 addition & 0 deletions cmd/enable/tests/invalid-key/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Could not find KEY [NONEXISTING_KEY]
Empty file.
6 changes: 6 additions & 0 deletions cmd/enable/tests/missing-key.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KEY_A="I'm key A"

# Comment for KEY_B
KEY_B="I'm key B"

KEY_C="I'm key C"
6 changes: 6 additions & 0 deletions cmd/enable/tests/missing-key/env.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
KEY_A="I'm key A"

# Comment for KEY_B
KEY_B="I'm key B"

KEY_C="I'm key C"
1 change: 1 addition & 0 deletions cmd/enable/tests/missing-key/stderr.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Missing required argument: KEY
34 changes: 18 additions & 16 deletions cmd/fmt/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ import (
"github.com/spf13/cobra"
)

var Command = &cobra.Command{
Use: "fmt",
Short: "Format a .env file",
GroupID: "manipulate",
RunE: func(cmd *cobra.Command, args []string) error {
filename := cmd.Flag("file").Value.String()
func NewCommand() *cobra.Command {
return &cobra.Command{
Use: "fmt",
Short: "Format a .env file",
GroupID: "manipulate",
RunE: func(cmd *cobra.Command, args []string) error {
filename := cmd.Flag("file").Value.String()

env, err := pkg.Load(filename)
if err != nil {
return err
}
env, err := pkg.Load(filename)
if err != nil {
return err
}

if err := pkg.Save(filename, env); err != nil {
return err
}
if err := pkg.Save(filename, env); err != nil {
return err
}

tui.Theme.Success.StdoutPrinter().Printfln("File [%s] was successfully formatted", filename)
tui.Theme.Success.StdoutPrinter().Printfln("File [%s] was successfully formatted", filename)

return nil
},
return nil
},
}
}
Loading

0 comments on commit 44323e2

Please sign in to comment.