Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[aztfy enhancement proposal] -o option for specified output dir #15

Merged
merged 9 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ type Config struct {
Logfile string `env:"AZTFY_LOGFILE" default:""`
Debug bool `env:"AZTFY_DEBUG" default:"false"`
MockClient bool `env:"AZTFY_MOCK_CLIENT" default:"false"`
OutputDir string // specified via flagOutputDir
koudaiii marked this conversation as resolved.
Show resolved Hide resolved
}

func NewConfig(rg string) (*Config, error) {
func NewConfig(rg string, outputDir string) (*Config, error) {
var cfg Config
if err := babyenv.Parse(&cfg); err != nil {
return nil, err
}
cfg.ResourceGroupName = rg
cfg.OutputDir = outputDir
return &cfg, nil
}
2 changes: 1 addition & 1 deletion internal/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ func NewMeta(cfg config.Config) (Meta, error) {
if cfg.MockClient {
return newMetaDummy(cfg.ResourceGroupName)
}
return newMetaImpl(cfg.ResourceGroupName)
return newMetaImpl(cfg.ResourceGroupName, cfg.OutputDir)
}
11 changes: 10 additions & 1 deletion internal/meta/meta_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type MetaImpl struct {
armTemplate armtemplate.Template
}

func newMetaImpl(rg string) (Meta, error) {
func newMetaImpl(rg string, outputDir string) (Meta, error) {
ctx := context.TODO()

// Initialize the workspace
Expand All @@ -52,6 +52,15 @@ func newMetaImpl(rg string) (Meta, error) {
return nil, fmt.Errorf("creating terraform cache dir %q: %w", tfDir, err)
}

if outputDir != "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we change the semantic of outputDir here a bit to make it the directory containing all the generated stuff, rather than a directory containing sub-directories where each of them corresponds to a resource group? If so, we need further ensure the outputDir is an empty directory before removing everything in there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@koudaiii Yep! Shall we further check wheter the wsp here is empty? If not, we shall error out, so that we don't accidentally remove anything for the user?

Copy link
Member Author

@koudaiii koudaiii Oct 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review. Ok, We check that wsp is empty. If not, we shall error out because we don't accidentally remove anything.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currentWorkingDirectory, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("error finding the current working directory: %w", err)
}

rootDir = filepath.Join(currentWorkingDirectory, outputDir)
}

wsp := filepath.Join(rootDir, rg)
if err := os.RemoveAll(wsp); err != nil {
return nil, fmt.Errorf("removing existing workspace %q: %w", wsp, err)
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
)

var (
flagVersion *bool
flagVersion *bool
flagOutputDir *string
)

func init() {
flagVersion = flag.Bool("v", false, "Print version")
flagOutputDir = flag.String("o", "", "Specify output dir. Default is user cache dir.")
koudaiii marked this conversation as resolved.
Show resolved Hide resolved
}

const usage = `aztfy [option] <resource group name>
Expand All @@ -39,7 +41,7 @@ func main() {
os.Exit(1)
}

cfg, err := config.NewConfig(flag.Args()[0])
cfg, err := config.NewConfig(flag.Args()[0], *flagOutputDir)
if err != nil {
log.Fatal(err)
}
Expand Down