From f85ec9a306478db4377c78f5f916bc5d8f3aeee8 Mon Sep 17 00:00:00 2001 From: Christian Winther Date: Fri, 9 Feb 2024 15:14:45 +0100 Subject: [PATCH] reorgnize command names into their own folders --- cmd/cmd_fmt.go | 19 ----------- cmd/cmd_json.go | 25 -------------- cmd/cmd_print.go | 22 ------------- cmd/{cmd_disable.go => disable/disable.go} | 14 +++++--- cmd/{cmd_enable.go => enable/enable.go} | 14 +++++--- cmd/fmt/fmt.go | 24 ++++++++++++++ cmd/{cmd_groups.go => groups/groups.go} | 17 ++++++---- cmd/json/json.go | 30 +++++++++++++++++ cmd/main.go | 33 ++++++++++++------- cmd/print/print.go | 27 +++++++++++++++ cmd/{cmd_set.go => set/set.go} | 14 +++++--- cmd/{cmd_update.go => update/update.go} | 20 ++++++----- cmd/{cmd_validate.go => validate/validate.go} | 19 +++++++---- cmd/{cmd_value.go => value/value.go} | 13 +++++--- {cmd => pkg/cli/shared}/flags.go | 4 +-- {cmd => pkg/cli/shared}/setup.go | 15 ++++----- 16 files changed, 183 insertions(+), 127 deletions(-) delete mode 100644 cmd/cmd_fmt.go delete mode 100644 cmd/cmd_json.go delete mode 100644 cmd/cmd_print.go rename cmd/{cmd_disable.go => disable/disable.go} (57%) rename cmd/{cmd_enable.go => enable/enable.go} (58%) create mode 100644 cmd/fmt/fmt.go rename cmd/{cmd_groups.go => groups/groups.go} (61%) create mode 100644 cmd/json/json.go create mode 100644 cmd/print/print.go rename cmd/{cmd_set.go => set/set.go} (93%) rename cmd/{cmd_update.go => update/update.go} (88%) rename cmd/{cmd_validate.go => validate/validate.go} (76%) rename cmd/{cmd_value.go => value/value.go} (72%) rename {cmd => pkg/cli/shared}/flags.go (97%) rename {cmd => pkg/cli/shared}/setup.go (70%) diff --git a/cmd/cmd_fmt.go b/cmd/cmd_fmt.go deleted file mode 100644 index f8ff767..0000000 --- a/cmd/cmd_fmt.go +++ /dev/null @@ -1,19 +0,0 @@ -package main - -import ( - "context" - - "github.com/jippi/dottie/pkg" - "github.com/urfave/cli/v3" -) - -var formatCmd = &cli.Command{ - Name: "fmt", - Usage: "Format the file", - Before: setup, - Action: func(_ context.Context, cmd *cli.Command) error { - pkg.Save(cmd.String("file"), env) - - return nil - }, -} diff --git a/cmd/cmd_json.go b/cmd/cmd_json.go deleted file mode 100644 index 3e876f1..0000000 --- a/cmd/cmd_json.go +++ /dev/null @@ -1,25 +0,0 @@ -package main - -import ( - "context" - "encoding/json" - "fmt" - - "github.com/urfave/cli/v3" -) - -var jsonCommand = &cli.Command{ - Name: "json", - Usage: "Print as JSON", - Before: setup, - Action: func(_ context.Context, _ *cli.Command) error { - b, err := json.MarshalIndent(env, "", " ") - if err != nil { - return err - } - - fmt.Println(string(b)) - - return nil - }, -} diff --git a/cmd/cmd_print.go b/cmd/cmd_print.go deleted file mode 100644 index 82d68e7..0000000 --- a/cmd/cmd_print.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -import ( - "context" - "fmt" - - "github.com/jippi/dottie/pkg/render" - "github.com/urfave/cli/v3" -) - -var printCommand = &cli.Command{ - Name: "print", - Usage: "Print environment variables", - Before: setup, - Action: func(_ context.Context, _ *cli.Command) error { - settings.Apply(render.WithInterpolation(true)) - - fmt.Println(render.NewRenderer(*settings).Statement(env).String()) - - return nil - }, -} diff --git a/cmd/cmd_disable.go b/cmd/disable/disable.go similarity index 57% rename from cmd/cmd_disable.go rename to cmd/disable/disable.go index 695cd44..1c96103 100644 --- a/cmd/cmd_disable.go +++ b/cmd/disable/disable.go @@ -1,19 +1,23 @@ -package main +package disable import ( "context" "github.com/jippi/dottie/pkg" - + "github.com/jippi/dottie/pkg/cli/shared" "github.com/urfave/cli/v3" ) -var disableCommand = &cli.Command{ +var Command = &cli.Command{ Name: "disable", Usage: "Comment/disable a key if it exists", - Before: setup, ArgsUsage: "KEY", - Action: func(_ context.Context, cmd *cli.Command) error { + Action: func(ctx context.Context, cmd *cli.Command) error { + env, _, err := shared.Setup(ctx, cmd) + if err != nil { + return err + } + key := cmd.Args().Get(0) existing := env.Get(key) existing.Active = false diff --git a/cmd/cmd_enable.go b/cmd/enable/enable.go similarity index 58% rename from cmd/cmd_enable.go rename to cmd/enable/enable.go index 5dd3b82..2616055 100644 --- a/cmd/cmd_enable.go +++ b/cmd/enable/enable.go @@ -1,19 +1,23 @@ -package main +package enable import ( "context" "github.com/jippi/dottie/pkg" - + "github.com/jippi/dottie/pkg/cli/shared" "github.com/urfave/cli/v3" ) -var enableCommand = &cli.Command{ +var Command = &cli.Command{ Name: "enable", Usage: "Uncomment/enable a key if it exists", - Before: setup, ArgsUsage: "KEY", - Action: func(_ context.Context, cmd *cli.Command) error { + Action: func(ctx context.Context, cmd *cli.Command) error { + env, _, err := shared.Setup(ctx, cmd) + if err != nil { + return err + } + key := cmd.Args().Get(0) existing := env.Get(key) diff --git a/cmd/fmt/fmt.go b/cmd/fmt/fmt.go new file mode 100644 index 0000000..fe66c2c --- /dev/null +++ b/cmd/fmt/fmt.go @@ -0,0 +1,24 @@ +package fmt + +import ( + "context" + + "github.com/jippi/dottie/pkg" + "github.com/jippi/dottie/pkg/cli/shared" + "github.com/urfave/cli/v3" +) + +var Command = &cli.Command{ + Name: "fmt", + Usage: "Format the file", + Action: func(ctx context.Context, cmd *cli.Command) error { + env, _, err := shared.Setup(ctx, cmd) + if err != nil { + return err + } + + pkg.Save(cmd.String("file"), env) + + return nil + }, +} diff --git a/cmd/cmd_groups.go b/cmd/groups/groups.go similarity index 61% rename from cmd/cmd_groups.go rename to cmd/groups/groups.go index 9606dcb..0c24053 100644 --- a/cmd/cmd_groups.go +++ b/cmd/groups/groups.go @@ -1,18 +1,23 @@ -package main +package groups import ( "context" "fmt" "github.com/gosimple/slug" + "github.com/jippi/dottie/pkg/cli/shared" "github.com/urfave/cli/v3" ) -var groupsCommand = &cli.Command{ - Name: "groups", - Usage: "Print groups found in the .env file", - Before: setup, - Action: func(_ context.Context, _ *cli.Command) error { +var Command = &cli.Command{ + Name: "groups", + Usage: "Print groups found in the .env file", + Action: func(ctx context.Context, cmd *cli.Command) error { + env, _, err := shared.Setup(ctx, cmd) + if err != nil { + return err + } + groups := env.Groups if len(groups) == 0 { return fmt.Errorf("No groups found") diff --git a/cmd/json/json.go b/cmd/json/json.go new file mode 100644 index 0000000..a882be0 --- /dev/null +++ b/cmd/json/json.go @@ -0,0 +1,30 @@ +package json + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/jippi/dottie/pkg/cli/shared" + "github.com/urfave/cli/v3" +) + +var Command = &cli.Command{ + Name: "json", + Usage: "Print as JSON", + Action: func(ctx context.Context, cmd *cli.Command) error { + env, _, err := shared.Setup(ctx, cmd) + if err != nil { + return err + } + + b, err := json.MarshalIndent(env, "", " ") + if err != nil { + return err + } + + fmt.Println(string(b)) + + return nil + }, +} diff --git a/cmd/main.go b/cmd/main.go index d52edb9..07737dc 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -9,7 +9,18 @@ import ( goversion "github.com/caarlos0/go-version" "github.com/davecgh/go-spew/spew" + "github.com/jippi/dottie/cmd/disable" + "github.com/jippi/dottie/cmd/enable" + "github.com/jippi/dottie/cmd/fmt" + "github.com/jippi/dottie/cmd/groups" + "github.com/jippi/dottie/cmd/json" + print_cmd "github.com/jippi/dottie/cmd/print" + "github.com/jippi/dottie/cmd/set" + "github.com/jippi/dottie/cmd/update" + "github.com/jippi/dottie/cmd/validate" + "github.com/jippi/dottie/cmd/value" "github.com/jippi/dottie/pkg/ast" + "github.com/jippi/dottie/pkg/cli/shared" "github.com/jippi/dottie/pkg/render" "github.com/urfave/cli/v3" ) @@ -48,18 +59,18 @@ func main() { Suggest: true, EnableShellCompletion: true, ShellCompletionCommandName: "completions", - Flags: globalFlags, + Flags: shared.GlobalFlags, Commands: []*cli.Command{ - disableCommand, - enableCommand, - groupsCommand, - formatCmd, - jsonCommand, - printCommand, - setCommand, - updateCommand, - validateCommand, - valueCommand, + disable.Command, + enable.Command, + fmt.Command, + groups.Command, + json.Command, + print_cmd.Command, + set.Command, + update.Command, + validate.Command, + value.Command, }, } diff --git a/cmd/print/print.go b/cmd/print/print.go new file mode 100644 index 0000000..5958766 --- /dev/null +++ b/cmd/print/print.go @@ -0,0 +1,27 @@ +package print_cmd + +import ( + "context" + "fmt" + + "github.com/jippi/dottie/pkg/cli/shared" + "github.com/jippi/dottie/pkg/render" + "github.com/urfave/cli/v3" +) + +var Command = &cli.Command{ + Name: "print", + Usage: "Print environment variables", + Action: func(ctx context.Context, cmd *cli.Command) error { + env, settings, err := shared.Setup(ctx, cmd) + if err != nil { + return err + } + + settings.Apply(render.WithInterpolation(true)) + + fmt.Println(render.NewRenderer(*settings).Statement(env).String()) + + return nil + }, +} diff --git a/cmd/cmd_set.go b/cmd/set/set.go similarity index 93% rename from cmd/cmd_set.go rename to cmd/set/set.go index 6b8a560..19348b6 100644 --- a/cmd/cmd_set.go +++ b/cmd/set/set.go @@ -1,4 +1,4 @@ -package main +package set import ( "context" @@ -7,18 +7,17 @@ import ( "github.com/jippi/dottie/pkg" "github.com/jippi/dottie/pkg/ast" + "github.com/jippi/dottie/pkg/cli/shared" "github.com/jippi/dottie/pkg/token" "github.com/jippi/dottie/pkg/tui" "github.com/jippi/dottie/pkg/validation" - "github.com/urfave/cli/v3" ) -var setCommand = &cli.Command{ +var Command = &cli.Command{ Name: "set", Usage: "Set/update one or multiple key=value pairs", UsageText: "set KEY=VALUE [KEY=VALUE ...]", - Before: setup, Flags: []cli.Flag{ &cli.BoolFlag{ Name: "disabled", @@ -71,7 +70,12 @@ var setCommand = &cli.Command{ Usage: "Set one or multiple lines of comments to the KEY=VALUE pair", }, }, - Action: func(_ context.Context, cmd *cli.Command) error { + Action: func(ctx context.Context, cmd *cli.Command) error { + env, _, err := shared.Setup(ctx, cmd) + if err != nil { + return err + } + if cmd.Args().Len() == 0 { return fmt.Errorf("Missing required argument: KEY=VALUE") } diff --git a/cmd/cmd_update.go b/cmd/update/update.go similarity index 88% rename from cmd/cmd_update.go rename to cmd/update/update.go index c3c83db..f5e5a3b 100644 --- a/cmd/cmd_update.go +++ b/cmd/update/update.go @@ -1,4 +1,4 @@ -package main +package update import ( "context" @@ -7,18 +7,22 @@ import ( "io" "os" + "github.com/hashicorp/go-getter" "github.com/jippi/dottie/pkg" "github.com/jippi/dottie/pkg/ast" - - "github.com/hashicorp/go-getter" + "github.com/jippi/dottie/pkg/cli/shared" "github.com/urfave/cli/v3" ) -var updateCommand = &cli.Command{ - Name: "update", - Usage: "Update the .env file from a source", - Before: setup, - Action: func(_ context.Context, _ *cli.Command) error { +var Command = &cli.Command{ + Name: "update", + Usage: "Update the .env file from a source", + Action: func(ctx context.Context, cmd *cli.Command) error { + env, _, err := shared.Setup(ctx, cmd) + if err != nil { + return err + } + fmt.Print("Finding source") source, err := env.GetConfig("dottie/source") if err != nil { diff --git a/cmd/cmd_validate.go b/cmd/validate/validate.go similarity index 76% rename from cmd/cmd_validate.go rename to cmd/validate/validate.go index a2a81c5..f47dbc5 100644 --- a/cmd/cmd_validate.go +++ b/cmd/validate/validate.go @@ -1,4 +1,4 @@ -package main +package validate import ( "context" @@ -6,16 +6,21 @@ import ( "fmt" "github.com/jippi/dottie/pkg" + "github.com/jippi/dottie/pkg/cli/shared" "github.com/jippi/dottie/pkg/tui" "github.com/jippi/dottie/pkg/validation" "github.com/urfave/cli/v3" ) -var validateCommand = &cli.Command{ - Name: "validate", - Usage: "Validate .env file", - Before: setup, - Action: func(_ context.Context, cmd *cli.Command) error { +var Command = &cli.Command{ + Name: "validate", + Usage: "Validate .env file", + Action: func(ctx context.Context, cmd *cli.Command) error { + env, _, err := shared.Setup(ctx, cmd) + if err != nil { + return err + } + res := validation.Validate(env) if len(res) == 0 { tui.Theme.Success.StderrPrinter().Box("No validation errors found") @@ -31,7 +36,7 @@ var validateCommand = &cli.Command{ validation.Explain(env, errIsh) } - env, err := pkg.Load(cmd.String("file")) + env, err = pkg.Load(cmd.String("file")) if err != nil { return fmt.Errorf("failed to reload .env file: %w", err) } diff --git a/cmd/cmd_value.go b/cmd/value/value.go similarity index 72% rename from cmd/cmd_value.go rename to cmd/value/value.go index eb9c927..d266ccd 100644 --- a/cmd/cmd_value.go +++ b/cmd/value/value.go @@ -1,18 +1,23 @@ -package main +package value import ( "context" "fmt" + "github.com/jippi/dottie/pkg/cli/shared" "github.com/urfave/cli/v3" ) -var valueCommand = &cli.Command{ +var Command = &cli.Command{ Name: "value", Usage: "Print value of a env key if it exists", - Before: setup, ArgsUsage: "KEY", - Action: func(_ context.Context, cmd *cli.Command) error { + Action: func(ctx context.Context, cmd *cli.Command) error { + env, _, err := shared.Setup(ctx, cmd) + if err != nil { + return err + } + key := cmd.Args().Get(0) if len(key) == 0 { return fmt.Errorf("Missing required argument: KEY") diff --git a/cmd/flags.go b/pkg/cli/shared/flags.go similarity index 97% rename from cmd/flags.go rename to pkg/cli/shared/flags.go index f4687e2..da588e2 100644 --- a/cmd/flags.go +++ b/pkg/cli/shared/flags.go @@ -1,8 +1,8 @@ -package main +package shared import "github.com/urfave/cli/v3" -var globalFlags = []cli.Flag{ +var GlobalFlags = []cli.Flag{ &cli.StringFlag{ Name: "file", Category: "Input:", diff --git a/cmd/setup.go b/pkg/cli/shared/setup.go similarity index 70% rename from cmd/setup.go rename to pkg/cli/shared/setup.go index 6c17e9c..adca1fe 100644 --- a/cmd/setup.go +++ b/pkg/cli/shared/setup.go @@ -1,23 +1,22 @@ -package main +package shared import ( "context" "github.com/jippi/dottie/pkg" + "github.com/jippi/dottie/pkg/ast" "github.com/jippi/dottie/pkg/render" "github.com/urfave/cli/v3" ) -func setup(_ context.Context, cmd *cli.Command) error { - var err error - - env, err = pkg.Load(cmd.String("file")) +func Setup(_ context.Context, cmd *cli.Command) (*ast.Document, *render.Settings, error) { + env, err := pkg.Load(cmd.String("file")) if err != nil { - return err + return nil, nil, err } - settings = render.NewSettings( + settings := render.NewSettings( render.WithBlankLines(cmd.Root().Bool("with-blank-lines")), render.WithColors(cmd.Root().Bool("colors")), render.WithComments(cmd.Root().Bool("with-comments")), @@ -31,5 +30,5 @@ func setup(_ context.Context, cmd *cli.Command) error { settings.Apply(render.WithFormattedOutput(true)) } - return nil + return env, settings, nil }