Skip to content

Commit

Permalink
Simplify envtool shell subcommands (#2614)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi authored May 12, 2023
1 parent 1a3a301 commit de38f79
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 47 deletions.
45 changes: 18 additions & 27 deletions cmd/envtool/envtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func printDiagnosticData(setupError error, logger *zap.SugaredLogger) {
}

// mkdir creates all directories from given paths.
func mkdir(paths []string) error {
func mkdir(paths ...string) error {
var errs error

for _, path := range paths {
Expand All @@ -331,15 +331,11 @@ func mkdir(paths []string) error {
}
}

if errs != nil {
return errs
}

return nil
return errs
}

// rmdir removes all directories from given paths.
func rmdir(paths []string) error {
func rmdir(paths ...string) error {
var errs error

for _, path := range paths {
Expand All @@ -348,18 +344,14 @@ func rmdir(paths []string) error {
}
}

if errs != nil {
return errs
}

return nil
return errs
}

// cli struct represents all command-line commands, fields and flags.
// It's used for parsing the user input.
var cli struct {
Debug bool `help:"Enable debug mode."`
Setup struct{} `cmd:""`
Setup struct{} `cmd:"" help:"Setup development environment."`
Shell struct {
Mkdir struct {
Paths []string `arg:"" name:"path" help:"Paths to create." type:"path"`
Expand Down Expand Up @@ -389,22 +381,21 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

args := kongCtx.Args
switch kongCtx.Command() {
var err error

switch cmd := kongCtx.Command(); cmd {
case "setup":
if err := setup(ctx, logger); err != nil {
printDiagnosticData(err, logger)
os.Exit(1)
}
err = setup(ctx, logger)
case "shell mkdir <path>":
if err := mkdir(args[2:]); err != nil {
printDiagnosticData(err, logger)
os.Exit(1)
}
err = mkdir(cli.Shell.Mkdir.Paths...)
case "shell rmdir <path>":
if err := rmdir(args[2:]); err != nil {
printDiagnosticData(err, logger)
os.Exit(1)
}
err = rmdir(cli.Shell.Rmdir.Paths...)
default:
err = fmt.Errorf("unknown command: %s", cmd)
}

if err != nil {
printDiagnosticData(err, logger)
os.Exit(1)
}
}
26 changes: 6 additions & 20 deletions cmd/envtool/envtool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package main

import (
"errors"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -37,38 +35,26 @@ func TestPrintDiagnosticData(t *testing.T) {
func TestRmdirAbsentDir(t *testing.T) {
t.Parallel()

err := rmdir([]string{"rz"})
err := rmdir("absent")
assert.NoError(t, err)
}

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

paths := []string{"ab/c"}
paths := []string{"ab/c", "ab"}

err := mkdir(paths)
err := mkdir(paths...)
assert.NoError(t, err)

// check if paths exist.
var errs error

for _, path := range paths {
if _, err = os.Stat(path); err != nil {
errs = errors.Join(errs, err)
}
assert.DirExists(t, path)
}

assert.NoError(t, errs)

err = rmdir(paths)
err = rmdir(paths...)
assert.NoError(t, err)

// check if paths do not exist.
for _, path := range paths {
if _, err = os.Stat(path); !os.IsNotExist(err) {
errs = errors.Join(errs, err)
}
assert.NoDirExists(t, path)
}

assert.NoError(t, errs)
}

0 comments on commit de38f79

Please sign in to comment.