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

architecture #3

Merged
merged 9 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ install: bin

.PHONY: bin
bin:
go build -o ./ize
go build -o ./ize ./cmd
8 changes: 6 additions & 2 deletions main.go → cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ limitations under the License.
*/
package main

import "github.com/hazelops/ize/cmd"
import (
"os"

"github.com/hazelops/ize/commands"
)

func main() {
cmd.Execute()
commands.Execute(os.Args[1:])
}
156 changes: 156 additions & 0 deletions commands/base.go
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
}
41 changes: 41 additions & 0 deletions commands/base_init.go
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
}
7 changes: 7 additions & 0 deletions commands/comandeer.go
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
}
7 changes: 7 additions & 0 deletions commands/helpers.go
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
}
24 changes: 24 additions & 0 deletions commands/ize.go
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
}
Loading