-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from hazelops/architecture
architecture
- Loading branch information
Showing
16 changed files
with
744 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ install: bin | |
|
||
.PHONY: bin | ||
bin: | ||
go build -o ./ize | ||
go build -o ./ize ./cmd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/hazelops/ize/config" | ||
"github.com/hazelops/ize/pkg/logger" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
var ( | ||
ll string | ||
) | ||
|
||
type baseCmd struct { | ||
cmd *cobra.Command | ||
} | ||
|
||
type baseBuilderCmd struct { | ||
*baseCmd | ||
*commandsBuilder | ||
} | ||
|
||
type izeCmd struct { | ||
*baseBuilderCmd | ||
|
||
//Need to get state app who build | ||
c *commandeer | ||
} | ||
|
||
type commandsBuilder struct { | ||
izeBuilderCommon | ||
|
||
commands []cmder | ||
} | ||
|
||
func newCommandBuilder() *commandsBuilder { | ||
return &commandsBuilder{} | ||
} | ||
|
||
func (b *commandsBuilder) newBuilderCmd(cmd *cobra.Command) *baseBuilderCmd { | ||
bcmd := &baseBuilderCmd{commandsBuilder: b, baseCmd: &baseCmd{cmd: cmd}} | ||
return bcmd | ||
} | ||
|
||
func (b *commandsBuilder) addCommands(commands ...cmder) *commandsBuilder { | ||
b.commands = append(b.commands, commands...) | ||
return b | ||
} | ||
|
||
func (b *commandsBuilder) addAll() *commandsBuilder { | ||
b.addCommands(b.newTerraformCmd()) | ||
|
||
return b | ||
} | ||
|
||
func (b *commandsBuilder) newBuilderBasicCdm(cmd *cobra.Command) *baseBuilderCmd { | ||
bcmd := &baseBuilderCmd{baseCmd: &baseCmd{cmd: cmd}, commandsBuilder: b} | ||
return bcmd | ||
} | ||
|
||
func (b *commandsBuilder) newIzeCmd() *izeCmd { | ||
cc := &izeCmd{} | ||
|
||
cc.baseBuilderCmd = b.newBuilderCmd(&cobra.Command{ | ||
Use: "ize", | ||
Short: "A brief description of your application", | ||
Long: `A longer description that spans multiple lines and likely contains | ||
examples and usage of using your application. For example: | ||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
}) | ||
|
||
cc.cmd.PersistentFlags().StringVarP(&ll, "log-level", "l", "warn", "enable debug message") | ||
cc.cmd.PersistentFlags().StringVarP(&cc.cfgFile, "config-file", "c", "", "set config file name") | ||
|
||
var logLevel zapcore.Level | ||
|
||
// TODO: Fix | ||
switch ll { | ||
case "info": | ||
logLevel = zapcore.InfoLevel | ||
|
||
case "debug": | ||
logLevel = zapcore.DebugLevel | ||
default: | ||
logLevel = zapcore.WarnLevel | ||
} | ||
|
||
cc.log = logger.NewSugaredLogger(logLevel) | ||
|
||
return cc | ||
} | ||
|
||
func addCommands(root *cobra.Command, commands ...cmder) { | ||
for _, command := range commands { | ||
cmd := command.getCommand() | ||
if cmd == nil { | ||
continue | ||
} | ||
root.AddCommand(cmd) | ||
} | ||
} | ||
|
||
func (c *baseCmd) getCommand() *cobra.Command { | ||
return c.cmd | ||
} | ||
|
||
func (b *commandsBuilder) build() *izeCmd { | ||
i := b.newIzeCmd() | ||
addCommands(i.getCommand(), b.commands...) | ||
return i | ||
} | ||
|
||
type izeBuilderCommon struct { | ||
cfgFile string | ||
cfg *config.Config | ||
log logger.StandartLogger | ||
} | ||
|
||
func (cc *izeBuilderCommon) Init() error { | ||
cfg, err := cc.initConfig(cc.cfgFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cc.cfg = cfg | ||
|
||
cwd, err := os.Getwd() | ||
if err != nil { | ||
fmt.Println("Error getting current directory") | ||
} | ||
|
||
// Find home directory. | ||
home, err := os.UserHomeDir() | ||
cobra.CheckErr(err) | ||
|
||
viper.AutomaticEnv() // read in environment variables that match | ||
|
||
//TODO ensure values of the variables are checked for nil before passing down to docker. | ||
|
||
// Global | ||
viper.SetDefault("ROOT_DIR", cwd) | ||
viper.SetDefault("INFRA_DIR", fmt.Sprintf("%v/.infra", cwd)) | ||
viper.SetDefault("ENV_DIR", fmt.Sprintf("%v/.infra/env/%v", cwd, cc.cfg.Env)) | ||
viper.SetDefault("HOME", fmt.Sprintf("%v", home)) | ||
viper.SetDefault("TF_LOG", fmt.Sprintf("")) | ||
viper.SetDefault("TF_LOG_PATH", fmt.Sprintf("%v/tflog.txt", viper.Get("ENV_DIR"))) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package commands | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/hazelops/ize/config" | ||
) | ||
|
||
func (c *izeBuilderCommon) initConfig(filename string) (*config.Config, error) { | ||
path, err := c.initConfigPath(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if path == "" { | ||
return nil, errors.New("A Waypoint configuration file (waypoint.hcl) is required but wasn't found.") | ||
} | ||
|
||
return c.initConfigLoad(path) | ||
} | ||
|
||
func (c *izeBuilderCommon) initConfigPath(filename string) (string, error) { | ||
path, err := config.FindPath(filename) | ||
if err != nil { | ||
return "", fmt.Errorf("Error looking for a Ize configuration: %s", err) | ||
} | ||
|
||
return path, nil | ||
} | ||
|
||
func (c *izeBuilderCommon) initConfigLoad(path string) (*config.Config, error) { | ||
cfg, err := config.Load(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
//TODO: Validate | ||
|
||
return cfg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package commands | ||
|
||
import "github.com/hazelops/ize/pkg/logger" | ||
|
||
type commandeer struct { | ||
logger logger.StandartLogger | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package commands | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
type cmder interface { | ||
getCommand() *cobra.Command | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package commands | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
type Response struct { | ||
Err error | ||
|
||
Cmd *cobra.Command | ||
} | ||
|
||
func Execute(args []string) Response { | ||
izeCmd := newCommandBuilder().addAll().build() | ||
cmd := izeCmd.getCommand() | ||
cmd.SetArgs(args) | ||
|
||
c, err := cmd.ExecuteC() | ||
|
||
var resp Response | ||
|
||
resp.Err = err | ||
resp.Cmd = c | ||
|
||
return resp | ||
} |
Oops, something went wrong.