Skip to content

Commit

Permalink
enable to switch to opentofu client
Browse files Browse the repository at this point in the history
opentofu client is not implemented yet.
  • Loading branch information
cappyzawa committed Apr 29, 2024
1 parent 5477b25 commit e904a4b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 11 deletions.
14 changes: 12 additions & 2 deletions cmd/tfswitch/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import (
"flag"
"io"
"os"
"runtime/debug"

"github.com/cappyzawa/tfswitch/v2/internal/di"
"github.com/cappyzawa/tfswitch/v2/internal/flags"
"github.com/mitchellh/cli"
)

Expand All @@ -20,6 +22,10 @@ type runnner struct {
}

func (r *runnner) Run(args []string) int {
var globalFlags flags.Global
flag.StringVar(&globalFlags.Target, "target", "terraform", "Target version")
flag.Parse()

ui := &cli.ColoredUi{
ErrorColor: cli.UiColorRed,
WarnColor: cli.UiColorYellow,
Expand All @@ -34,8 +40,12 @@ func (r *runnner) Run(args []string) int {
version = getVersion()
}
c := cli.NewCLI(args[0], version)
c.Args = args[1:]
dc := di.NewContainer(ui, r.dataHome)
c.Args = flag.Args()
dc, err := di.NewContainer(ui, r.dataHome, globalFlags.Target)
if err != nil {
ui.Error(err.Error())
return 1
}
c.Commands = map[string]cli.CommandFactory{
"use": dc.UseCommand,
"local-list": dc.LocalListCommand,
Expand Down
11 changes: 6 additions & 5 deletions internal/command/remote-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"strings"

"github.com/cappyzawa/tfswitch/v2/internal/flags"
"github.com/cappyzawa/tfswitch/v2/internal/repository"
"github.com/mitchellh/cli"
)
Expand Down Expand Up @@ -31,10 +32,10 @@ Examples:
}

func (c *RemoteListCommand) Run(args []string) int {
var filter string
flags := flag.NewFlagSet("", flag.ExitOnError)
flags.StringVar(&filter, "filter", "", "Filter by the specified version (Prefix Match)")
if err := flags.Parse(args); err != nil {
var f flags.RemoteList
fs := flag.NewFlagSet("remote-list", flag.ContinueOnError)
fs.StringVar(&f.Filter, "filter", "", "Filter by the specified version (Prefix Match)")
if err := fs.Parse(args); err != nil {
c.UI.Error(err.Error())
return 1
}
Expand All @@ -45,7 +46,7 @@ func (c *RemoteListCommand) Run(args []string) int {
return 1
}
for _, v := range versions {
if strings.HasPrefix(v, filter) {
if strings.HasPrefix(v, f.Filter) {
c.UI.Output(v)
}
}
Expand Down
30 changes: 26 additions & 4 deletions internal/di/container.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,57 @@
package di

import (
"errors"
"net/http"
"net/url"

"github.com/cappyzawa/tfswitch/v2/internal/command"
"github.com/cappyzawa/tfswitch/v2/internal/opentofu"
"github.com/cappyzawa/tfswitch/v2/internal/repository"
"github.com/cappyzawa/tfswitch/v2/internal/terraform"
"github.com/mitchellh/cli"
)

const (
TargetTerraform string = "terraform"
TargetOpenTofu string = "opentofu"
)

type Container struct {
UI *cli.ColoredUi
DataHome string

client repository.Client
}

func NewContainer(ui *cli.ColoredUi, dataHome string) *Container {
return &Container{
func NewContainer(ui *cli.ColoredUi, dataHome string, target string) (*Container, error) {
c := &Container{
UI: ui,
DataHome: dataHome,
}
switch target {
case TargetTerraform:
c.client = c.terraformClient()
case TargetOpenTofu:
c.client = c.opentofuClient()
default:
return nil, errors.New("invalid target. must be either terraform or opentofu")
}
return c, nil
}

func (c *Container) UseCommand() (cli.Command, error) {
return &command.UseCommand{
UI: c.UI,
DataHome: c.DataHome,
Client: c.terraformClient(),
Client: c.client,
}, nil
}

func (c *Container) RemoteListCommand() (cli.Command, error) {
return &command.RemoteListCommand{
UI: c.UI,
Client: c.terraformClient(),
Client: c.client,
}, nil
}

Expand All @@ -54,6 +72,10 @@ func (c *Container) terraformClient() repository.Client {
return terraform.NewClient(u, hc)
}

func (c *Container) opentofuClient() repository.Client {
return opentofu.NewClient()
}

func (c *Container) httpClient() *http.Client {
return http.DefaultClient
}
5 changes: 5 additions & 0 deletions internal/flags/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package flags

type Global struct {
Target string
}
6 changes: 6 additions & 0 deletions internal/flags/remote-list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package flags

type RemoteList struct {
Global
Filter string
}
4 changes: 4 additions & 0 deletions internal/opentofu/opentofu.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ var _ repository.Client = (*opentofu)(nil)

type opentofu struct{}

func NewClient() repository.Client {
return &opentofu{}
}

func (o *opentofu) Name() string {
return "opentofu"
}
Expand Down

0 comments on commit e904a4b

Please sign in to comment.