Skip to content

Commit

Permalink
Add option for specify output dir
Browse files Browse the repository at this point in the history
  • Loading branch information
koudaiii committed Oct 13, 2021
1 parent e6ca9a0 commit 0ab6c38
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
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
}

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 @@ -47,6 +47,15 @@ func newMetaImpl(rg string) (Meta, error) {
return nil, fmt.Errorf("creating workspace root %q: %w", rootDir, err)
}

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

rootDir = filepath.Join(currentWorkingDirectory, outputDir)
}

tfDir := filepath.Join(rootDir, "terraform")
if err := os.MkdirAll(tfDir, 0755); err != nil {
return nil, fmt.Errorf("creating terraform cache dir %q: %w", tfDir, 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.")
}

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

0 comments on commit 0ab6c38

Please sign in to comment.