Skip to content

Commit

Permalink
cmd: use dot to specify current dir with --outdir flag
Browse files Browse the repository at this point in the history
Signed-off-by: luigidematteis <[email protected]>
  • Loading branch information
luigidematteis committed Dec 4, 2024
1 parent e8fb961 commit eb0783d
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 59 deletions.
13 changes: 4 additions & 9 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
_ "github.com/sighupio/furyctl/internal/apis/kfd/v1alpha2/ekscluster"
"github.com/sighupio/furyctl/internal/app"
"github.com/sighupio/furyctl/internal/cluster"
utils "github.com/sighupio/furyctl/internal/cmdutils"
"github.com/sighupio/furyctl/internal/config"
"github.com/sighupio/furyctl/internal/git"
"github.com/sighupio/furyctl/internal/lockfile"
Expand Down Expand Up @@ -98,20 +99,14 @@ func NewApplyCmd() *cobra.Command {

outDir := flags.Outdir

// Get home dir.
logrus.Debug("Getting Home Directory Path...")

homeDir, err := os.UserHomeDir()
outDir, err = utils.ResolveOutputDirectory(outDir)
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

return fmt.Errorf("error while getting user home directory: %w", err)
return fmt.Errorf("Error while resolving output dir: %w", err)
}

if outDir == "" {
outDir = homeDir
}
logrus.Debug("Resolved output directory: ", outDir)

if flags.BinPath == "" {
flags.BinPath = filepath.Join(outDir, ".furyctl", "bin")
Expand Down
14 changes: 5 additions & 9 deletions cmd/connect/openvpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package connect
import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/sirupsen/logrus"
Expand All @@ -17,6 +16,7 @@ import (
"github.com/sighupio/fury-distribution/pkg/apis/config"
"github.com/sighupio/furyctl/internal/analytics"
"github.com/sighupio/furyctl/internal/app"
utils "github.com/sighupio/furyctl/internal/cmdutils"
cobrax "github.com/sighupio/furyctl/internal/x/cobra"
execx "github.com/sighupio/furyctl/internal/x/exec"
yamlx "github.com/sighupio/furyctl/pkg/x/yaml"
Expand Down Expand Up @@ -64,20 +64,16 @@ func NewOpenVPNCmd() *cobra.Command {
return ErrProfileFlagRequired
}

// Get home dir.
logrus.Debug("Getting Home Directory Path...")
outDir := flags.Outdir
homeDir, err := os.UserHomeDir()

outDir, err := utils.ResolveOutputDirectory(outDir)
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

return fmt.Errorf("%w: %w", ErrCannotGetHomeDir, err)
return fmt.Errorf("Error while resolving output dir: %w", err)
}

if outDir == "" {
outDir = homeDir
}
logrus.Debug("Resolved output directory: ", outDir)

// Parse furyctl.yaml config.
logrus.Debug("Parsing furyctl.yaml file...")
Expand Down
12 changes: 4 additions & 8 deletions cmd/delete/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/sighupio/furyctl/internal/analytics"
"github.com/sighupio/furyctl/internal/app"
"github.com/sighupio/furyctl/internal/cluster"
utils "github.com/sighupio/furyctl/internal/cmdutils"
"github.com/sighupio/furyctl/internal/config"
"github.com/sighupio/furyctl/internal/git"
"github.com/sighupio/furyctl/internal/lockfile"
Expand Down Expand Up @@ -85,19 +86,14 @@ func NewClusterCmd() *cobra.Command {

outDir := flags.Outdir

logrus.Debug("Getting Home Directory Path...")

homeDir, err := os.UserHomeDir()
outDir, err = utils.ResolveOutputDirectory(outDir)
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

return fmt.Errorf("error while getting user home directory: %w", err)
return fmt.Errorf("Error while resolving output dir: %w", err)
}

if outDir == "" {
outDir = homeDir
}
logrus.Debug("Resolved output directory: ", outDir)

if flags.BinPath == "" {
flags.BinPath = filepath.Join(outDir, ".furyctl", "bin")
Expand Down
12 changes: 4 additions & 8 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cmd

import (
"fmt"
"os"
"path/filepath"

"github.com/r3labs/diff/v3"
Expand All @@ -18,6 +17,7 @@ import (
"github.com/sighupio/furyctl/internal/analytics"
"github.com/sighupio/furyctl/internal/app"
"github.com/sighupio/furyctl/internal/cluster"
utils "github.com/sighupio/furyctl/internal/cmdutils"
"github.com/sighupio/furyctl/internal/git"
"github.com/sighupio/furyctl/internal/state"
cobrax "github.com/sighupio/furyctl/internal/x/cobra"
Expand Down Expand Up @@ -67,20 +67,16 @@ func NewDiffCmd() *cobra.Command {

execx.Debug = flags.Debug

logrus.Debug("Getting Home Directory Path...")
outDir := flags.Outdir

homeDir, err := os.UserHomeDir()
outDir, err = utils.ResolveOutputDirectory(outDir)
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

return fmt.Errorf("error while getting user home directory: %w", err)
return fmt.Errorf("Error while resolving output dir: %w", err)
}

if outDir == "" {
outDir = homeDir
}
logrus.Debug("Resolved output directory: ", outDir)

if flags.BinPath == "" {
flags.BinPath = filepath.Join(outDir, ".furyctl", "bin")
Expand Down
28 changes: 4 additions & 24 deletions cmd/dump/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package dump
import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/sirupsen/logrus"
Expand All @@ -16,6 +15,7 @@ import (

"github.com/sighupio/furyctl/internal/analytics"
"github.com/sighupio/furyctl/internal/app"
utils "github.com/sighupio/furyctl/internal/cmdutils"
"github.com/sighupio/furyctl/internal/config"
"github.com/sighupio/furyctl/internal/distribution"
"github.com/sighupio/furyctl/internal/git"
Expand Down Expand Up @@ -82,20 +82,14 @@ The generated folder will be created starting from a provided templates folder a

outDir := flags.Outdir

// Get home dir.
logrus.Debug("Getting Home Directory Path...")

homeDir, err := os.UserHomeDir()
outDir, err = utils.ResolveOutputDirectory(outDir)
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

return fmt.Errorf("error while getting user home directory: %w", err)
return fmt.Errorf("Error while resolving output dir: %w", err)
}

if outDir == "" {
outDir = homeDir
}
logrus.Debug("Resolved output directory: ", outDir)

absDistroPatchesLocation := flags.DistroPatchesLocation

Expand Down Expand Up @@ -161,20 +155,6 @@ The generated folder will be created starting from a provided templates folder a
return fmt.Errorf("%s - %w", absFuryctlPath, err)
}

outDir = flags.Outdir

currentDir, err := os.Getwd()
if err != nil {
cmdEvent.AddErrorMessage(err)
tracker.Track(cmdEvent)

return fmt.Errorf("error while getting current directory: %w", err)
}

if outDir == "" {
outDir = currentDir
}

outDir = filepath.Join(outDir, "distribution")

logrus.Info("Generating distribution manifests...")
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func NewRootCmd() *RootCommand {
"outdir",
"o",
"",
"Change the directory where to create the data directory (.furyctl)",
"Change the directory where to create the data directory (.furyctl). Default to $HOME.",
)

rootCmd.PersistentFlags().StringVarP(
Expand Down
38 changes: 38 additions & 0 deletions internal/cmdutils/directory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package utils

import (
"fmt"
"os"

"github.com/sirupsen/logrus"
)

// ResolveOutputDirectory determines the output directory based on user input.
// It defaults to the user's home directory if `outDir` is empty.
func ResolveOutputDirectory(outDir string) (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("Error while getting user home directory: %w", err)
}

logrus.Debug("Resolved home directory: ", homeDir)

if outDir == "" {
outDir = homeDir
logrus.Debug("Empty outdir flag. Set outdir to: ", homeDir)
}

currentDir, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("Error while getting current working directory: %w", err)
}

logrus.Debug("Resolved current directory: ", currentDir)

if outDir == "." {
outDir = currentDir
logrus.Debug("Set output dir to current directory: ", currentDir)
}

return outDir, nil
}

0 comments on commit eb0783d

Please sign in to comment.